I'm writing a script to scour the metadata of YouTube videos and grab timecodes out of them, if any.
with urllib.request.urlopen("https://www.googleapis.com/youtube/v3/videos?id=m65QTeKRWNg&key=AIzaSyDls3PGTAKqbr5CqSmxt71fzZTNHZCQzO8&part=snippet") as url:
data = json.loads(url.read().decode())
description = json.dumps(data['items'][0]['snippet']['description'], indent=4, sort_keys=True)
print(description)
This works fine, so I go ahead and find the timecodes.
# finds timecodes like 00:00
timeBeforeHour = re.findall(r'[\d\.-]+:[\d.-]+', description)
>>[''0:00', '6:00', '9:30', '14:55', '19:00', '23:23', '28:18', '33:33', '37:44', '40:04', '44:15', '48:00', '54:00', '58:18', '1:02', '1:06', '1:08', '1:12', '1:17', '1:20']
It goes beyond and grabs times after 59:00, but not correctly as it misses the final ":", so I grab the remaining set:
# finds timecodes like 00:00:00
timePastHour = re.findall(r'[\d\.-]+:[\d.-]+:[\d\.-]+', description)
>>['1:02:40', '1:06:10', '1:08:15', '1:12:25', '1:17:08', '1:20:34']
I want to concatenate them, but still have the issue of the incorrect times in the first regex. How can I stop the range of the first regex going above an hour i.e 59:59?
I look at regex and my head explodes a bit, any clarifacation would be super!
edit:
I've tried this:
description = re.findall(r'?<!\d:)(?<!\d)[0-5]\d:[0-5]\d(?!:?\d', description)
and this:
description = re.findall(r'^|[^\d:])([0-5]?[0-9]:[0-5][0-9])([^\d:]|$', description)
but I'm entering them wrong. What is it position of the regex doing?
Also for context, this is part of the sample I'm trying to strip:
Naked\n1:02:40 Marvel 83' - Genesis\n1:06:10 Ward-Iz - The Chase\n1:08:15 Epoch - Formula\n1:12:25 Perturbator - Night Business\n1:17:08 Murkula - Death Code\n1:20:34 LAZERPUNK - Revenge\n\nPhotography by Jezael Melgoza"