-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
146 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.contrib import admin | ||
|
||
from ..models.countries import Country | ||
|
||
|
||
@admin.register(Country) | ||
class CountryAdmin(admin.ModelAdmin): | ||
list_display = ("name", "code", "gdp", "currency", "wikipedia_url") | ||
list_filter = ("currency",) | ||
readonly_fields = list_display |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
from core.models.countries import Country | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Scrap object from wikipedia and save in the db" | ||
|
||
def handle(self, *args, **options): | ||
self.stdout.write("Scrapping and saving...") | ||
Country.scrap_from_wikipedia() | ||
self.stdout.write("Done!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 4.2.5 on 2024-01-04 19:17 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("core", "0023_user_asked_to_verify_email"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Country", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("code", models.CharField(max_length=8)), | ||
("name", models.CharField(max_length=64)), | ||
("gdp", models.PositiveIntegerField(default=50000)), | ||
("currency", models.CharField(max_length=8)), | ||
("wikipedia_url", models.URLField(max_length=128)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.5 on 2024-01-04 19:52 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("core", "0024_country"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="country", | ||
name="code", | ||
field=models.CharField(db_index=True, max_length=8, unique=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
from django.db import models | ||
|
||
|
||
class Country(models.Model): | ||
code = models.CharField(max_length=8, unique=True, db_index=True) | ||
name = models.CharField(max_length=64) | ||
gdp = models.PositiveIntegerField(default=50_000) | ||
currency = models.CharField(max_length=8) | ||
wikipedia_url = models.URLField(max_length=128) | ||
|
||
def __str__(self): | ||
return self.code + " " + self.name | ||
|
||
@classmethod | ||
def scrap_from_wikipedia(cls, delete_objs=False): | ||
if delete_objs: | ||
cls.objects.all().delete() | ||
if cls.objects.count() > 0: | ||
return | ||
host = "https://en.wikipedia.org" | ||
url = host + "/wiki/List_of_countries_by_GDP_(nominal)_per_capita" | ||
r = requests.get(url) | ||
soup = BeautifulSoup(r.text, "html.parser") | ||
table = soup.find("table", {"class": "wikitable"}) | ||
for row in table.tbody.find_all("tr"): | ||
name, gdp, c_url = None, None, None | ||
columns = row.find_all("td") | ||
if columns != []: | ||
name = columns[0].text.strip() | ||
c_url = host + columns[0].find_next("a").get("href") | ||
gdp = columns[2].text.strip().replace(",", "") | ||
c_r = requests.get(c_url) | ||
c_soup = BeautifulSoup(c_r.text, "html.parser") | ||
c_table = c_soup.find("table", {"class": "vcard"}) | ||
print(c_url) | ||
currency, code = None, None | ||
for c_tr in c_table.tbody.find_all("tr"): | ||
for c_child in c_tr.children: | ||
if c_child.name == "th": | ||
td = c_child.next_sibling | ||
if c_child.text == "Currency": | ||
try: | ||
anchors = td.find_all("a") | ||
currency = [ | ||
a | ||
for a in anchors | ||
if "ISO 4217" in a.get("title") | ||
][0].text | ||
except Exception as e: | ||
print(e) | ||
|
||
if "ISO 3166" in c_child.text: | ||
code = td.find_all("a")[0].text | ||
properties = (name, gdp, c_url, currency, code) | ||
if all(properties): | ||
cls.objects.create( | ||
name=name, | ||
code=code, | ||
currency=currency, | ||
wikipedia_url=c_url, | ||
gdp=gdp, | ||
) | ||
else: | ||
print(f"## Error {properties} ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,4 +50,5 @@ black | |
pylint-django | ||
async-timeout | ||
huey | ||
geoip2 | ||
geoip2 | ||
beautifulsoup4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters