60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
from progress.bar import Bar
|
|
|
|
from imdb_utils import IMDbUtils
|
|
from wiki_utils import WikiUtils
|
|
|
|
|
|
JACKNET_WIKI_URL = "https://wiki.jacknet.io"
|
|
|
|
|
|
def get_viewings_csv_attachment_id(token_id, token_secret):
|
|
attachments = WikiUtils.get_attachments(JACKNET_WIKI_URL, token_id, token_secret)
|
|
|
|
# Page ID of "https://wiki.jacknet.io/books/vcinema/page/csv"
|
|
page_id = 11
|
|
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)
|
|
|
|
|
|
def get_vcinema_viewings(token_id, token_secret):
|
|
attachment_id = get_viewings_csv_attachment_id(token_id, token_secret)
|
|
|
|
viewings_csv = WikiUtils.get_attachment_contents(attachment_id, JACKNET_WIKI_URL, token_id, token_secret)
|
|
viewings_csv = viewings_csv.decode("utf-8")
|
|
viewings_csv_rows = viewings_csv.strip().split("\n")
|
|
|
|
headers = viewings_csv_rows.pop(0).split(",")
|
|
viewings = [dict(zip(headers, row.split(","))) for row in viewings_csv_rows]
|
|
|
|
return viewings
|
|
|
|
|
|
def add_imdb_data_to_viewings(viewings, field_name):
|
|
viewing_count = len(viewings)
|
|
|
|
with Bar('Processing', max=viewing_count) as bar:
|
|
bar.message = "Processing"
|
|
bar.suffix = '%(percent).1f%% - %(eta)ds'
|
|
|
|
for (viewing_num, viewing) in enumerate(viewings):
|
|
imdb_entry = IMDbUtils.get_movie(viewing['imdb_id'])
|
|
|
|
viewing[field_name] = imdb_entry[field_name]
|
|
bar.next()
|
|
bar.finish()
|
|
|
|
|
|
def filter_viewings(viewings, filter_field, remove_duplicates=True):
|
|
viewings_filtered = {}
|
|
|
|
for viewing in viewings:
|
|
viewing_field = viewing[filter_field]
|
|
if viewing_field in viewings_filtered.keys():
|
|
if not remove_duplicates or not any(x['imdb_id'] == viewing['imdb_id'] for x in viewings_filtered[viewing_field]):
|
|
viewings_filtered[viewing_field] += [viewing]
|
|
else:
|
|
viewings_filtered[viewing[filter_field]] = [viewing]
|
|
|
|
return viewings_filtered
|