vcinema/update_viewings_csv.py

45 lines
1.7 KiB
Python
Raw Normal View History

2022-03-08 22:51:21 +00:00
import argparse
from bs4 import BeautifulSoup
import hashlib
from bookstack import Bookstack
from vcinema_utils import VCinemaUtils
2022-03-24 17:29:50 +00:00
def update_viewings_csv(token_id, token_secret, check_existing=True):
2022-03-08 22:51:21 +00:00
# Page ID of https://wiki.jacknet.io/books/vcinema/page/csv
page_id = 11
2022-03-09 20:22:14 +00:00
print("Retrieving viewings page")
2022-03-08 22:51:21 +00:00
html_page = Bookstack.get_page_html(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, page_id)
soup = BeautifulSoup(html_page, 'html.parser')
2022-03-08 22:53:24 +00:00
csv_data = soup.find("code").text.strip().encode('utf-8')
2022-03-08 22:51:21 +00:00
2022-03-24 17:29:50 +00:00
if check_existing:
print("Retrieving existing file")
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)
existing_attachment_hash = hashlib.md5(attachment).hexdigest()
page_hash = hashlib.md5(csv_data).hexdigest()
2022-03-08 22:51:21 +00:00
2022-03-24 17:29:50 +00:00
if not check_existing or page_hash != existing_attachment_hash:
2022-03-09 20:22:14 +00:00
print("Updating file")
2022-03-08 22:51:21 +00:00
# bookstack update file via api doesn't work
2022-03-08 22:53:24 +00:00
Bookstack.post_attachment(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, "vcinema.csv", csv_data, page_id)
2022-03-08 22:51:21 +00:00
Bookstack.delete_attachment(VCinemaUtils.JACKNET_WIKI_URL, token_id, token_secret, existing_attachment_id)
2022-03-24 17:29:50 +00:00
print("File updated")
2022-03-08 22:51:21 +00:00
else:
2022-03-24 17:29:50 +00:00
print("File already up-to-date")
2022-03-08 22:51:21 +00:00
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()
2022-03-24 17:29:50 +00:00
update_viewings_csv(args.token_id, args.token_secret)