vcinema/update_hidden_themes.py

64 lines
2.2 KiB
Python
Raw Normal View History

2022-04-08 22:45:29 +01:00
from collections import OrderedDict
from vcinema_utils import VCinemaUtils
# Page ID of https://wiki.jacknet.io/books/vcinema/page/films-by-reference
HIDDEN_THEMES_PAGE_ID = 63
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():
hidden_themes = set()
if len(data['viewings']) > 1:
viewings_keywords = []
for viewing in data['viewings']:
if 'keywords' in viewing:
viewings_keywords.append(set(viewing['keywords']))
if len(viewings_keywords) > 1:
hidden_themes = set.intersection(*viewings_keywords)
if hidden_themes != set():
viewings_filtered_watch_date[date]['hidden_themes'] = list(hidden_themes)
return viewings_filtered_watch_date
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((k for k in sorted(data['hidden_themes'])) if 'hidden_themes' in data else "N/A")
table += " | ".join(row_data)
return table