import requests, os, sys
from icalendar import Calendar as iCalendar, Event
from datetime import datetime
from dateutil import relativedelta
td = relativedelta.relativedelta(hours=5)

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

td = relativedelta.relativedelta(hours=5)
odt = datetime.now() + td

venue, created = Organization.objects.get_or_create(
     name="Terminal Bar",
     city="Minneapolis",
     website="https://terminalbarmn.com",
    )
event_type = "Mu"

try:
    scraper, created = Scraper.objects.get_or_create(
        name="Terminal Bar",
        website="https://calendar.google.com/calendar/ical/terminalbar32%40gmail.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)