Improved logging and added --quiet for scripting

This commit is contained in:
Leon Haag-Fank 2024-04-05 12:51:52 +02:00
parent b1c604626b
commit 093d01a841

45
zeit-dl
View file

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import os import os
import sys
import re import re
from datetime import datetime from datetime import datetime
import argparse import argparse
@ -10,7 +11,14 @@ EPAPER = 'https://epaper.zeit.de'
MEINE = 'https://meine.zeit.de' MEINE = 'https://meine.zeit.de'
def log(*texts, args=None, level='INFO'):
if args is None or not args.quiet:
print(level+':', *texts)
def main(): def main():
ret = 0
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('user', type=str, help='Username') parser.add_argument('user', type=str, help='Username')
parser.add_argument('pwd', type=str, help='Password') parser.add_argument('pwd', type=str, help='Password')
@ -20,6 +28,7 @@ def main():
parser.add_argument('-t', '--type', type=str, choices=['pdf', 'epub', 'mp3'], default='pdf', help='File type') parser.add_argument('-t', '--type', type=str, choices=['pdf', 'epub', 'mp3'], default='pdf', help='File type')
parser.add_argument('-f', '--force', action='store_true', help='Redownload file even if already present') parser.add_argument('-f', '--force', action='store_true', help='Redownload file even if already present')
parser.add_argument('--format', type=str, default="{abo}_{issue}.{ext}", help='Filename format. Possible formatting strings are {abo}, {issue}, {ext} and datetime format codes.') parser.add_argument('--format', type=str, default="{abo}_{issue}.{ext}", help='Filename format. Possible formatting strings are {abo}, {issue}, {ext} and datetime format codes.')
parser.add_argument('-q', '--quiet', action='store_true', help='No output except for filename if written.')
args = parser.parse_args() args = parser.parse_args()
if args.user is None or args.pwd is None: if args.user is None or args.pwd is None:
parser.error('You need to supply a username and password') parser.error('You need to supply a username and password')
@ -33,21 +42,26 @@ def main():
try: try:
page.locator("input[type='submit']").click() page.locator("input[type='submit']").click()
if page.url == MEINE+'/anmelden': if page.url == MEINE+'/anmelden':
print('could not login') log('could not login', args=args, level='ERROR')
ret = 1
else: else:
download(page, args) ret = download(page, args=args)
except Exception as e: except Exception as e:
page.goto(MEINE+'/abmelden') page.goto(MEINE+'/abmelden')
print(e) log(e, args=args, level='ERROR')
ret = 1
finally: finally:
page.goto(MEINE+'/abmelden') page.goto(MEINE+'/abmelden')
print('finally: logout') log('finally: logout', args=args)
browser.close() browser.close()
return ret
def download(page, args): def download(page, args):
if args.abo == 'zeit-audio': if args.abo == 'zeit-audio':
print('not yet implemented') log('not yet implemented', args=args, level='ERROR')
return 1
else: else:
if args.issue is None: if args.issue is None:
page.goto(EPAPER+'/abo/'+args.abo) page.goto(EPAPER+'/abo/'+args.abo)
@ -67,8 +81,12 @@ def download(page, args):
filepath = os.path.join(args.out, filename) filepath = os.path.join(args.out, filename)
if os.path.isfile(filepath): if os.path.isfile(filepath):
print('Issue already exists:', filepath) log('File already exists:', filepath, args=args, level='WARNING')
return 1 if args.force:
log('Continuing...', args=args)
else:
log('Aborting...', args=args)
return 0
dl_btns = page.locator("div.download-buttons > a.btn").all() dl_btns = page.locator("div.download-buttons > a.btn").all()
url = None url = None
@ -76,19 +94,24 @@ def download(page, args):
js_obj = btn.get_attribute('data-wt-click') js_obj = btn.get_attribute('data-wt-click')
match = re.search(r"9: ?'([^']*)'", js_obj) match = re.search(r"9: ?'([^']*)'", js_obj)
if match.group(1) == args.type: if match.group(1) == args.type:
print(js_obj) log(js_obj, args=args)
url = EPAPER+btn.get_attribute('href') url = EPAPER+btn.get_attribute('href')
continue continue
if url is None: if url is None:
print('Could not find appropriate button for', args.type) log('Could not find appropriate button for', args.type, args=args, level='ERROR')
return 1 return 1
file = page.context.request.get(url) file = page.context.request.get(url)
if file.headers['content-type'] != 'text/html': if file.headers['content-type'] != 'text/html':
print('Downloading {}...'.format(filename)) if args.quiet:
print(filename)
else:
log('Downloading {}...'.format(filename), args=args)
with open(filepath, 'wb') as f: with open(filepath, 'wb') as f:
f.write(file.body()) f.write(file.body())
return 0
if __name__ == "__main__": if __name__ == "__main__":
main() sys.exit(main())