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
from events.digitools import getBrowser, createURL, createBasicArticle, getSource

scraper, created = Scraper.objects.get_or_create(
     name="Racket MN",
     website="https://racketmn.com",
     last_ran = datetime.now(),
    )

org, created = Organization.objects.get_or_create(
     name="Racket MN",
     city="Minneapolis",
     website="https://racketmn.com",
     is_venue=False,
    )

event_type = "Ja"

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

def get_events(ps, event_type):
    count = 0
    contents = ps.xpath('.//*/div[@class="PostCard_stackedWrapper__S21Fy"]') + ps.xpath('.//*/div[@class="PostCard_wrapper__uteO3"]')
    for c in contents:
        if count > 10:
                br.close()
                quit()
        try:
            article = {}
            article['title'] = c.xpath('.//div/a/h3/text()')[0]
            article['link'] = org.website + c.xpath('.//div/a/@href')[1]
            createBasicArticle(article, event_type, org)
            count+=1
        except Exception as e:
            print(e)
            ppr(article)
            print("\n+++\n")

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


ps = getSource(br, org.website)
get_events(ps, "Ed")
sleep(3)

br.close()
scraper.save()