import re, os, sys
from datetime import datetime

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

from events.models import Event, Organization

from pprint import pprint as ppr

from time import sleep
from pprint import pprint as ppr
from selenium import webdriver as wd
from selenium.webdriver.common.by import By

from xvfbwrapper import Xvfb
from lxml import html
import pytz

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

DATETIME_FORMAT = '%a, %b %d %Y'
calendar_url = "https://www.facebook.com/mortimersmpls/events/"
current_year = str(datetime.now().year)

# Initiate and start the Browser
br = wd.Firefox()



br.get(calendar_url)
sleep(10)
br.find_element(By.XPATH, '//*/div[@class="x1i10hfl xjbqb8w x6umtig x1b1mbwd xaqea5y xav7gou x1ypdohk xe8uvvx xdj266r x11i5rnm xat24cr x1mh8g0r xexx8yu x4uap5 x18d9i69 xkhd6sd x16tdsg8 x1hl2dhg xggy1nq x1o1ewxj x3x9cwd x1e5q0jg x13rtm0m x87ps6o x1lku1pv x1a2a7pz x9f619 x3nfvp2 xdt5ytf xl56j7k x1n2onr6 xh8yej3"]').click()
print("Input Login Info")
sleep(30)

ps = html.fromstring(br.page_source)

listings = ps.xpath('.//*/div[@class="x9f619 x1n2onr6 x1ja2u2z x78zum5 x2lah0s x1qughib x6s0dn4 xozqiw3 x1q0g3np x1pi30zi x1swvt13 xsag5q8 xz9dl7a x1n0m28w xp7jhwk x1wsgfga x9otpla"]')

for l in listings:
    gT = l.xpath('.//*/span/text()')
    dateTime = gT[0]
    show_title = gT[1]
    link = l.xpath('.//*/a/@href')[0].split("?")[0] + " " + current_year
    print(show_title, dateTime, link)
    venue, created = Organization.objects.get_or_create(name="Mortimer's")
    try:
        new_event = Event.objects.update_or_create(
            event_type = 'Mu',
            show_title = show_title,
            show_link = link,
            show_date = datetime.strptime(dateTime.split(" AT")[0].strip(), DATETIME_FORMAT),
            venue = venue
        )
    except Exception as e:
        print(e, "\n\n++++\n\n")
        continue


br.close()