Astronomy/mkbinder.py

43 lines
1.2 KiB
Python
Raw Normal View History

2020-11-28 01:09:07 +00:00
#!/usr/bin/python3
2020-11-28 00:29:13 +00:00
import pdfkit
2020-11-28 01:09:07 +00:00
import weasyprint
2020-11-28 00:08:09 +00:00
import re
2020-11-28 00:29:13 +00:00
import os
2020-11-28 01:09:07 +00:00
import argparse
# CLI arguments
parser = argparse.ArgumentParser()
parser.add_argument('--backend', '-b', dest = 'backend', help = 'change the download backend; default: pdfkit', default = 'pdfkit', choices = ['pdfkit', 'weasyprint'])
args = parser.parse_args()
2020-11-28 00:08:09 +00:00
2020-11-28 00:29:13 +00:00
for filename in os.listdir("."):
if not filename.endswith(".txt"): continue
print("Examining: " + filename)
2020-11-28 00:08:09 +00:00
try:
2020-11-28 00:29:13 +00:00
os.mkdir(filename[:-4])
2020-11-28 00:08:09 +00:00
except:
pass
2020-11-28 00:29:13 +00:00
file = open(filename, "r")
links = file.readlines()
for link in links:
print("Downloading: " + link)
name = os.path.join(filename[:-4], re.sub(r'(?u)[^-\w.]', '', link[5:]) + ".pdf")
# name = re.sub(r'(?u)[^-\w.]', '', link[5:]) + ".pdf"
# print(name)
try:
# weasyprint seems faster?
2020-11-28 01:09:07 +00:00
if args.backend == 'pdfkit':
pdfkit.from_url(link, name)
else:
pdf = weasyprint.HTML(link).write_pdf()
open(name, 'wb').write(pdf)
2020-11-28 00:29:13 +00:00
except:
# Ignore exceptions
# Probably not a good idea
pass