# Generated by Django 3.0.6 on 2020-05-21 16:45

from django.db import migrations

import requests

QUERY_CATS = """
SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q146.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr,en". } }
"""


def get_all_cats(apps, schema_editor):
    WikiDataModel = apps.get_model("example", "WikiDataModel")

    def wikidata_url_to_id(url: str) -> int:
        """Get a wikidata URL and return the wikidata ID."""
        return int(
            url.split("/")[-1][1:]
        )  # The ID is a string with Q and an int. keep only the int.

    req = requests.get(
        "https://query.wikidata.org/sparql",
        params={"format": "json", "query": QUERY_CATS},
    )
    for result in req.json()["results"]["bindings"]:
        WikiDataModel.objects.create(
            name=result["itemLabel"]["value"],
            wikidata=wikidata_url_to_id(result["item"]["value"]),
        )


def delete_all_cats(apps, schema_editor):
    WikiDataModel = apps.get_model("example", "WikiDataModel")
    WikiDataModel.objects.delete()


class Migration(migrations.Migration):

    dependencies = [
        ("example", "0001_initial"),
    ]

    operations = [
        migrations.RunPython(get_all_cats, delete_all_cats),
    ]
