Skip to content

Commit

Permalink
[extractor/youtube] Support shorter relative time format (#7191)
Browse files Browse the repository at this point in the history
See: TeamNewPipe/NewPipeExtractor#1067

Authored by: coletdjnz
  • Loading branch information
coletdjnz authored Jun 3, 2023
1 parent 1a7dcca commit 2fb35f6
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions yt_dlp/extractor/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,9 +893,16 @@ def _extract_thumbnails(data, *path_list):
def extract_relative_time(relative_time_text):
"""
Extracts a relative time from string and converts to dt object
e.g. 'streamed 6 days ago', '5 seconds ago (edited)', 'updated today'
e.g. 'streamed 6 days ago', '5 seconds ago (edited)', 'updated today', '8 yr ago'
"""
mobj = re.search(r'(?P<start>today|yesterday|now)|(?P<time>\d+)\s*(?P<unit>microsecond|second|minute|hour|day|week|month|year)s?\s*ago', relative_time_text)

# XXX: this could be moved to a general function in utils.py
# The relative time text strings are roughly the same as what
# Javascript's Intl.RelativeTimeFormat function generates.
# See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
mobj = re.search(
r'(?P<start>today|yesterday|now)|(?P<time>\d+)\s*(?P<unit>sec(?:ond)?|s|min(?:ute)?|h(?:our|r)?|d(?:ay)?|w(?:eek|k)?|mo(?:nth)?|y(?:ear|r)?)s?\s*ago',
relative_time_text)
if mobj:
start = mobj.group('start')
if start:
Expand Down

0 comments on commit 2fb35f6

Please sign in to comment.