vcinema/update_films_by_year_page.py

57 lines
2.1 KiB
Python
Raw Normal View History

2022-02-20 21:26:38 +00:00
import argparse
from collections import OrderedDict
2022-02-27 14:34:23 +00:00
from progress.bar import Bar
2022-02-20 21:26:38 +00:00
2022-02-24 18:35:00 +00:00
from bookstack import Bookstack
2022-02-20 21:26:38 +00:00
from vcinema_utils import VCinemaUtils
def build_table(films_by_year, progressbar=None):
2022-02-27 13:54:18 +00:00
films_by_year_sorted = OrderedDict(sorted(films_by_year.items(), key=lambda t: t[0], reverse=True))
2022-02-20 21:26:38 +00:00
page_table = "| Year | Films |\n| - | - |\n"
2022-02-27 14:34:23 +00:00
for year in films_by_year_sorted.keys():
page_table += str(year) + " | "
page_table += "<br>".join("[{}](https://www.imdb.com/title/tt{}/)".format(film['title'], film['imdb_id']) for film in films_by_year_sorted[year])
page_table += "\n"
2022-02-27 14:34:23 +00:00
if progressbar is not None:
progressbar.next()
2022-02-20 21:26:38 +00:00
return page_table
def update_films_by_year_page(token_id, token_secret):
print("Retrieving VCinema viewings")
viewings = VCinemaUtils.get_vcinema_viewings(token_id, token_secret)
viewing_count = len(viewings)
with Bar('Retrieving movie data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining') as bar:
VCinemaUtils.add_imdb_data_to_viewings(viewings, ['year'], bar)
2022-02-20 21:26:38 +00:00
with Bar('Processing viewing data', max=viewing_count, suffix='%(percent).1f%% - %(eta)ds remaining') as bar:
viewings_by_year = VCinemaUtils.filter_viewings(viewings, 'year', bar)
2022-02-20 21:26:38 +00:00
year_count = len(viewings_by_year)
with Bar('Generating table', max=year_count, suffix='%(percent).1f%% - %(eta)ds remaining') as bar:
film_by_year_table = build_table(viewings_by_year, bar)
2022-02-20 21:26:38 +00:00
# Page ID of https://wiki.jacknet.io/books/vcinema/page/films-by-release-year
2022-02-27 13:54:18 +00:00
page_id = 24
2022-02-20 21:26:38 +00:00
print("Updating page")
2022-02-26 14:46:06 +00:00
Bookstack.update_page(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, page_id, markdown=film_by_year_table)
2022-02-20 21:26:38 +00:00
print("Done!")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Update page displaying VCinema films sorted by release year.')
parser.add_argument('token_id', help='API token ID.')
parser.add_argument('token_secret', help='API token secret.')
args = parser.parse_args()
update_films_by_year_page(args.token_id, args.token_secret)