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

from events.models import Event as DSEvent, Organization
from digitools import getBrowser, createDashURL, createBasicEvent, getSource

venue, created = Organization.objects.get_or_create(
     name="Comedy Corner",
     city="Minneapolis",
     website="https://comedycornerunderground.com/calendar",
    )

calendar_url = "https://comedycornerunderground.com/calendar"

DATETIME_FORMAT = '%m %d %I%p %Y'

def get_events(ps, link):
    contents = ps.xpath('.//*/td')
    for c in contents:
        try:
            day_num = c.xpath('.//*/div[@class="marker-daynum"]/text()')[0]
            events = c.xpath('.//*/li')
            # print(events)
            for e in events:
                event = {}
                print(link)
                month = link.split("month=")[1].split("-")[0]
                year = link.split("month=")[1].split("-")[1]
                event['title'] = e.xpath('.//*/span[@class="item-title"]/text()')[0]
                event['time'] = e.xpath('.//*/span[@class="item-time item-time--12hr"]/text()')[0].replace("\xa0", "")
                event['link'] = "https://comedycornerunderground.com" + e.xpath('.//a/@href')[0]
                event['date'] = str(day_num) + ' ' + event['time'] + 'm'
                dateStamp = month + ' ' + event['date'] + ' ' + year
                event['dateStamp'] = datetime.strptime(dateStamp, DATETIME_FORMAT)
                createBasicEvent(event, 'Co')
                print('\n\n++\n\n')
        except Exception as e:
            continue

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

links = createDashURL("https://comedycornerunderground.com/calendar?view=calendar&month=")

for link in links:
    ps = getSource(br, link)
    get_events(ps, link)
    sleep(5)

# ppr(events)
br.close()