vcinema/wiki_pages/FilmsByReference.py

101 lines
4.0 KiB
Python
Raw Permalink Normal View History

2022-03-25 22:19:40 +00:00
from collections import OrderedDict
import wikipedia
2022-03-25 22:19:40 +00:00
2022-12-04 12:16:26 +00:00
from bookstack import Bookstack
2022-03-25 22:19:40 +00:00
from vcinema_utils import VCinemaUtils
# Page ID of https://wiki.jacknet.io/books/vcinema/page/references
2022-04-15 17:22:04 +01:00
PAGE_ID = 62
2022-03-25 22:19:40 +00:00
def get_films_by_reference(viewings):
2022-12-04 13:00:58 +00:00
films_by_reference = {}
for viewing in viewings:
if "keywords" in viewing.keys():
for keyword in viewing["keywords"]:
if keyword.startswith("reference-to-"):
for reference in films_by_reference:
if keyword in films_by_reference[reference]["keywords"]:
films_by_reference[reference]["films"].append(viewing)
break
else:
2022-12-09 20:25:19 +00:00
keyword = keyword[13:]
2022-12-04 13:00:58 +00:00
2022-12-09 20:25:19 +00:00
if keyword.startswith("a-"):
keyword = keyword[2:]
2022-12-16 19:11:25 +00:00
if keyword.endswith("-character"):
keyword = keyword[:-10]
2022-12-09 20:25:19 +00:00
referenced = keyword.replace("-", " ")
2022-12-04 13:00:58 +00:00
try:
searches = wikipedia.search(referenced, suggestion=False)
referenced_page = wikipedia.page(title=referenced, auto_suggest=False)
page_title = referenced_page.title
page_url = referenced_page.url
2022-12-04 14:18:30 +00:00
except wikipedia.DisambiguationError as e:
page_title = e.title
2022-12-10 08:51:53 +00:00
page_title = page_title[0].upper() + page_title[1:]
2022-12-04 14:18:30 +00:00
page_url = VCinemaUtils.generate_wikipedia_url(page_title)
2022-12-04 13:00:58 +00:00
except wikipedia.PageError as _:
if len(searches) > 0:
try:
referenced_page = wikipedia.page(title=searches[0], auto_suggest=False)
page_title = referenced_page.title
page_url = referenced_page.url
except wikipedia.DisambiguationError as e:
page_title = e.title
page_title = page_title[0].upper() + page_title[1:]
page_url = VCinemaUtils.generate_wikipedia_url(page_title)
else:
page_title = referenced.title()
page_url = None
2022-12-04 13:00:58 +00:00
if page_title in films_by_reference.keys():
films_by_reference[page_title]["keywords"].append(keyword)
2022-12-10 09:11:01 +00:00
if viewing not in films_by_reference[page_title]["films"]:
films_by_reference[page_title]["films"].append(viewing)
2022-12-04 13:00:58 +00:00
else:
films_by_reference[page_title] = {"url": page_url,
2022-12-04 14:20:49 +00:00
"keywords": [keyword],
"films": [viewing]}
2022-12-04 13:00:58 +00:00
return films_by_reference
2022-03-25 22:19:40 +00:00
2022-04-16 09:43:51 +01:00
def update_page(token_id, token_secret, films_by_reference_keyword):
page = build_page(films_by_reference_keyword)
Bookstack.update_page(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, PAGE_ID, markdown=page)
2022-12-04 13:00:58 +00:00
def build_page(films_by_reference):
2022-12-09 20:25:19 +00:00
films_by_reference = OrderedDict(sorted(films_by_reference.items(), key=lambda t: t[0]))
2022-12-04 12:16:26 +00:00
table = "| Referenced | Films |\n| - | - |"
2022-12-09 20:25:19 +00:00
for reference, referenced in films_by_reference.items():
2022-12-04 12:16:26 +00:00
table += "\n"
row_data = []
reference_url = referenced["url"]
referenced_films = referenced["films"]
if reference_url is None:
row_data.append(reference)
else:
row_data.append(VCinemaUtils.generate_markdown_link(reference, reference_url))
2022-12-04 12:16:26 +00:00
row_data.append(VCinemaUtils.get_film_list(referenced_films))
2022-04-08 22:01:58 +01:00
table += " | ".join(row_data)
return table