import requests, os, sys
from icalendar import Calendar as iCalendar, Event

from pprint import pprint as ppr
import pytz

import django
sys.path.append('../../../')
os.environ['DJANGO_SETTINGS_MODULE'] = 'ds_events.settings'
django.setup()

from events.models import Event as DSEvent, Organization, Scraper, Calendar

import events.digitools as digitools

from datetime import datetime
from dateutil import relativedelta
td = relativedelta.relativedelta(hours=5)

venue, created = Organization.objects.get_or_create(
     name="Bunkers",
     city="Minneapolis",
     website="https://bunkersmusic.com/calendar/",
     is_venue = True
    )

try:
    scraper, created = Scraper.objects.get_or_create(
        name="Bunkers",
        website="https://calendar.google.com/calendar/ical/js94epu90r2et31aopons1ifm8%40group.calendar.google.com/public/basic.ics",
        calendar = Calendar.objects.get(id=1),
        items = 0,
        new_items = 0,
        last_ran = datetime.now(),
        )
except Exception as e:
    print(e)
    scraper = Scraper.objects.get(name=venue.name)

item_count_start = scraper.items

event_type = "Mu"

objIcalData = requests.get(scraper.website)
gcal = iCalendar.from_ical(objIcalData.text)
tz = pytz.timezone("US/Central")
digitools.getiCalEvents(gcal, scraper, venue)
digitools.updateScraper(scraper, item_count_start)
