import os, sys
from datetime import datetime
from dateutil import relativedelta

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

from time import sleep
from pprint import pprint as ppr
import pytz

from events.models import Organization, Scraper
import events.digitools as digitools

current_year = str(datetime.now().year)

venue, created = Organization.objects.get_or_create(
     name="Eastside Freedom Library",
     city="Minneapolis",
     website="https://eastsidefreedomlibrary.org/events/",
     is_venue=True
    )

scraper,item_count_start = digitools.getScraper(venue)

tz = pytz.timezone("US/Central")

DATETIME_FORMAT = '%B %d @ %I:%M %p %Y'

def get_events(ps):
    contents = ps.xpath('.//*/article')
    # ppr("contents:", contents)
    for c in contents:
        try:
            event = {}
            event['scraper'] = scraper
            event['calendar'] = scraper.calendar
            event['title'] = c.xpath('.//*/h3[@class="tribe-events-calendar-list__event-title tribe-common-h6 tribe-common-h4--min-medium"]/a/text()')[0].strip()
            event['link'] = c.xpath('.//*/h3[@class="tribe-events-calendar-list__event-title tribe-common-h6 tribe-common-h4--min-medium"]/a/@href')[0]
            event['date'] = c.xpath('.//*/span[@class="tribe-event-date-start"]/text()')[0].strip() + " " + current_year
            event['dateStamp'] =datetime.strptime(event['date'], DATETIME_FORMAT)
            try:
                new_event = digitools.createBasicEvent(event, "Ed", venue)
                scraper.items+=1
            except Exception as e:
                print(e)
                ppr(event)
                print("\n+++\n")
        except Exception as e:
            print(e)

if len(sys.argv) >= 2:
    arg1 = sys.argv[1]
    br = digitools.getBrowser(arg1)
else:
    print("No run_env")
    quit()

calendar_url = 'https://eastsidefreedomlibrary.org/events/'

ps = digitools.getSource(br, calendar_url)

get_events(ps)

# ppr(events)
br.close()

digitools.updateScraper(scraper, item_count_start)