Commit c48634b3 authored by Gijs Hendriksen's avatar Gijs Hendriksen
Browse files

Failed project, rewriting to single-use script

parent 3eb067df
from django.utils import timezone
from django.utils.timezone import datetime
from utils.management.commands import legacylogin
from events.models import Event
from bs4 import BeautifulSoup
import requests
import os
import re
class Command(legacylogin.Command):
help = "Scrapes the events from the old Thalia website"
def handle(self, *args, **options):
super().handle(*args, **options)
if Event.objects.count() != 0:
print("This command should only be executed when there are no events yet.")
return
url = "https://thalia.nu/events/admin/activity"
documentpage = self.session.get(url)
soup = BeautifulSoup(documentpage.text, 'lxml')
for form in soup.find_all('form'):
eventid = int(form.input['value'])
eventurl = "https://thalia.nu/events/admin/activity/edit/{}".format(eventid)
soup = BeautifulSoup(self.session.get(eventurl).text, 'lxml')
event = Event()
event.id = eventid
event.title = soup.find(id='title')['value']
event.description = soup.find(id='description').text
times = [int(el['value']) for el in soup.find_all(selected=True)]
# TODO: Make this better!
dates = {}
for date_type in ('begin_registration_dt',
'end_registration_dt',
'end_date_dt',
'start_date_dt',
'end_cancel_dt'):
script_tag = soup.find('script', text=lambda match: date_type in match)
if script_tag:
# TODO: Please dont hate me for this regex
match = re.search(r'{}[\s\S]+new Date\((\d{{4}}), (\d{{1,2}}) - 1, (\d{{1,2}})\)\)'.format(date_type), script_tag.text)
year = match.group(1)
month = match.group(2)
day = match.group(3)
dates[date_type] = '{}-{}-{}'.format(year, month, day)
event.start = '{} {}:{}'.format(dates['start_date_dt'], times.pop(0), times.pop(0))
event.end = '{} {}:{}'.format(dates['end_date_dt'], times.pop(0), times.pop(0))
event.registration_start = '{} {}:{}'.format(dates['begin_registration_dt'], times.pop(0), times.pop(0))
event.registration_end = '{} {}:{}'.format(dates[''], times.pop(0), times.pop(0))
canceldate = soup.find(id='end_cancel_dt')
event.cancel_deadline = '{} {}:{}'.format(canceldate, times.pop(0), times.pop(0))
event.location = soup.find(id='location')['value']
event.price = float(soup.find(id='member_price')['value'])
event.cost = float(soup.find(id='thalia_costs')['value'])
event.max_participants = int(soup.find(id='registration_limit')['value'])
event.published = form.a.span.text == 'Ja'
event.save()
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-08-31 19:39
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('events', '0003_remove_event_registration_required'),
]
operations = [
migrations.AddField(
model_name='event',
name='cancel_deadline',
field=models.DateTimeField(blank=True, null=True, verbose_name='cancel deadline'),
),
]
......@@ -37,6 +37,12 @@ class Event(models.Model):
blank=True
)
cancel_deadline = models.DateTimeField(
_("cancel deadline"),
null=True,
blank=True
)
location = models.CharField(_("location"), max_length=255)
map_location = models.CharField(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment