Merge pull request 'feature/improve-references-page' (#10) from feature/improve-references-page into master

Reviewed-on: #10
This commit is contained in:
Sarah 2022-12-09 21:17:40 +00:00
commit 64041803a6
2 changed files with 67 additions and 11 deletions

View File

@ -135,8 +135,16 @@ def generate_markdown_link(text, url):
def generate_imdb_film_link(film):
return generate_markdown_link(film['title'], "https://www.imdb.com/title/tt{}/".format(film['imdb_id']))
return generate_markdown_link(film['title'], generate_imdb_url(film['imdb_id']))
def generate_wikipedia_page_link(page_title):
return generate_markdown_link(page_title, "https://en.wikipedia.org/wiki/{}".format(page_title.replace(" ", "_")))
return generate_markdown_link(page_title, generate_wikipedia_url(page_title))
def generate_imdb_url(imdb_id):
return "https://www.imdb.com/title/tt{}/".format(imdb_id)
def generate_wikipedia_url(page_title):
"https://en.wikipedia.org/wiki/{}".format(page_title.replace(" ", "_"))

View File

@ -1,5 +1,5 @@
from collections import OrderedDict
import string
import wikipedia
from bookstack import Bookstack
from vcinema_utils import VCinemaUtils
@ -9,10 +9,54 @@ 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")}
films_by_reference = {}
return viewings_filtered_by_reference_keyword
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:
keyword = keyword[13:]
if keyword.startswith("a-"):
keyword = keyword[2:]
referenced = keyword.replace("-", " ")
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
except wikipedia.DisambiguationError as e:
page_title = e.title
page_url = VCinemaUtils.generate_wikipedia_url(page_title)
except wikipedia.PageError as _:
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_url = VCinemaUtils.generate_wikipedia_url(page_title)
if page_title in films_by_reference.keys():
films_by_reference[page_title]["films"].append(viewing)
films_by_reference[page_title]["keywords"].append(keyword)
else:
films_by_reference[page_title] = {"url": page_url,
"keywords": [keyword],
"films": [viewing]}
return films_by_reference
def update_page(token_id, token_secret, films_by_reference_keyword):
@ -20,17 +64,21 @@ def update_page(token_id, token_secret, 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]))
def build_page(films_by_reference):
films_by_reference = OrderedDict(sorted(films_by_reference.items(), key=lambda t: t[0]))
table = "| Referenced | Films |\n| - | - |"
for year in reference_keywords_sorted.keys():
for reference, referenced in films_by_reference.items():
table += "\n"
row_data = []
row_data.append(VCinemaUtils.generate_wikipedia_page_link(string.capwords(year[13:].replace("-", " "))))
row_data.append(VCinemaUtils.get_film_list(reference_keywords_sorted[year]))
reference_url = referenced["url"]
referenced_films = referenced["films"]
row_data.append(VCinemaUtils.generate_markdown_link(reference, reference_url))
row_data.append(VCinemaUtils.get_film_list(referenced_films))
table += " | ".join(row_data)