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

venue, created = Organization.objects.get_or_create(
     name="Ginkgo Coffee",
     city="Saint Paul",
     website="https://ginkgocoffee.com/events/",
     is_venue = True
    )

scraper,item_count_start = digitools.getScraper(venue)

event_type = ""

# Time Signatures
tz = pytz.timezone("US/Central")
DATETIME_FORMAT_2 = '%b %d %Y %I:%M %p'
DATETIME_FORMAT = '%B %d @ %I:%M %p %Y'

def get_events(ps, event_type):
    contents = ps.xpath('.//*/article')
    for c in contents:
        try:
            event = {}
            dateTime = c.xpath('.//*/span[@class="tribe-event-date-start"]/text()')[0]
            month = c.xpath('.//*/span[@class="tribe-event-date-start"]/text()')[0].split(' ')[0]
            year = datetime.now().year
            if month == "January":
                year = int(year) + 1
            event['scraper'] = scraper
            event['calendar'] = scraper.calendar
            event['title'] = c.xpath('.//*/h3/a/text()')[0].replace("\n", "").replace("\t", "")
            event['date'] = " ".join([ dateTime, str(year)])
            event['dateStamp'] = datetime.strptime(event['date'], DATETIME_FORMAT)
            event['link'] = c.xpath('.//*/h3/a/@href')[0]
            digitools.createBasicEvent(event, event_type, venue)
        except Exception as e:
            print(e)
            ppr(event)
            print("\n\n+++\n\n")

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

ps = digitools.getSource(br, venue.website)
get_events(ps, "Mu")
sleep(3)

br.close()

digitools.updateScraper(scraper, item_count_start)