2023-10-15 16:22:52 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
from selenium import webdriver
|
|
|
|
from selenium.webdriver.chrome.service import Service
|
|
|
|
from selenium.webdriver.chrome.options import Options
|
|
|
|
|
2023-11-11 16:55:04 +01:00
|
|
|
|
2023-10-15 16:22:52 +02:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
2023-11-11 16:55:04 +01:00
|
|
|
class SimpleEvent:
|
|
|
|
|
|
|
|
def __init__(self, data):
|
|
|
|
self.elements = {}
|
|
|
|
|
|
|
|
for key in ["id", "start_timestamp", "end_timestamp"]:
|
|
|
|
self.elements[key] = data[key] if key in data else None
|
|
|
|
|
|
|
|
if "parent_event" in data:
|
|
|
|
self.parent = SimpleEvent(data["parent_event"])
|
|
|
|
|
|
|
|
|
2023-10-15 16:22:52 +02:00
|
|
|
class Event:
|
|
|
|
|
|
|
|
name = "event"
|
2023-11-11 16:55:04 +01:00
|
|
|
keys = [
|
|
|
|
["start_time_formatted", 'start_timestamp',
|
|
|
|
'is_past',
|
|
|
|
"name",
|
|
|
|
"price_info",
|
|
|
|
"cover_media_renderer",
|
|
|
|
"event_creator",
|
|
|
|
"id",
|
|
|
|
"day_time_sentence",
|
|
|
|
"event_place",
|
|
|
|
"comet_neighboring_siblings"],
|
2023-11-11 19:34:55 +01:00
|
|
|
["event_description"],
|
|
|
|
["start_timestamp", "end_timestamp"]
|
2023-11-11 16:55:04 +01:00
|
|
|
]
|
|
|
|
rules = {
|
|
|
|
"event_description": { "description": ["text"]},
|
|
|
|
"cover_media_renderer": {"image_alt": ["cover_photo", "photo", "accessibility_caption"], "image": ["cover_photo", "photo", "full_image", "uri"]},
|
|
|
|
"event_creator": { "event_creator_name": ["name"], "event_creator_url": ["url"] },
|
|
|
|
"event_place": {"event_place_name": ["name"] }
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, i, event):
|
|
|
|
self.fragments = {}
|
|
|
|
self.elements = {}
|
|
|
|
self.neighbor_events = None
|
2023-11-11 19:34:55 +01:00
|
|
|
self.possible_end_timestamp = []
|
2023-11-11 16:55:04 +01:00
|
|
|
self.add_fragment(i, event)
|
|
|
|
|
|
|
|
def add_fragment(self, i, event):
|
|
|
|
self.fragments[i] = event
|
|
|
|
|
2023-11-11 19:34:55 +01:00
|
|
|
if Event.keys[i] == ["start_timestamp", "end_timestamp"]:
|
|
|
|
self.get_possible_end_timestamp(i, event)
|
|
|
|
else:
|
|
|
|
for k in Event.keys[i]:
|
|
|
|
if k == "comet_neighboring_siblings":
|
|
|
|
self.get_neighbor_events(event[k])
|
|
|
|
elif k in Event.rules:
|
|
|
|
for nk, rule in Event.rules[k].items():
|
|
|
|
c = event[k]
|
|
|
|
for ki in rule:
|
|
|
|
c = c[ki]
|
|
|
|
self.elements[nk] = c
|
|
|
|
else:
|
|
|
|
self.elements[k] = event[k]
|
2023-11-11 16:55:04 +01:00
|
|
|
|
2023-11-11 19:34:55 +01:00
|
|
|
def get_possible_end_timestamp(self, i, data):
|
|
|
|
self.possible_end_timestamp.append(dict((k, data[k]) for k in Event.keys[i]))
|
2023-10-15 16:22:52 +02:00
|
|
|
|
2023-11-11 16:55:04 +01:00
|
|
|
def get_neighbor_events(self, data):
|
|
|
|
self.neighbor_events = [SimpleEvent(d) for d in data]
|
2023-10-15 16:22:52 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-11-11 16:55:04 +01:00
|
|
|
return str(self.elements) + "\n Neighbors: " + ", ".join([ne.elements["id"] for ne in self.neighbor_events])
|
2023-10-15 16:22:52 +02:00
|
|
|
|
2023-11-11 16:55:04 +01:00
|
|
|
def consolidate_current_event(self):
|
2023-11-11 19:34:55 +01:00
|
|
|
if self.neighbor_events is not None and "id" in self.elements and "end_timestamp" not in self.elements:
|
2023-11-11 16:55:04 +01:00
|
|
|
id = self.elements["id"]
|
|
|
|
for ne in self.neighbor_events:
|
|
|
|
if ne.elements["id"] == id:
|
|
|
|
self.elements["end_timestamp"] = ne.elements["end_timestamp"]
|
2023-11-11 19:34:55 +01:00
|
|
|
|
|
|
|
if "end_timestamp" not in self.elements and len(self.possible_end_timestamp) != 0:
|
|
|
|
for s in self.possible_end_timestamp:
|
|
|
|
if s["start_timestamp"] == self.elements["start_timestamp"]:
|
|
|
|
self.elements["end_timestamp"] = s["end_timestamp"]
|
|
|
|
break
|
|
|
|
|
|
|
|
def find_event_fragment_in_array(array, event, first = True):
|
2023-10-15 16:22:52 +02:00
|
|
|
if isinstance(array, dict):
|
2023-11-11 16:55:04 +01:00
|
|
|
|
2023-11-11 19:34:55 +01:00
|
|
|
seen = False
|
2023-11-11 16:55:04 +01:00
|
|
|
for i, ks in enumerate(Event.keys):
|
|
|
|
if len(ks) == len([k for k in ks if k in array]):
|
2023-11-11 19:34:55 +01:00
|
|
|
seen = True
|
2023-11-11 16:55:04 +01:00
|
|
|
if event is None:
|
2023-11-11 19:34:55 +01:00
|
|
|
event = Event(i, array)
|
2023-11-11 16:55:04 +01:00
|
|
|
else:
|
|
|
|
event.add_fragment(i, array)
|
2023-11-11 19:34:55 +01:00
|
|
|
# only consider the first of Event.keys
|
|
|
|
break
|
|
|
|
if not seen:
|
|
|
|
for k in array:
|
|
|
|
event = Event.find_event_fragment_in_array(array[k], event, False)
|
2023-10-15 16:22:52 +02:00
|
|
|
elif isinstance(array, list):
|
|
|
|
for e in array:
|
2023-11-11 19:34:55 +01:00
|
|
|
event = Event.find_event_fragment_in_array(e, event, False)
|
2023-11-11 16:55:04 +01:00
|
|
|
|
2023-11-11 19:34:55 +01:00
|
|
|
if event is not None and first:
|
2023-11-11 16:55:04 +01:00
|
|
|
event.consolidate_current_event()
|
|
|
|
return event
|
2023-10-15 16:22:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
#url="https://www.facebook.com/events/ical/export/?eid=2294200007432315"
|
2023-11-11 19:34:55 +01:00
|
|
|
#url="https://www.facebook.com/events/2294199997432316/2294200007432315/"
|
|
|
|
#url="https://www.facebook.com/events/635247792092358/"
|
|
|
|
url="https://www.facebook.com/events/872781744074648"
|
|
|
|
url="https://www.facebook.com/events/1432798543943663?"
|
2023-10-15 16:22:52 +02:00
|
|
|
#url_cal = "https://www.facebook.com/events/ical/export/?eid=993406668581410"
|
|
|
|
#url="https://jmtrivial.info"
|
|
|
|
|
|
|
|
cachedir = "cache"
|
|
|
|
result = hashlib.md5(url.encode())
|
|
|
|
hash = result.hexdigest()
|
|
|
|
|
|
|
|
filename = os.path.join(cachedir, hash + ".html")
|
|
|
|
|
|
|
|
if os.path.isfile(filename):
|
2023-11-11 16:55:04 +01:00
|
|
|
# print("Use cache")
|
2023-10-15 16:22:52 +02:00
|
|
|
with open(filename) as f:
|
|
|
|
doc = "\n".join(f.readlines())
|
|
|
|
else:
|
|
|
|
print("Download page")
|
|
|
|
|
|
|
|
options = Options()
|
|
|
|
options.add_argument("--headless=new")
|
|
|
|
service = Service("/usr/bin/chromedriver")
|
|
|
|
|
|
|
|
driver = webdriver.Chrome(service=service, options=options)
|
|
|
|
driver.get(url)
|
|
|
|
doc = driver.page_source
|
|
|
|
driver.quit()
|
|
|
|
|
|
|
|
dir = os.path.dirname(filename)
|
|
|
|
if not os.path.exists(dir):
|
|
|
|
os.makedirs(dir)
|
|
|
|
with open(filename, "w") as text_file:
|
|
|
|
text_file.write(doc)
|
|
|
|
|
|
|
|
|
|
|
|
soup = BeautifulSoup(doc)
|
|
|
|
|
2023-11-11 16:55:04 +01:00
|
|
|
event = None
|
2023-10-15 16:22:52 +02:00
|
|
|
for json_script in soup.find_all('script', type="application/json"):
|
|
|
|
json_txt = json_script.get_text()
|
|
|
|
json_struct = json.loads(json_txt)
|
2023-11-11 16:55:04 +01:00
|
|
|
|
|
|
|
event = Event.find_event_fragment_in_array(json_struct, event)
|
|
|
|
|
|
|
|
print(event)
|
|
|
|
|