From c93277812ea3eb574361594c6390a005e752d135 Mon Sep 17 00:00:00 2001 From: Viktor Lindgren Date: Sat, 21 Sep 2013 16:26:33 +0200 Subject: [PATCH 1/4] Added --lastrun option. This feature is useful for subscription as it remember when it last time ran in order to not try to download earlier videos that may have been seen and deleted already. --- README.md | 2 ++ youtube_dl/__init__.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f54945acc..c142a7da6 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ which means you can modify it, redistribute it or use it however you like. --date DATE download only videos uploaded in this date --datebefore DATE download only videos uploaded before this date --dateafter DATE download only videos uploaded after this date + --lastrun FILE reads and update the FILE with last completed run. + It sets --dateafter to the DATE from the FILE. ## Download Options: -r, --rate-limit LIMIT maximum download rate (e.g. 50k or 44.6m) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 1ed30aae3..b47eebbba 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -181,6 +181,7 @@ def parseOpts(overrideArguments=None): selection.add_option('--date', metavar='DATE', dest='date', help='download only videos uploaded in this date', default=None) selection.add_option('--datebefore', metavar='DATE', dest='datebefore', help='download only videos uploaded before this date', default=None) selection.add_option('--dateafter', metavar='DATE', dest='dateafter', help='download only videos uploaded after this date', default=None) + selection.add_option('--lastrun', metavar='FILE', dest='lastrun', help='reads and update the file with last completed run. It sets --dateafter to this value.', default=None) authentication.add_option('-u', '--username', @@ -532,7 +533,12 @@ def _real_main(argv=None): if opts.date is not None: date = DateRange.day(opts.date) else: - date = DateRange(opts.dateafter, opts.datebefore) + if opts.lastrun: + with open(opts.lastrun, "r") as conf: + date = DateRange(conf.read(), opts.datebefore) + else: + date = DateRange(opts.dateafter, opts.datebefore) + # --all-sub automatically sets --write-sub if --write-auto-sub is not given # this was the old behaviour if only --all-sub was given. @@ -668,6 +674,10 @@ def _real_main(argv=None): except (IOError, OSError) as err: sys.exit(u'ERROR: unable to save cookie jar') + if opts.lastrun and retcode is 0: + with open(opts.lastrun, "w") as conf: + conf.write(date_from_str("now")) + sys.exit(retcode) def main(argv=None): From e071e4cb8f9ea290390eefdb3bc72f7a565a648c Mon Sep 17 00:00:00 2001 From: Viktor Lindgren Date: Sat, 21 Sep 2013 17:23:12 +0200 Subject: [PATCH 2/4] For option --lastrun creates file if not exist... For option --lastrun creates file if not exist and use DATE now. --- youtube_dl/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index b47eebbba..983c70c8d 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -534,6 +534,10 @@ def _real_main(argv=None): date = DateRange.day(opts.date) else: if opts.lastrun: + if not os.path.exists(opts.lastrun): + with open(opts.lastrun, "w") as conf: + conf.write(date_from_str("now")) + with open(opts.lastrun, "r") as conf: date = DateRange(conf.read(), opts.datebefore) else: From f4cb904a5e72169d84f286ecd06c770c1edb31ed Mon Sep 17 00:00:00 2001 From: Viktor Lindgren Date: Sat, 21 Sep 2013 20:56:03 +0200 Subject: [PATCH 3/4] Option --lastrun is now more tolerant for errors. Uses now .readline() and .strip() on date file. --- youtube_dl/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 983c70c8d..6e36b22a5 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -56,6 +56,7 @@ from .FileDownloader import * from .extractor import gen_extractors from .YoutubeDL import YoutubeDL from .PostProcessor import * +from datetime import date as dateh def parseOpts(overrideArguments=None): def _readOptions(filename_bytes): @@ -536,10 +537,10 @@ def _real_main(argv=None): if opts.lastrun: if not os.path.exists(opts.lastrun): with open(opts.lastrun, "w") as conf: - conf.write(date_from_str("now")) + conf.write(dateh.today().strftime("%Y%m%d")) with open(opts.lastrun, "r") as conf: - date = DateRange(conf.read(), opts.datebefore) + date = DateRange(conf.readline().strip(), opts.datebefore) else: date = DateRange(opts.dateafter, opts.datebefore) @@ -680,7 +681,7 @@ def _real_main(argv=None): if opts.lastrun and retcode is 0: with open(opts.lastrun, "w") as conf: - conf.write(date_from_str("now")) + conf.write(dateh.today().strftime("%Y%m%d")) sys.exit(retcode) From d272e852a4704273971edfbb2f6e71530c307ec2 Mon Sep 17 00:00:00 2001 From: Viktor Lindgren Date: Sat, 21 Sep 2013 21:07:15 +0200 Subject: [PATCH 4/4] Option --lastrun ignores exitcode --- youtube_dl/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 6e36b22a5..41dc32d7f 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -672,6 +672,10 @@ def _real_main(argv=None): ydl.to_screen(u'--max-download limit reached, aborting.') retcode = 101 + if opts.lastrun: + with open(opts.lastrun, "w") as conf: + conf.write(dateh.today().strftime("%Y%m%d")) + # Dump cookie jar if requested if opts.cookiefile is not None: try: @@ -679,10 +683,6 @@ def _real_main(argv=None): except (IOError, OSError) as err: sys.exit(u'ERROR: unable to save cookie jar') - if opts.lastrun and retcode is 0: - with open(opts.lastrun, "w") as conf: - conf.write(dateh.today().strftime("%Y%m%d")) - sys.exit(retcode) def main(argv=None):