From 7c0b7dc574ef0f1a814fbde393e54586e7bdee9f Mon Sep 17 00:00:00 2001 From: Sarah Date: Tue, 8 Mar 2022 22:51:21 +0000 Subject: [PATCH] Create update_viewings_csv.py --- update_viewings_csv.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 update_viewings_csv.py diff --git a/update_viewings_csv.py b/update_viewings_csv.py new file mode 100644 index 0000000..567dce2 --- /dev/null +++ b/update_viewings_csv.py @@ -0,0 +1,44 @@ +import argparse +from bs4 import BeautifulSoup +import hashlib + + +from bookstack import Bookstack +from vcinema_utils import VCinemaUtils + + +def update_viewings_csv_file_from_page(token_id, token_secret): + # Page ID of https://wiki.jacknet.io/books/vcinema/page/csv + page_id = 11 + + html_page = Bookstack.get_page_html(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, page_id) + + soup = BeautifulSoup(html_page, 'html.parser') + csv_data = soup.find("code").text.strip() + + existing_attachment_id = VCinemaUtils.get_viewings_csv_attachment_id(token_id, token_secret) + + attachment = Bookstack.get_attachment(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, existing_attachment_id).decode('utf-8') + + existing_attachment_hash = hashlib.md5(attachment.encode('utf-8')).hexdigest() + + page_hash = hashlib.md5(csv_data.encode('utf-8')).hexdigest() + + if page_hash != existing_attachment_hash: + print("updating file") + # bookstack update file via api doesn't work + Bookstack.post_attachment(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, "vcinema.csv", csv_data.encode("utf-8"), page_id) + Bookstack.delete_attachment(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, existing_attachment_id) + print("done") + else: + print("file already up to date") + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Update the vcinema.csv file with data from the CSV wiki page.') + parser.add_argument('token_id', help='API token ID.') + parser.add_argument('token_secret', help='API token secret.') + + args = parser.parse_args() + + update_viewings_csv_file_from_page(args.token_id, args.token_secret)