from collections import OrderedDict import wikipedia from vcinema_utils import VCinemaUtils # Page ID of https://wiki.jacknet.io/books/vcinema/page/references PAGE_ID = 62 def get_films_by_reference(viewings): viewings_filtered_by_keyword = VCinemaUtils.filter_viewings(viewings, "keywords") viewings_filtered_by_reference_keyword = {k: v for k, v in viewings_filtered_by_keyword.items() if k.startswith("reference-to")} return viewings_filtered_by_reference_keyword 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) def build_page(films_by_reference_keyword): reference_keywords_sorted = OrderedDict(sorted(films_by_reference_keyword.items(), key=lambda t: t[0])) table = "| Referenced | Films |\n| - | - |" for reference_keyword in reference_keywords_sorted.keys(): table += "\n" row_data = [] referenced = reference_keyword[13:].replace("-", " ") if referenced.startswith("a "): referenced = referenced[2:] else: referenced = referenced try: searches = wikipedia.search(referenced, suggestion=False) referenced_page = wikipedia.page(title=referenced, auto_suggest=False) row_data.append("[{}]({}) ()".format(referenced_page.title, referenced_page.url, reference_keyword)) except wikipedia.DisambiguationError as e1: row_data.append("[{}](https://en.wikipedia.org/wiki/{})) ()".format(e1.title, e1.title.replace(" ", "_"), reference_keyword)) except wikipedia.PageError as _: try: referenced_page = wikipedia.page(title=searches[0], auto_suggest=False) row_data.append("[{}]({}) ()".format(referenced_page.title, referenced_page.url, reference_keyword)) except wikipedia.DisambiguationError as e2: row_data.append( "[{}](https://en.wikipedia.org/wiki/{})) ()".format(e2.title, e2.title.replace(" ", "_"),reference_keyword)) row_data.append(VCinemaUtils.get_film_list(reference_keywords_sorted[reference_keyword])) table += " | ".join(row_data) return table