from collections import Counter from concurrent.futures import ThreadPoolExecutor, as_completed import csv import functools from imdb_utils import IMDbUtils from bookstack import Bookstack JACKNET_WIKI_URL = "https://wiki.jacknet.io" # Page ID of https://wiki.jacknet.io/books/vcinema/page/csv CSV_PAGE_ID = 11 def get_viewings_csv_attachment_id(token_id, token_secret): attachments = Bookstack.get_attachments(JACKNET_WIKI_URL, token_id, token_secret) viewings_csv_file_name = "vcinema.csv" return next((x['id'] for x in attachments if x['uploaded_to'] == CSV_PAGE_ID and x['name'] == viewings_csv_file_name), None) def get_vcinema_viewings(token_id, token_secret, viewings_csv=None, combine_repeat_viewings=True): if viewings_csv is None: attachment_id = get_viewings_csv_attachment_id(token_id, token_secret) viewings_csv = Bookstack.get_attachment(JACKNET_WIKI_URL, token_id, token_secret, attachment_id) viewings_csv = viewings_csv.decode("utf-8") viewings_csv_rows = viewings_csv.strip().split("\n") viewings = list(csv.DictReader(viewings_csv_rows, quotechar='"')) if combine_repeat_viewings: for viewing in viewings: viewing['viewings'] = [ {'date_watched': viewing['date_watched'], 'season': viewing['season'], 'rating': viewing['rating']}] viewing.pop('date_watched') viewing.pop('season') viewing.pop('rating') watch_counts = Counter([x['imdb_id'] for x in viewings]) repeat_watches = [k for k, v in watch_counts.items() if v > 1] for film in repeat_watches: viewing_indexes = [index for index, viewing in enumerate(viewings) if viewing['imdb_id'] == film] first_watch = viewings[viewing_indexes[0]] for index in viewing_indexes[1::]: first_watch['viewings'].extend(viewings[index]['viewings']) for index in reversed(viewing_indexes[1::]): viewings.pop(index) return viewings def increment_progressbar(bar, _): bar.next() def add_imdb_data_to_viewings(viewings, field_names, progressbar=None): with ThreadPoolExecutor(4) as executor: future_imdb_tasks = {executor.submit(IMDbUtils.get_movie, viewing['imdb_id']) for viewing in viewings} if progressbar is not None: for this_future in future_imdb_tasks: this_future.add_done_callback(functools.partial(increment_progressbar, progressbar)) for future in as_completed(future_imdb_tasks): imdb_data = future.result() for viewing in viewings: if viewing['imdb_id'] == imdb_data.movieID: for field_name in field_names: if field_name in imdb_data: viewing[field_name] = imdb_data[field_name] if progressbar is not None: progressbar.finish() def filter_viewings(viewings, filter_field, progressbar=None): viewings_filtered = {} for viewing in viewings: if filter_field in viewing: viewing_field = viewing[filter_field] if isinstance(viewing_field, list): for fve in list(viewing_field): if fve in viewings_filtered.keys(): viewings_filtered[fve] += [viewing] else: viewings_filtered[fve] = [viewing] else: if viewing_field in viewings_filtered.keys(): viewings_filtered[viewing_field] += [viewing] else: viewings_filtered[viewing_field] = [viewing] if progressbar is not None: progressbar.next() return viewings_filtered