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

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 dateutil import relativedelta
td = relativedelta.relativedelta(hours=5)

venue, created = Organization.objects.get_or_create(
     name="KJ's Hideaway",
     city="Minneapolis",
     website="",
    )
    
try:
    scraper, created = Scraper.objects.get_or_create(
        name="KJ's Hideaway",
        website="https://calendar.google.com/calendar/ical/sgmok5t13vspeoruhruh33dhj0hgc50q%40import.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)