diff --git a/botlib/README.md b/botlib/README.md index 831836e..aa86791 100644 --- a/botlib/README.md +++ b/botlib/README.md @@ -7,7 +7,7 @@ from botlib.cli import Cli, DirType from botlib.cron import Cron from botlib.curl import Curl from botlib.feed2list import Feed2List -from botlib.helper import Log, FileTime, StrFormat +from botlib.helper import Log, FileTime, StrFormat, FileWrite from botlib.html2list import HTML2List, MatchGroup from botlib.oncedb import OnceDB from botlib.tgclient import TGClient diff --git a/botlib/curl.py b/botlib/curl.py index c5e5f2c..4821470 100755 --- a/botlib/curl.py +++ b/botlib/curl.py @@ -105,7 +105,7 @@ class Curl: return False @staticmethod - def once(dest_dir, fname, urllist, date, desc=None, *, + def once(dest_dir, fname, urllist, date=None, *, override=False, dry_run=False, verbose=False, intro=''): did_update = False for url_str in urllist: @@ -116,22 +116,14 @@ class Curl: ext = parts.path.split('.')[-1] or 'unknown' file_path = os.path.join(dest_dir, fname + '.' + ext) if override or not os.path.isfile(file_path): - if not did_update and verbose and intro: - print(intro) - did_update = True + url = parts.geturl() if verbose: - print(' GET', parts.geturl()) - if not dry_run: - Curl.file(parts.geturl(), file_path, raise_except=True) - FileTime.set(file_path, date) - if desc: - desc_path = os.path.join(dest_dir, fname + '.txt') - if override or not os.path.isfile(desc_path): + if not did_update and intro: + print(intro) + print(' GET', url) did_update = True - if verbose: - print(' –>', desc_path) if not dry_run: - with open(desc_path, 'w') as f: - f.write(desc) - FileTime.set(desc_path, date) + Curl.file(url, file_path, raise_except=True) + if date: + FileTime.set(file_path, date) return did_update diff --git a/botlib/helper.py b/botlib/helper.py index 0061987..2df5c70 100755 --- a/botlib/helper.py +++ b/botlib/helper.py @@ -71,3 +71,28 @@ class StrFormat: text = text.replace('̈', 'e') # replace umlauts e.g., Ä -> Ae text = text.encode('ASCII', 'ignore') return ''.join(chr(c) for c in text if chr(c) in StrFormat.fnameChars) + + +class FileWrite: + @staticmethod + def once(dest_dir, fname, date=None, *, + override=False, dry_run=False, verbose=False, intro=''): + def _decorator(func): + path = os.path.join(dest_dir, fname) + if os.path.isfile(path) and not override: + return + content = func() + if not content: + return + if verbose: + if intro and not isinstance(intro, bool): + print(intro) + print(' –>', path) + if dry_run: + return + # write file + with open(path, 'w') as f: + f.write(content) + if date: + FileTime.set(path, date) + return _decorator