Skip to content

Commit

Permalink
countries
Browse files Browse the repository at this point in the history
  • Loading branch information
ramibch committed Jan 4, 2024
1 parent 65e3269 commit 4141aab
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .profiles import * # noqa
from .tex import * # noqa
from .users import * # noqa
from .countries import * # noqa
10 changes: 10 additions & 0 deletions core/admin/countries.py
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
13 changes: 13 additions & 0 deletions core/management/commands/scrap_countries.py
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!")
31 changes: 31 additions & 0 deletions core/migrations/0024_country.py
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)),
],
),
]
17 changes: 17 additions & 0 deletions core/migrations/0025_alter_country_code.py
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),
),
]
3 changes: 2 additions & 1 deletion core/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
from .profiles import Profile # noqa
from .tex import Tex # noqa
from .users import User # noqa
from .users import UserPremiumPlan # noqa1
from .users import UserPremiumPlan # noqa
from .countries import Country # noqa
67 changes: 67 additions & 0 deletions core/models/countries.py
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} ")
2 changes: 0 additions & 2 deletions core/models/cvs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import tempfile
import time
from functools import cache
from pathlib import Path
from subprocess import CalledProcessError
from subprocess import PIPE
Expand All @@ -17,7 +16,6 @@
from django.utils import timezone

from ..exceptions import TexError
from .profiles import Profile


now = timezone.now()
Expand Down
3 changes: 2 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ black
pylint-django
async-timeout
huey
geoip2
geoip2
beautifulsoup4
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ babel==2.12.1
backcall==0.2.0
# via ipython
beautifulsoup4==4.11.2
# via wagtail
# via
# -r requirements.in
# wagtail
black==23.9.1
# via -r requirements.in
boto3==1.28.53
Expand Down

0 comments on commit 4141aab

Please sign in to comment.