From 1ea86101fdcb197b95c720dc47b5869d26f41095 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 1 Jul 2021 23:42:53 +1200 Subject: [PATCH] Support premiere videos on channel --- extractors/channel.py | 20 +++++++++++++++----- tools/converters.py | 12 +++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/extractors/channel.py b/extractors/channel.py index 655bbe3..454a4e5 100644 --- a/extractors/channel.py +++ b/extractors/channel.py @@ -74,20 +74,29 @@ def extract_channel(ucid): v["gridVideoRenderer"] for v in tab_parts["gridRenderer"]["items"] if "gridVideoRenderer" in v ) for v in videos: - live = True - length_text = "LIVE" + live = False + is_upcoming = False + length_text = "UNKNOWN" length_seconds = -1 for o in v["thumbnailOverlays"]: if "thumbnailOverlayTimeStatusRenderer" in o: length_text = combine_runs(o["thumbnailOverlayTimeStatusRenderer"]["text"]) - if o["thumbnailOverlayTimeStatusRenderer"]["style"] != "LIVE": + length_text_style = o["thumbnailOverlayTimeStatusRenderer"]["style"] + if length_text_style == "DEFAULT": length_seconds = length_text_to_seconds(length_text) - live = False + elif length_text_style == "LIVE": + live = True + elif length_text_style == "UPCOMING": + is_upcoming = True published = 0 published_text = "Live now" + premiere_timestamp = None if "publishedTimeText" in v: published_text = v["publishedTimeText"]["simpleText"] published = past_text_to_time(published_text) + if "upcomingEventData" in v: + premiere_timestamp = v["upcomingEventData"]["startTime"] + published_text = time_to_past_text(int(premiere_timestamp)) view_count_text = combine_runs(v["viewCountText"]) if "viewCountText" in v else None view_count_text_short = combine_runs(v["shortViewCountText"]) if "shortViewCountText" in v else None @@ -112,7 +121,8 @@ def extract_channel(ucid): "liveNow": live, "paid": None, "premium": None, - "isUpcoming": None + "isUpcoming": is_upcoming, + "premiereTimestamp": premiere_timestamp }) channel = { diff --git a/tools/converters.py b/tools/converters.py index 87abcd1..7a96029 100644 --- a/tools/converters.py +++ b/tools/converters.py @@ -191,6 +191,13 @@ def past_text_to_time(text): def time_to_past_text(timestamp): now = int(time.time()) diff = now - timestamp + + # also allow for times in the future by using the same algorithm, then altering the output at the end + time_is_in_past = True + if diff < 0: + diff = -diff + time_is_in_past = False + units = [ ["year", 365 * 24 * 60 * 60], ["month", 30 * 24 * 60 * 60], @@ -205,7 +212,10 @@ def time_to_past_text(timestamp): if diff > unit_value or index + 1 >= len(units): number = diff // unit_value plural_unit = unit_name if number == 1 else unit_name + "s" - return "{} {} ago".format(number, plural_unit) + if time_is_in_past: + return "{} {} ago".format(number, plural_unit) + else: + return "in {} {}".format(number, plural_unit) def get_language_label_from_url(url_string): url = urlparse(url_string)