vcinema/wiki_pages/HiddenThemes.py

70 lines
2.4 KiB
Python
Raw Normal View History

2022-04-08 22:45:29 +01:00
from collections import OrderedDict
2022-04-16 09:43:51 +01:00
from bookstack import Bookstack
2022-04-08 22:45:29 +01:00
from vcinema_utils import VCinemaUtils
# Page ID of https://wiki.jacknet.io/books/vcinema/page/films-by-reference
2022-04-15 17:29:19 +01:00
PAGE_ID = 63
2022-04-08 22:45:29 +01:00
def get_hidden_themes(viewings, token_id, token_secret):
# Bit horrible to need to request this again, but it affects the order of the result table
viewings_ungrouped = VCinemaUtils.get_vcinema_viewings(token_id, token_secret, combine_repeat_viewings=False)
# Copy keywords from grouped viewings to ungrouped viewings
for viewing_ungrouped in viewings_ungrouped:
for viewing in viewings:
if viewing['imdb_id'] == viewing_ungrouped['imdb_id']:
if 'keywords' in viewing:
viewing_ungrouped['keywords'] = viewing['keywords']
break
viewings_filtered_watch_date = VCinemaUtils.filter_viewings(viewings_ungrouped, "date_watched")
for date, viewings in viewings_filtered_watch_date.items():
viewing_dict = {"viewings": viewings}
viewings_filtered_watch_date[date] = viewing_dict
# Add hidden themes
for date, data in viewings_filtered_watch_date.items():
2022-06-27 00:21:07 +01:00
hidden_themes = set()
2022-04-08 22:45:29 +01:00
if len(data['viewings']) > 1:
2022-06-27 00:21:07 +01:00
viewings_keywords = []
2022-04-08 22:45:29 +01:00
for viewing in data['viewings']:
if 'keywords' in viewing:
2022-06-27 00:21:07 +01:00
viewings_keywords.append(set(viewing['keywords']))
2022-04-08 22:45:29 +01:00
2022-06-27 00:21:07 +01:00
if len(viewings_keywords) > 1:
hidden_themes = set.intersection(*viewings_keywords)
2022-04-08 22:45:29 +01:00
2022-06-27 00:21:07 +01:00
if hidden_themes != set():
viewings_filtered_watch_date[date]['hidden_themes'] = list(hidden_themes)
2022-04-08 22:45:29 +01:00
return viewings_filtered_watch_date
2022-04-16 09:43:51 +01:00
def update_page(token_id, token_secret, hidden_themes):
page = build_page(hidden_themes)
2022-06-27 00:21:07 +01:00
Bookstack.update_page(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, PAGE_ID, markdown=page)
2022-04-16 09:43:51 +01:00
2022-04-08 22:45:29 +01:00
def build_page(hidden_themes):
hidden_themes = OrderedDict(sorted(hidden_themes.items(), key=lambda t: t[0]))
table = "| Date | Films | Hidden Themes |\n| - | - | - |"
for date, data in hidden_themes.items():
table += "\n"
row_data = []
row_data.append(str(date))
row_data.append(VCinemaUtils.get_film_list(data['viewings']))
row_data.append(("<br>".join(k for k in sorted(data['hidden_themes']))) if 'hidden_themes' in data else "N/A")
2022-04-08 22:45:29 +01:00
table += " | ".join(row_data)
return table