Add backends arg
This commit is contained in:
parent
47e6933a6a
commit
4abae31b34
2 changed files with 44 additions and 5 deletions
30
README.md
30
README.md
|
@ -1,2 +1,30 @@
|
|||
# Astronomy
|
||||
Science Olympiad Astronomy
|
||||
|
||||
Resources for Science Olympiad Astronomy
|
||||
|
||||
## Guide
|
||||
|
||||
### Google Drive
|
||||
|
||||
Folder: https://drive.google.com/drive/folders/1tSWqSONcjwJWfYled2abuSCmDSUt4KiA?usp=sharing
|
||||
|
||||
|
||||
### Generate a binder
|
||||
|
||||
You need to have Python 3 installed.
|
||||
|
||||
Install dependencies: `pdfkit`, `weasyprint`, `wkhtmltopdf`
|
||||
|
||||
Add links to download to text files. The links in each text file will be downloaded to their own folder.
|
||||
|
||||
Run `./mkbinder.py`. It may take a while, depending on how many links to download.
|
||||
|
||||
```
|
||||
usage: mkbinder.py [-h] [--backend {pdfkit,weasyprint}]
|
||||
|
||||
optional arguments:
|
||||
-h, --help show this help message and exit
|
||||
--backend {pdfkit,weasyprint}, -b {pdfkit,weasyprint}
|
||||
change the download backend; default: pdfkit
|
||||
```
|
||||
|
||||
|
|
19
mkbinder.py
Normal file → Executable file
19
mkbinder.py
Normal file → Executable file
|
@ -1,7 +1,16 @@
|
|||
#!/usr/bin/python3
|
||||
import pdfkit
|
||||
# import weasyprint
|
||||
import weasyprint
|
||||
import re
|
||||
import os
|
||||
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()
|
||||
|
||||
|
||||
for filename in os.listdir("."):
|
||||
if not filename.endswith(".txt"): continue
|
||||
|
@ -22,9 +31,11 @@ for filename in os.listdir("."):
|
|||
# print(name)
|
||||
try:
|
||||
# weasyprint seems faster?
|
||||
pdfkit.from_url(link, name)
|
||||
# pdf = weasyprint.HTML(link).write_pdf()
|
||||
# open(name, 'wb').write(pdf)
|
||||
if args.backend == 'pdfkit':
|
||||
pdfkit.from_url(link, name)
|
||||
else:
|
||||
pdf = weasyprint.HTML(link).write_pdf()
|
||||
open(name, 'wb').write(pdf)
|
||||
except:
|
||||
# Ignore exceptions
|
||||
# Probably not a good idea
|
||||
|
|
Loading…
Reference in a new issue