Skip to content

Commit

Permalink
add extra date option patch
Browse files Browse the repository at this point in the history
  • Loading branch information
kade-robertson committed Sep 6, 2020
1 parent d51e23d commit 4d6c8b1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
36 changes: 35 additions & 1 deletion youtube_dl/YoutubeDL.py
Expand Up @@ -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'])
Expand All @@ -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'(?<!%)%\((?P<field>autonumber|playlist_index)\)s'
FIELD_SIZE_COMPAT_RE = r'(?<!%)%\((?P<field>autonumber|autonumberoffset|playlist_index)\)s'
mobj = re.search(FIELD_SIZE_COMPAT_RE, outtmpl)
if mobj:
outtmpl = re.sub(
Expand Down Expand Up @@ -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'):
Expand Down
1 change: 1 addition & 0 deletions youtube_dl/__init__.py
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions youtube_dl/extractor/common.py
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions youtube_dl/options.py
Expand Up @@ -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,
Expand Down

0 comments on commit 4d6c8b1

Please sign in to comment.