# Path: unicodex/models.py
# class Codepoint(models.Model):
#     name = models.CharField(max_length=100)
#     description = models.TextField(blank=True, help_text="Totally optional, tbh.")
#     codepoint = models.CharField(
#         max_length=10,
#         help_text="U+xxxx; tell me X, or I can work it out myself",
#         blank=True,
#     )
#     emojipedia_name = models.CharField(
#         max_length=50,
#         help_text="If the name of this codepoint is strange on Emojipedia, add it here. Otherwise, I'll work it out myself.",
#         blank=True,
#     )
#     order = models.IntegerField(default=1)
# 
#     @property
#     def render(self):
#         return f"U{str(self.codepoint).zfill(8)}"
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def display(self):
#         return f"&#x{self.codepoint};&#xFE0F;"
# 
# class Design(models.Model):
#     vendorversion = models.ForeignKey(VendorVersion, on_delete=models.CASCADE)
#     codepoint = models.ForeignKey(Codepoint, on_delete=models.CASCADE)
# 
#     def design_path(instance, filename):
#         path = (
#             f"design/{slugify(instance.vendorversion.vendor.name)}"
#             f"/{slugify(instance.vendorversion.name)}/"
#             f"{slugify(instance.codepoint.name)}/{filename}"
#         )
#         print(path)
#         return path
# 
#     image = models.FileField(upload_to=design_path)
# 
#     class Meta:
#         unique_together = ("vendorversion", "codepoint")
# 
#     def __str__(self):
#         return " ".join([str(self.codepoint), str(self.vendorversion)])
# 
# class VendorVersion(models.Model):
#     name = models.CharField(max_length=100)
#     vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return "{} {}".format(self.vendor, self.name)


import io
import os
import sys
import requests
from tempfile import NamedTemporaryFile
from urllib.parse import urlparse
from urllib.request import urlopen
from django.core.files.base import File
from django.core.management import call_command
from django.core.management.base import BaseCommand, CommandError
from django.utils.text import slugify
from bs4 import BeautifulSoup
from unicodex.models import Codepoint, Design, VendorVersion
            emojipedia_name = cp.emojipedia_name
        else:
            emojipedia_name = slugify(cp.name)

        uri = f"https://emojipedia.org/{emojipedia_name}"
        out(f"Retrieving data for {cp.name} from {uri}")

        resp = requests.get(uri)
        page = BeautifulSoup(resp.content, "html.parser")

        v = page.find_all(class_="vendor-image")

        def download_image(url):
            img_temp = NamedTemporaryFile(dir=".", delete=True)
            img_temp.write(urlopen(url).read())
            img_temp.flush()
            return img_temp

        out(f"Parsing {uri}")

        stats = {"ignored": 0, "added": 0, "skipped": 0}
        for x in v:
            if x.a:
                href = x.a["href"]
                url = x.a.img["data-src"]

                _, vendor, version, _, _ = href.split("/")
                version = version.replace("-", " ")

                try:

# Path: unicodex/models.py
# class Codepoint(models.Model):
#     name = models.CharField(max_length=100)
#     description = models.TextField(blank=True, help_text="Totally optional, tbh.")
#     codepoint = models.CharField(
#         max_length=10,
#         help_text="U+xxxx; tell me X, or I can work it out myself",
#         blank=True,
#     )
#     emojipedia_name = models.CharField(
#         max_length=50,
#         help_text="If the name of this codepoint is strange on Emojipedia, add it here. Otherwise, I'll work it out myself.",
#         blank=True,
#     )
#     order = models.IntegerField(default=1)
# 
#     @property
#     def render(self):
#         return f"U{str(self.codepoint).zfill(8)}"
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def display(self):
#         return f"&#x{self.codepoint};&#xFE0F;"
# 
# class Design(models.Model):
#     vendorversion = models.ForeignKey(VendorVersion, on_delete=models.CASCADE)
#     codepoint = models.ForeignKey(Codepoint, on_delete=models.CASCADE)
# 
#     def design_path(instance, filename):
#         path = (
#             f"design/{slugify(instance.vendorversion.vendor.name)}"
#             f"/{slugify(instance.vendorversion.name)}/"
#             f"{slugify(instance.codepoint.name)}/{filename}"
#         )
#         print(path)
#         return path
# 
#     image = models.FileField(upload_to=design_path)
# 
#     class Meta:
#         unique_together = ("vendorversion", "codepoint")
# 
#     def __str__(self):
#         return " ".join([str(self.codepoint), str(self.vendorversion)])
# 
# class VendorVersion(models.Model):
#     name = models.CharField(max_length=100)
#     vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE)
# 
#     def __str__(self):
#         return "{} {}".format(self.vendor, self.name)


# Path: unicodex/management/commands/import_from_vendor.py
import io
import os
import sys
import requests
from tempfile import NamedTemporaryFile
from urllib.parse import urlparse
from urllib.request import urlopen
from django.core.files.base import File
from django.core.management import call_command
from django.core.management.base import BaseCommand, CommandError
from django.utils.text import slugify
from bs4 import BeautifulSoup
from unicodex.models import Codepoint, Design, VendorVersion

            emojipedia_name = cp.emojipedia_name
        else:
            emojipedia_name = slugify(cp.name)

        uri = f"https://emojipedia.org/{emojipedia_name}"
        out(f"Retrieving data for {cp.name} from {uri}")

        resp = requests.get(uri)
        page = BeautifulSoup(resp.content, "html.parser")

        v = page.find_all(class_="vendor-image")

        def download_image(url):
            img_temp = NamedTemporaryFile(dir=".", delete=True)
            img_temp.write(urlopen(url).read())
            img_temp.flush()
            return img_temp

        out(f"Parsing {uri}")

        stats = {"ignored": 0, "added": 0, "skipped": 0}
        for x in v:
            if x.a:
                href = x.a["href"]
                url = x.a.img["data-src"]

                _, vendor, version, _, _ = href.split("/")
                version = version.replace("-", " ")

                try:

                    vv = VendorVersion.objects.get(