From 3772e7f190c308a0b5c79a82ab40c58f6cdc9319 Mon Sep 17 00:00:00 2001 From: Sarah Date: Sun, 20 Feb 2022 22:00:14 +0000 Subject: [PATCH] use csv file instead of reading from csv page --- vcinema_utils/VCinemaUtils.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/vcinema_utils/VCinemaUtils.py b/vcinema_utils/VCinemaUtils.py index 9862daf..63b8711 100644 --- a/vcinema_utils/VCinemaUtils.py +++ b/vcinema_utils/VCinemaUtils.py @@ -1,4 +1,3 @@ -from bs4 import BeautifulSoup from progress.bar import Bar from imdb_utils import IMDbUtils @@ -8,16 +7,25 @@ from wiki_utils import WikiUtils JACKNET_WIKI_URL = "https://wiki.jacknet.io" -def get_vcinema_viewings(token_id, token_secret): - # Page ID of /Vcinema/CSV +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" - html_page = WikiUtils.get_page_export_html(page_id, JACKNET_WIKI_URL, token_id, token_secret) + return next((x['id'] for x in attachments if x['uploaded_to'] == page_id and x['name'] == viewings_csv_file_name), None) - soup = BeautifulSoup(html_page, 'html.parser') - elements = soup.find("code").text.strip().split("\n") - headers = elements.pop(0).split(",") - viewings = [dict(zip(headers, row.split(","))) for row in elements] + +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