# This function adapted from https://github.com/cdown/srt/blob/11089f1e021f2e074d04c33fc7ffc4b7b52e7045/srt.py, lines 69 and 189 (MIT License) def timedelta_to_sbv_timestamp(timedelta_timestamp): r""" Convert a :py:class:`~datetime.timedelta` to an SRT timestamp. .. doctest:: >>> import datetime >>> delta = datetime.timedelta(hours=1, minutes=23, seconds=4) >>> timedelta_to_sbv_timestamp(delta) '01:23:04,000' :param datetime.timedelta timedelta_timestamp: A datetime to convert to an SBV timestamp :returns: The timestamp in SBV format :rtype: str """ SECONDS_IN_HOUR = 3600 SECONDS_IN_MINUTE = 60 HOURS_IN_DAY = 24 MICROSECONDS_IN_MILLISECOND = 1000 hrs, secs_remainder = divmod(timedelta_timestamp.seconds, SECONDS_IN_HOUR) hrs += timedelta_timestamp.days * HOURS_IN_DAY mins, secs = divmod(secs_remainder, SECONDS_IN_MINUTE) msecs = timedelta_timestamp.microseconds // MICROSECONDS_IN_MILLISECOND return "%1d:%02d:%02d.%03d" % (hrs, mins, secs, msecs) from datetime import timedelta from json import dumps from gc import collect # import requests from time import sleep # https://docs.python.org/3/library/html.parser.html from html.parser import HTMLParser class MyHTMLParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.captions = [] self.title = "" self.description = "" self.inittitle = "" self.initdescription = "" def check_attr(self, attrs, attr, value): for item in attrs: if item[0] == attr and item[1] == value: return True return False def get_attr(self, attrs, attr): for item in attrs: if item[0] == attr: return item[1] return False def handle_starttag(self, tag, attrs): if tag == "input" and self.check_attr(attrs, "class", "yt-uix-form-input-text event-time-field event-start-time"): self.captions.append({"startTime": int(self.get_attr(attrs, "data-start-ms")), "text": ""}) elif tag == "input" and self.check_attr(attrs, "class", "yt-uix-form-input-text event-time-field event-end-time"): self.captions[len(self.captions)-1]["endTime"] = int(self.get_attr(attrs, "data-end-ms")) elif tag == "input" and self.check_attr(attrs, "id", "metadata-title"): self.title = self.get_attr(attrs, "value") elif tag == "textarea" and self.check_attr(attrs, "id", "metadata-description"): self.initdescription = self.get_attr(attrs, "data-original-description") def handle_data(self, data): if self.get_starttag_text() and self.get_starttag_text().startswith("