Finish migration task
This commit is contained in:
parent
62bb736efb
commit
f88e440836
3 changed files with 35 additions and 9 deletions
|
@ -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()
|
|
@ -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")
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue