From f88e440836aebd95c327d98bcd7ba9e6fd2883a6 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 5 Dec 2020 13:15:30 +0100 Subject: [PATCH] Finish migration task --- .../commands/import_sixxs_prefixes.py | 40 +++++++++++++++---- ipv6ula/models.py | 2 +- ipv6ula/views.py | 2 +- 3 files changed, 35 insertions(+), 9 deletions(-) rename import_sixxs_prefixes.py => ipv6ula/management/commands/import_sixxs_prefixes.py (66%) diff --git a/import_sixxs_prefixes.py b/ipv6ula/management/commands/import_sixxs_prefixes.py similarity index 66% rename from import_sixxs_prefixes.py rename to ipv6ula/management/commands/import_sixxs_prefixes.py index 8126ac3..33d9ad1 100644 --- a/import_sixxs_prefixes.py +++ b/ipv6ula/management/commands/import_sixxs_prefixes.py @@ -1,7 +1,16 @@ +# Nico Schottelius, 2020-12-05, GPLv3+ +# Developed at Hack4Glarus 2020 Winter - https://hack4glarus.ch + from html.parser import HTMLParser import urllib.request +import re + +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model +from ipv6ula.models import ULA + +url = "https://www.sixxs.net/tools/grh/ula/list/" -# https://www.sixxs.net/tools/grh/ula/list/ class MyHTMLParser(HTMLParser): def __init__(self): @@ -22,8 +31,6 @@ class MyHTMLParser(HTMLParser): self.website = False def handle_starttag(self, tag, attrs): -# print(f"Encountered a start tag: '{tag}'") - if tag == "th": self.th = True @@ -44,7 +51,6 @@ class MyHTMLParser(HTMLParser): elif self.col_index == 4 and tag == "a": self.website = True - def handle_endtag(self, tag): if tag == "th": self.th = False @@ -54,7 +60,6 @@ class MyHTMLParser(HTMLParser): def handle_data(self, data): if self.th and data == "Prefix": - print("Found table start") self.in_table = True if self.prefix: @@ -69,6 +74,8 @@ class MyHTMLParser(HTMLParser): self.record['name'] = data self.name = False elif self.org: + if data == '\xa0': + data = '' self.record['organization'] = data self.org = False elif self.website: @@ -79,12 +86,31 @@ class MyHTMLParser(HTMLParser): for record in self.results: print(record) +class Command(BaseCommand): + help = "Import prefixes registered on sixxs" + + def handle(self, *args, **options): + default_owner = get_user_model().objects.get(username='admin') + + parser = MyHTMLParser() + response = urllib.request.urlopen(url) + html = "\n".join([ line.decode('utf-8') for line in response.readlines() ]) + + parser.feed(html) + + for record in parser.results: + db_prefix=re.search("(.*)/", record['prefix']).groups()[0] # strip away /48 + + record['owner'] = default_owner + del record['prefix'] + + ULA.objects.get_or_create(prefix=db_prefix, defaults = record) + + if __name__ == '__main__': - url = "https://www.sixxs.net/tools/grh/ula/list/" parser = MyHTMLParser() response = urllib.request.urlopen(url) html = "\n".join([ line.decode('utf-8') for line in response.readlines() ]) parser.feed(html) - parser.report() diff --git a/ipv6ula/models.py b/ipv6ula/models.py index 252af72..7a005ff 100644 --- a/ipv6ula/models.py +++ b/ipv6ula/models.py @@ -4,8 +4,8 @@ from django.db import models from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ - import ipaddress +import re def validate_ula_prefix(prefix): ula_net = ipaddress.IPv6Network("fd00::/8") diff --git a/ipv6ula/views.py b/ipv6ula/views.py index d4e874f..2c38d62 100644 --- a/ipv6ula/views.py +++ b/ipv6ula/views.py @@ -15,7 +15,7 @@ from .forms import ULAForm class IndexView(ListView): model = ULA - paginate_by = 50 +# paginate_by = 50 queryset = ULA.objects.order_by('prefix') class SubmitView(LoginRequiredMixin, CreateView):