vcinema/vcinema_utils/VCinemaUtils.py

143 lines
4.4 KiB
Python
Raw Normal View History

2022-04-03 20:23:22 +01:00
from concurrent.futures import ThreadPoolExecutor
2022-03-12 16:08:02 +00:00
import csv
2022-12-19 22:26:38 +00:00
from datetime import datetime
2022-02-20 21:26:38 +00:00
2022-12-19 22:26:38 +00:00
from vcinema_utils.VCinemaFilm import VCinemaFilm
2022-02-20 21:26:38 +00:00
from imdb_utils import IMDbUtils
2022-02-27 14:34:23 +00:00
from bookstack import Bookstack
2022-02-20 21:26:38 +00:00
2022-02-20 21:53:36 +00:00
JACKNET_WIKI_URL = "https://wiki.jacknet.io"
2022-03-26 00:33:39 +00:00
# Page ID of https://wiki.jacknet.io/books/vcinema/page/csv
CSV_PAGE_ID = 11
2022-02-20 21:53:36 +00:00
2022-04-03 20:23:22 +01:00
def get_viewings_csv_attachment_id(token_id, token_secret):
2022-02-27 14:34:23 +00:00
attachments = Bookstack.get_attachments(JACKNET_WIKI_URL, token_id, token_secret)
viewings_csv_file_name = "vcinema.csv"
2022-03-26 00:33:39 +00:00
return next((x['id'] for x in attachments if x['uploaded_to'] == CSV_PAGE_ID and x['name'] == viewings_csv_file_name), None)
2022-12-19 22:26:38 +00:00
def get_vcinema_viewings(token_id, token_secret):
attachment_id = get_viewings_csv_attachment_id(token_id, token_secret)
2022-02-20 21:26:38 +00:00
2022-12-19 22:26:38 +00:00
viewings_csv = Bookstack.get_attachment(JACKNET_WIKI_URL, token_id, token_secret, attachment_id)
viewings_csv = viewings_csv.decode("utf-8")
2022-12-19 22:26:38 +00:00
viewings_csv_rows = viewings_csv.strip().split("\n")
2022-02-20 21:26:38 +00:00
2022-04-03 18:24:58 +01:00
viewings = list(csv.DictReader(viewings_csv_rows, quotechar='"'))
2022-12-19 22:26:38 +00:00
return viewings
2022-03-12 16:08:02 +00:00
2022-02-27 14:34:23 +00:00
2022-12-19 22:26:38 +00:00
def get_vcinema_films(token_id, token_secret):
viewings = get_vcinema_viewings(token_id, token_secret)
films = {}
2022-02-27 14:34:23 +00:00
2022-12-19 22:26:38 +00:00
for viewing in viewings:
imdb_id = viewing["imdb_id"]
title = viewing["title"]
2022-02-27 14:34:23 +00:00
2022-12-19 22:26:38 +00:00
if imdb_id not in films.keys():
film = VCinemaFilm(imdb_id=imdb_id, title=title)
films[imdb_id] = film
2022-02-27 14:34:23 +00:00
2022-12-19 22:26:38 +00:00
date_watched = datetime.strptime(viewing['date_watched'], "%Y-%m-%d")
season = viewing['season']
rating = viewing['rating']
2022-02-27 14:34:23 +00:00
2022-12-19 22:26:38 +00:00
films[imdb_id].add_viewing(date_watched, season, rating)
2022-02-20 21:26:38 +00:00
2022-12-19 22:26:38 +00:00
return list(films.values())
2022-02-20 21:26:38 +00:00
2022-12-19 22:26:38 +00:00
def add_imdb_data(imdb_id, films, data_fields, progressbar=None):
2022-04-03 20:23:22 +01:00
movie = IMDbUtils.get_movie(imdb_id)
2022-02-27 14:34:23 +00:00
2022-12-19 22:26:38 +00:00
for film in films:
if film.get_imdb_id() == movie.movieID:
2022-04-03 20:23:22 +01:00
for field_name in data_fields:
if field_name in movie:
2022-12-19 22:26:38 +00:00
film.add_imdb_data(field_name, movie[field_name])
2022-02-27 14:34:23 +00:00
2022-04-03 20:23:22 +01:00
if progressbar is not None:
progressbar.next()
2022-02-27 14:34:23 +00:00
2022-12-19 22:26:38 +00:00
def add_imdb_keywords(imdb_id, films, progressbar=None):
2022-04-03 20:27:09 +01:00
movie = IMDbUtils.get_movie_keywords(imdb_id)
2022-12-19 22:26:38 +00:00
for film in films:
if film.get_imdb_id() == movie.movieID:
2022-04-03 20:27:09 +01:00
if 'keywords' in movie:
2022-12-19 22:26:38 +00:00
film.add_imdb_data('keywords', movie['keywords'])
2022-04-03 20:27:09 +01:00
if progressbar is not None:
progressbar.next()
2022-12-19 22:26:38 +00:00
def add_imdb_data_to_films(films, field_names, progress_bar=None):
2022-04-03 20:23:22 +01:00
with ThreadPoolExecutor(4) as executor:
future_imdb_tasks = set()
2022-04-08 21:06:31 +01:00
if ('keywords' in field_names and len(field_names) > 1) or ('keywords' not in field_names and len(field_names) > 0):
2022-12-19 22:26:38 +00:00
future_imdb_tasks.update(executor.submit(add_imdb_data, film.get_imdb_id(), films, field_names, progress_bar) for film in films)
2022-04-03 20:27:09 +01:00
if 'keywords' in field_names:
2022-12-19 22:26:38 +00:00
future_imdb_tasks.update(executor.submit(add_imdb_keywords, film.get_imdb_id(), films, progress_bar) for film in films)
2022-02-20 21:26:38 +00:00
2022-04-03 20:23:22 +01:00
progress_bar.max = len(future_imdb_tasks)
2022-02-20 21:26:38 +00:00
2022-04-03 20:23:22 +01:00
if progress_bar is not None:
progress_bar.finish()
2022-03-26 00:33:39 +00:00
2022-02-20 21:26:38 +00:00
2022-12-19 22:26:38 +00:00
def filter_films(films: [VCinemaFilm], field: str) -> [VCinemaFilm]:
films_filtered = {}
2022-02-20 21:26:38 +00:00
2022-12-19 22:26:38 +00:00
for film in films:
if film.get_imdb_data(field) is not None:
field_value = film.get_imdb_data(field)
if isinstance(field_value, list):
for value in list(field_value):
if value in films_filtered.keys():
films_filtered[value] += [film]
2022-02-27 14:34:23 +00:00
else:
2022-12-19 22:26:38 +00:00
films_filtered[value] = [film]
else:
2022-12-19 22:26:38 +00:00
if field_value in films_filtered.keys():
films_filtered[field_value] += [film]
else:
2022-12-19 22:26:38 +00:00
films_filtered[field_value] = [film]
2022-02-27 14:34:23 +00:00
2022-12-19 22:26:38 +00:00
return films_filtered
2022-04-08 21:37:14 +01:00
2022-12-19 22:26:38 +00:00
def get_film_list(films: [VCinemaFilm]) -> str:
2022-04-08 21:37:14 +01:00
film_links = []
for film in films:
2022-12-19 22:26:38 +00:00
film_link = film.get_imdb_link()
2022-04-08 21:37:14 +01:00
film_links.append(film_link)
if len(film_links) > 0:
return "<br>".join(film_links)
else:
return ""
2022-12-04 14:11:27 +00:00
2022-12-19 22:26:38 +00:00
def generate_markdown_link(text, url) -> str:
2022-12-04 14:11:27 +00:00
return "[{}]({})".format(text, url)
def generate_wikipedia_page_link(page_title):
2022-12-04 14:25:17 +00:00
return generate_markdown_link(page_title, generate_wikipedia_url(page_title))
def generate_wikipedia_url(page_title):
2022-12-10 07:59:05 +00:00
return "https://en.wikipedia.org/wiki/{}".format(page_title.replace(" ", "_"))