Remove date checking functionality from mkbinder.py

This commit is contained in:
Anthony Wang 2022-01-22 21:59:32 -06:00
parent 70bd8774d1
commit f2f33a6665
Signed by: a
GPG key ID: BC96B00AEC5F2D76

View file

@ -1,17 +1,13 @@
#!/usr/bin/env python3
import re
import os
import re
from argparse import ArgumentParser
import pickle
# PDF conversion backends
import pdfkit
import weasyprint
from htmldate import find_date
from datetime import date
# CLI arguments
parser = ArgumentParser()
@ -21,68 +17,27 @@ args = parser.parse_args()
for filename in os.listdir('Links'):
if not filename.endswith('.txt'): continue
print('Examining: ' + filename)
os.makedirs(filename, exists_ok=True)
try:
os.mkdir(filename[:-4])
except: # I love bad error handling
pass
try:
dates = pickle.load(open(os.path.join('Links', filename[:-4] + '.pickle'), 'rb'))
except:
dates = {}
file = open(os.path.join('Links', filename), 'r')
links = file.readlines()
section = ''
for link in links:
for link in open(os.path.join('Links', filename), 'r').readlines():
if link[0:2] == '# ':
section = link[2:]
if link[0] == '#' or link[0] == '\n':
continue
try:
new_date_str = find_date(link[:-1])
except:
pass
if new_date_str == None:
new_date = date.fromisoformat('9999-01-01')
else:
new_date = date.fromisoformat(new_date_str)
try:
old_date = dates[link[:-1]]
except:
old_date = date.fromisoformat('1970-01-01')
# Time to print!
if new_date > old_date or args.force:
name = section + ' - ' + re.sub(r'(?u)[^-\w.]', '', link[5:]) + '.pdf'
if not os.path.exists(os.path.join('Links', name)) or args.force:
print('Downloading: ' + link[:-1])
print('Edit date: ' + str(new_date))
print('Destination: ' + name)
name = os.path.join(filename[:-4], section + ' - ' + re.sub(r'(?u)[^-\w.]', '', link[5:]) + '.pdf')
# name = re.sub(r'(?u)[^-\w.]', '', link[5:]) + '.pdf'
# print(name)
# Time to print!
try:
# weasyprint seems faster?
# but seems to be broken sometimes???
if args.backend == 'pdfkit':
pdfkit.from_url(link, name)
else:
pdf = weasyprint.HTML(link).write_pdf()
open(name, 'wb').write(pdf)
except: # Maybe should handle errors a little bit better?
print('Error when printing')
pass
# Update date
if new_date != date.fromisoformat('9999-01-01'):
dates[link[:-1]] = new_date
# Dump dates to a pickle
pickle.dump(dates, open(os.path.join('Links', filename[:-4] + '.pickle'), 'wb'))
except:
print('Error when printing 😱')