#!/usr/bin/env python3
from bs4 import BeautifulSoup
import datetime
import http.client
import random
import re
from typing import Dict, List
import file_writer
import grab_bag
import page_builder
import profanity_filter
import renderer
import renderer_catalog
class stranger_events_renderer(renderer.debuggable_abstaining_renderer):
def __init__(self, name_to_timeout_dict: Dict[str, int]):
super(stranger_events_renderer, self).__init__(name_to_timeout_dict, True)
self.feed_site = "everout.com"
self.events = grab_bag.grab_bag()
def debug_prefix(self) -> str:
return "stranger"
def periodic_render(self, key: str) -> bool:
self.debug_print("called for action %s" % key)
if key == "Fetch Events":
return self.fetch_events()
elif key == "Shuffle Events":
return self.shuffle_events()
else:
raise error("Unknown operaiton")
def get_style(self):
return """
"""
def shuffle_events(self) -> bool:
layout = page_builder.page_builder()
layout.set_layout(page_builder.page_builder.LAYOUT_FOUR_ITEMS)
layout.set_title("Stranger Events")
layout.set_style(self.get_style())
subset = self.events.subset(4)
if subset is None:
self.debug_print("Not enough events to build page.")
return False
for msg in subset:
layout.add_item(msg)
with file_writer.file_writer("stranger-events_2_36000.html") as f:
layout.render_html(f)
return True
def fetch_events(self) -> bool:
self.events.clear()
feed_uris = [
"/stranger-seattle/events/?page=1",
"/stranger-seattle/events/?page=2",
"/stranger-seattle/events/?page=3",
]
now = datetime.datetime.now()
ts = now + datetime.timedelta(1)
tomorrow = datetime.datetime.strftime(ts, "%Y-%m-%d")
feed_uris.append(f"/stranger-seattle/events/?start-date={tomorrow}")
delta = 5 - now.weekday()
if delta <= 0:
delta += 7
if delta > 1:
ts = now + datetime.timedelta(delta)
next_sat = datetime.datetime.strftime(ts, "%Y-%m-%d")
feed_uris.append(f"/stranger-seattle/events/?start-date={next_sat}&page=1")
feed_uris.append(f"/stranger-seattle/events/?start-date={next_sat}&page=2")
delta += 1
if delta > 1:
ts = now + datetime.timedelta(delta)
next_sun = datetime.datetime.strftime(ts, "%Y-%m-%d")
feed_uris.append(f"/stranger-seattle/events/?start-date={next_sun}&page=1")
feed_uris.append(f"/stranger-seattle/events/?start-date={next_sun}&page=2")
filter = profanity_filter.profanity_filter()
for uri in feed_uris:
try:
self.debug_print("fetching 'https://%s%s'" % (self.feed_site, uri))
self.conn = http.client.HTTPSConnection(self.feed_site)
self.conn.request("GET", uri, None, {"Accept-Charset": "utf-8"})
response = self.conn.getresponse()
if response.status != 200:
self.debug_print("Connection failed, status %d" % (response.status))
self.debug_print(response.getheaders())
continue
raw = response.read()
except:
self.debug_print("Exception talking to the stranger, ignoring.")
continue
soup = BeautifulSoup(raw, "html.parser")
for x in soup.find_all("div", class_="row event list-item mb-3 py-3"):
text = x.get_text()
if filter.contains_bad_words(text):
continue
raw = str(x)
raw = raw.replace(
'src="/', 'align="left" src="https://www.thestranger.com/'
)
raw = raw.replace('href="/', 'href="https://www.thestranger.com/')
raw = raw.replace("FREE", "Free")
raw = raw.replace("Save Event", "")
raw = re.sub("^\s*$", "", raw, 0, re.MULTILINE)
raw = re.sub(
']*class="calendar-post-ticket"[^<>]*>.*#span>',
"",
raw,
0,
re.DOTALL | re.IGNORECASE,
)
self.events.add(raw)
self.debug_print(f"fetched {self.events.size()} events so far.")
return self.events.size() > 0
# Test
# x = stranger_events_renderer({"Test", 123})
# x.periodic_render("Fetch Events")
# x.periodic_render("Shuffle Events")