2022-02-27 14:34:23 +00:00
|
|
|
from collections import Counter
|
|
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
2022-02-27 15:23:20 +00:00
|
|
|
import functools
|
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-02-20 22:00:14 +00: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)
|
2022-02-20 22:00:14 +00:00
|
|
|
|
2022-02-20 22:01:38 +00:00
|
|
|
# Page ID of "https://wiki.jacknet.io/books/vcinema/page/csv"
|
2022-02-20 21:26:38 +00:00
|
|
|
page_id = 11
|
2022-02-20 22:00:14 +00:00
|
|
|
viewings_csv_file_name = "vcinema.csv"
|
|
|
|
|
|
|
|
return next((x['id'] for x in attachments if x['uploaded_to'] == page_id and x['name'] == viewings_csv_file_name), None)
|
|
|
|
|
|
|
|
|
2022-02-27 14:34:23 +00:00
|
|
|
def get_vcinema_viewings(token_id, token_secret, combine_repeat_viewings=True):
|
2022-02-20 22:00:14 +00:00
|
|
|
attachment_id = get_viewings_csv_attachment_id(token_id, token_secret)
|
2022-02-20 21:26:38 +00:00
|
|
|
|
2022-02-27 14:34:23 +00:00
|
|
|
viewings_csv = Bookstack.get_attachment(JACKNET_WIKI_URL, token_id, token_secret, attachment_id)
|
2022-02-20 22:00:14 +00:00
|
|
|
viewings_csv = viewings_csv.decode("utf-8")
|
|
|
|
viewings_csv_rows = viewings_csv.strip().split("\n")
|
2022-02-20 21:26:38 +00:00
|
|
|
|
2022-02-20 22:00:14 +00:00
|
|
|
headers = viewings_csv_rows.pop(0).split(",")
|
|
|
|
viewings = [dict(zip(headers, row.split(","))) for row in viewings_csv_rows]
|
2022-02-20 21:26:38 +00:00
|
|
|
|
2022-03-08 22:44:20 +00:00
|
|
|
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')
|
|
|
|
|
2022-02-27 14:34:23 +00:00
|
|
|
if combine_repeat_viewings:
|
|
|
|
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::]:
|
2022-03-08 22:44:20 +00:00
|
|
|
first_watch['viewings'].append(viewings[index]['viewings'])
|
2022-02-27 14:34:23 +00:00
|
|
|
|
|
|
|
for index in reversed(viewing_indexes[1::]):
|
|
|
|
viewings.pop(index)
|
|
|
|
|
2022-02-20 21:26:38 +00:00
|
|
|
return viewings
|
|
|
|
|
|
|
|
|
2022-02-27 15:23:20 +00:00
|
|
|
def increment_progressbar(bar, _):
|
|
|
|
bar.next()
|
2022-02-27 14:34:23 +00:00
|
|
|
|
|
|
|
|
2022-02-27 14:41:52 +00:00
|
|
|
def add_imdb_data_to_viewings(viewings, field_names, progressbar=None):
|
|
|
|
with ThreadPoolExecutor(4) as executor:
|
2022-02-27 15:23:20 +00:00
|
|
|
future_to_url = {executor.submit(IMDbUtils.get_movie, viewing['imdb_id']) for viewing in viewings}
|
|
|
|
|
|
|
|
if progressbar is not None:
|
|
|
|
for this_future in future_to_url:
|
|
|
|
this_future.add_done_callback(functools.partial(increment_progressbar, progressbar))
|
2022-02-27 14:34:23 +00:00
|
|
|
|
2022-02-27 14:41:52 +00:00
|
|
|
for future in as_completed(future_to_url):
|
|
|
|
imdb_data = future.result()
|
2022-02-20 21:26:38 +00:00
|
|
|
|
2022-02-27 14:41:52 +00:00
|
|
|
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]
|
2022-02-20 21:26:38 +00:00
|
|
|
|
|
|
|
|
2022-02-27 15:34:06 +00:00
|
|
|
def filter_viewings(viewings, filter_field, progressbar=None):
|
2022-02-20 21:26:38 +00:00
|
|
|
viewings_filtered = {}
|
|
|
|
|
2022-02-27 15:34:06 +00:00
|
|
|
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]
|
2022-02-27 14:34:23 +00:00
|
|
|
else:
|
2022-02-27 15:34:06 +00:00
|
|
|
viewings_filtered[fve] = [viewing]
|
|
|
|
else:
|
|
|
|
if viewing_field in viewings_filtered.keys():
|
|
|
|
viewings_filtered[viewing_field] += [viewing]
|
|
|
|
else:
|
|
|
|
viewings_filtered[viewing_field] = [viewing]
|
2022-02-27 14:34:23 +00:00
|
|
|
|
2022-02-27 15:34:06 +00:00
|
|
|
if progressbar is not None:
|
|
|
|
progressbar.next()
|
2022-02-20 21:26:38 +00:00
|
|
|
|
|
|
|
return viewings_filtered
|