From 093d01a8419b71a751ffbdc1cc5b7f883a70e1b8 Mon Sep 17 00:00:00 2001 From: Leon Haag-Fank Date: Fri, 5 Apr 2024 12:51:52 +0200 Subject: [PATCH] Improved logging and added --quiet for scripting --- zeit-dl | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/zeit-dl b/zeit-dl index 06776b0..26faa27 100755 --- a/zeit-dl +++ b/zeit-dl @@ -1,6 +1,7 @@ #!/usr/bin/env python import os +import sys import re from datetime import datetime import argparse @@ -10,7 +11,14 @@ EPAPER = 'https://epaper.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(): + ret = 0 + parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('user', type=str, help='Username') 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('-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('-q', '--quiet', action='store_true', help='No output except for filename if written.') args = parser.parse_args() if args.user is None or args.pwd is None: parser.error('You need to supply a username and password') @@ -33,21 +42,26 @@ def main(): try: page.locator("input[type='submit']").click() if page.url == MEINE+'/anmelden': - print('could not login') + log('could not login', args=args, level='ERROR') + ret = 1 else: - download(page, args) + ret = download(page, args=args) except Exception as e: page.goto(MEINE+'/abmelden') - print(e) + log(e, args=args, level='ERROR') + ret = 1 finally: page.goto(MEINE+'/abmelden') - print('finally: logout') + log('finally: logout', args=args) browser.close() + return ret + def download(page, args): if args.abo == 'zeit-audio': - print('not yet implemented') + log('not yet implemented', args=args, level='ERROR') + return 1 else: if args.issue is None: page.goto(EPAPER+'/abo/'+args.abo) @@ -67,8 +81,12 @@ def download(page, args): filepath = os.path.join(args.out, filename) if os.path.isfile(filepath): - print('Issue already exists:', filepath) - return 1 + log('File already exists:', filepath, args=args, level='WARNING') + if args.force: + log('Continuing...', args=args) + else: + log('Aborting...', args=args) + return 0 dl_btns = page.locator("div.download-buttons > a.btn").all() url = None @@ -76,19 +94,24 @@ def download(page, args): js_obj = btn.get_attribute('data-wt-click') match = re.search(r"9: ?'([^']*)'", js_obj) if match.group(1) == args.type: - print(js_obj) + log(js_obj, args=args) url = EPAPER+btn.get_attribute('href') continue 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 file = page.context.request.get(url) 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: f.write(file.body()) + return 0 + if __name__ == "__main__": - main() + sys.exit(main())