From 4d6c8b13eb362fa0dba39d8a647cd9697b79cfd9 Mon Sep 17 00:00:00 2001 From: Kade Robertson Date: Thu, 4 Apr 2019 17:57:55 -0400 Subject: [PATCH] add extra date option patch --- README.md | 16 +++++++++++++++ youtube_dl/YoutubeDL.py | 36 +++++++++++++++++++++++++++++++++- youtube_dl/__init__.py | 1 + youtube_dl/extractor/common.py | 16 +++++++++++++++ youtube_dl/options.py | 4 ++++ 5 files changed, 72 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 45326c69ec5..80d0bcd4489 100644 --- a/README.md +++ b/README.md @@ -510,6 +510,22 @@ The basic usage is not to set any template arguments when downloading a single f - `release_date` (string): The date (YYYYMMDD) when the video was released - `timestamp` (numeric): UNIX timestamp of the moment the video became available - `upload_date` (string): Video upload date (YYYYMMDD) + - `upload_year` (string): Year the video was uploaded (YYYY). + - `upload_month` (string): Month the video was uploaded (MM). + - `upload_day` (string): Day the video was uploaded (DD). + - `upload_dyofyr` (string): Day of year the video was uploaded (DDD). + - `upload_wkofyr` (string): Week of year the video was uploaded (WW). + - `upload_dyn` (string): Localized day of the week the video was uploaded (Monday). + - `upload_dyna` (string): Localized abbreviated day of the week the video was uploaded (Mon). + - `upload_mntn` (string): Localized month of the year the video was uploaded (September). + - `upload_mntna` (string): Localized abbreviated month of the year the video was uploaded (Sep). + - `upload_hour24` (string): Hour of the day (24 hour clock) the video was uploaded (HH). + - `upload_hour12` (string): Hour of the day (12 hour clock) the video was uploaded (hh). + - `upload_minute` (string): Minute of the hour the video was uploaded (mm). + - `upload_second` (string): Second of the minute the video was uploaded (ss). + - `upload_ampm` (string): Localized AM/PM indicator of when video was uploaded (PP). + - `upload_time24` (string): Time of day (24 hour clock) the video was uploaded (HHmmss). + - `upload_time12` (string): Time of day (12 hour clock) the video was uploaded (hhmmssPP). - `uploader_id` (string): Nickname or id of the video uploader - `channel` (string): Full name of the channel the video is uploaded on - `channel_id` (string): Id of the channel diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 19370f62b0d..f9a95b95825 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -638,9 +638,11 @@ def prepare_filename(self, info_dict): template_dict['epoch'] = int(time.time()) autonumber_size = self.params.get('autonumber_size') + autonumber_offset = self.params.get('autonumber_offset') if autonumber_size is None: autonumber_size = 5 template_dict['autonumber'] = self.params.get('autonumber_start', 1) - 1 + self._num_downloads + template_dict['autonumberoffset'] = template_dict['autonumber'] + autonumber_offset if template_dict.get('resolution') is None: if template_dict.get('width') and template_dict.get('height'): template_dict['resolution'] = '%dx%d' % (template_dict['width'], template_dict['height']) @@ -665,8 +667,9 @@ def prepare_filename(self, info_dict): field_size_compat_map = { 'playlist_index': len(str(template_dict['n_entries'])), 'autonumber': autonumber_size, + 'autonumberoffset': autonumber_size } - FIELD_SIZE_COMPAT_RE = r'(?autonumber|playlist_index)\)s' + FIELD_SIZE_COMPAT_RE = r'(?autonumber|autonumberoffset|playlist_index)\)s' mobj = re.search(FIELD_SIZE_COMPAT_RE, outtmpl) if mobj: outtmpl = re.sub( @@ -1485,6 +1488,37 @@ def sanitize_numeric_fields(info): except (ValueError, OverflowError, OSError): pass + # Add extended date fields for organization + upload_date = None + if info_dict.get('timestamp') is not None: + try: + upload_date = datetime.datetime.utcfromtimestamp(info_dict['timestamp']) + except (ValueError, OverflowError, OSError): + pass + # should only occur if timestamp didn't exist + if upload_date is None and info_dict.get('upload_date') is not None: + try: + upload_date = datetime.datetime.strptime(info_dict['upload_date'], '%Y%m%d') + except (ValueError, OverflowError, OSError): + pass + if upload_date is not None: + info_dict['upload_year'] = upload_date.strftime('%Y') + info_dict['upload_month'] = upload_date.strftime('%m') + info_dict['upload_day'] = upload_date.strftime('%d') + info_dict['upload_dyofyr'] = upload_date.strftime('%j') + info_dict['upload_wkofyr'] = upload_date.strftime('%U') + info_dict['upload_dyn'] = upload_date.strftime('%A') + info_dict['upload_dyn_a'] = upload_date.strftime('%a') + info_dict['upload_mntn'] = upload_date.strftime('%B') + info_dict['upload_mntn_a'] = upload_date.strftime('%b') + info_dict['upload_hour24'] = upload_date.strftime('%H') + info_dict['upload_hour12'] = upload_date.strftime('%I') + info_dict['upload_minute'] = upload_date.strftime('%M') + info_dict['upload_second'] = upload_date.strftime('%S') + info_dict['upload_ampm'] = upload_date.strftime('%p') + info_dict['upload_time24'] = upload_date.strftime('%H%M%S') + info_dict['upload_time12'] = upload_date.strftime('%I%M%S%p') + # Auto generate title fields corresponding to the *_number fields when missing # in order to always have clean titles. This is very common for TV series. for field in ('chapter', 'season', 'episode'): diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 9a659fc654d..d0e4e89a114 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -342,6 +342,7 @@ def parse_retries(retries): 'outtmpl': outtmpl, 'autonumber_size': opts.autonumber_size, 'autonumber_start': opts.autonumber_start, + 'autonumber_offset': opts.autonumber_offset, 'restrictfilenames': opts.restrictfilenames, 'ignoreerrors': opts.ignoreerrors, 'force_generic_extractor': opts.force_generic_extractor, diff --git a/youtube_dl/extractor/common.py b/youtube_dl/extractor/common.py index a61753b17cd..ef42ae1d72f 100644 --- a/youtube_dl/extractor/common.py +++ b/youtube_dl/extractor/common.py @@ -232,6 +232,22 @@ class InfoExtractor(object): timestamp: UNIX timestamp of the moment the video became available. upload_date: Video upload date (YYYYMMDD). If not explicitly set, calculated from timestamp. + upload_year: Year the video was uploaded (YYYY). + upload_month: Month the video was uploaded (MM). + upload_day: Day the video was uploaded (DD). + upload_dyofyr: Day of year the video was uploaded (DDD). + upload_wkofyr: Week of year the video was uploaded (WW). + upload_dyn: Localized day of the week the video was uploaded (Monday). + upload_dyna: Localized abbreviated day of the week the video was uploaded (Mon). + upload_mntn: Localized month of the year the video was uploaded (September). + upload_mntna: Localized abbreviated month of the year the video was uploaded (Sep). + upload_hour24: Hour of the day (24 hour clock) the video was uploaded (HH). + upload_hour12: Hour of the day (12 hour clock) the video was uploaded (hh). + upload_minute: Minute of the hour the video was uploaded (mm). + upload_second: Second of the minute the video was uploaded (ss). + upload_ampm: Localized AM/PM indicator of when video was uploaded (PP). + upload_time24: Time of day (24 hour clock) the video was uploaded (HHmmss). + upload_time12: Time of day (12 hour clock) the video was uploaded (hhmmssPP). uploader_id: Nickname or id of the video uploader. uploader_url: Full URL to a personal webpage of the video uploader. channel: Full name of the channel the video is uploaded on. diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 6d5ac62b3ba..46cc1363977 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -697,6 +697,10 @@ def _comma_separated_values_options_callback(option, opt_str, value, parser): '--autonumber-start', dest='autonumber_start', metavar='NUMBER', default=1, type=int, help='Specify the start value for %(autonumber)s (default is %default)') + filesystem.add_option( + '--autonumber-offset', + dest='autonumber_offset', metavar='NUMBER', default=0, type=int, + help='Specify an optional offset for autonumber, used as %(autonumberoffset)s') filesystem.add_option( '--restrict-filenames', action='store_true', dest='restrictfilenames', default=False,