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="Uniocorn Riot",
     website="https://unicornriot.ninja/",
     last_ran = datetime.now(),
    )

org, created = Organization.objects.get_or_create(
     name="Uniocorn Riot",
     city="Minneapolis",
     website="https://unicornriot.ninja/",
     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):
    contents = ps.xpath('.//*/article')
    for c in contents[:10]:
        try:
            article = {}
            article['title'] = c.xpath('.//*/h3[@class="title entry-title is-3"]/a/text()')[0]
            article['link'] = c.xpath('.//*/h3[@class="title entry-title is-3"]/a/@href')[0]
            createBasicArticle(article, event_type, org)
        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()