get references before generating table

This commit is contained in:
Sarah 2022-12-04 12:16:26 +00:00
parent e89eb33230
commit 9de7ec31c2
1 changed files with 37 additions and 12 deletions

View File

@ -1,6 +1,7 @@
from collections import OrderedDict
import wikipedia
from bookstack import Bookstack
from vcinema_utils import VCinemaUtils
# Page ID of https://wiki.jacknet.io/books/vcinema/page/references
@ -20,14 +21,11 @@ def update_page(token_id, token_secret, films_by_reference_keyword):
def build_page(films_by_reference_keyword):
reference_keywords_sorted = OrderedDict(sorted(films_by_reference_keyword.items(), key=lambda t: t[0]))
references = {}
table = "| Referenced | Films |\n| - | - |"
for reference_keyword in films_by_reference_keyword:
for reference_keyword in reference_keywords_sorted.keys():
table += "\n"
row_data = []
print(reference_keyword)
referenced = reference_keyword[13:].replace("-", " ")
@ -39,17 +37,44 @@ def build_page(films_by_reference_keyword):
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))
page_title = referenced_page.title
page_url = referenced_page.url
except wikipedia.DisambiguationError as e1:
row_data.append("[{}](https://en.wikipedia.org/wiki/{})) ()".format(e1.title, e1.title.replace(" ", "_"), reference_keyword))
page_title = e1.title
page_url = "https://en.wikipedia.org/wiki/{}".format(e1.title.replace(" ", "_"))
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))
page_title = referenced_page.title
page_url = referenced_page.url
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]))
page_title = e2.title
page_url = "https://en.wikipedia.org/wiki/{}".format(e2.title.replace(" ", "_"))
if page_title in references.keys():
references[page_title]["films"].extend(films_by_reference_keyword[reference_keyword])
else:
references[page_title] = {"url": page_url,
"films": films_by_reference_keyword[reference_keyword]}
references_sorted = OrderedDict(sorted(references.items(), key=lambda t: t[0]))
table = "| Referenced | Films |\n| - | - |"
for reference, referenced in references_sorted.items():
table += "\n"
row_data = []
reference_url = referenced["url"]
reference_title = reference
referenced_films = referenced["films"]
row_data.append("[{}]({})".format(reference_title, reference_url))
row_data.append(VCinemaUtils.get_film_list(referenced_films))
table += " | ".join(row_data)