From 0a6a07838d1a898cc375a38d1ec4eb6555fd71a6 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 31 Aug 2020 02:26:46 +1200 Subject: [PATCH] Add publishedText to /channels/latest --- extractors/channel.py | 4 +++- tools/converters.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/extractors/channel.py b/extractors/channel.py index d155b01..42359ee 100644 --- a/extractors/channel.py +++ b/extractors/channel.py @@ -120,6 +120,7 @@ def extract_channel_latest(ucid): media_group = entry.find("{http://search.yahoo.com/mrss/}group") description = media_group.find("{http://search.yahoo.com/mrss/}description").text media_community = media_group.find("{http://search.yahoo.com/mrss/}community") + published = int(dateutil.parser.isoparse(entry.find("{http://www.w3.org/2005/Atom}published").text).timestamp()) results.append({ "type": "video", "title": entry.find("{http://www.w3.org/2005/Atom}title").text, @@ -131,7 +132,8 @@ def extract_channel_latest(ucid): "description": description, "descriptionHtml": add_html_links(escape_html_textcontent(description)), "viewCount": int(media_community.find("{http://search.yahoo.com/mrss/}statistics").attrib["views"]), - "published": int(dateutil.parser.isoparse(entry.find("{http://www.w3.org/2005/Atom}published").text).timestamp()), + "published": published, + "publishedText": time_to_past_text(published), "lengthSeconds": None, "liveNow": None, "paid": None, diff --git a/tools/converters.py b/tools/converters.py index 8d46aa4..23c498c 100644 --- a/tools/converters.py +++ b/tools/converters.py @@ -1,6 +1,7 @@ import configuration import datetime import re +import time def length_text_to_seconds(text): s = text.split(":") @@ -164,3 +165,23 @@ def past_text_to_time(text): elif unit == "ye": multiplier = 365 * 24 * 60 * 60 return int(datetime.datetime.now().timestamp()) - number * multiplier + +def time_to_past_text(timestamp): + now = int(time.time()) + diff = now - timestamp + print(diff, type(diff)) + units = [ + ["year", 365 * 24 * 60 * 60], + ["month", 30 * 24 * 60 * 60], + ["week", 7 * 24 * 60 * 60], + ["day", 24 * 60 * 60], + ["hour", 60 * 60], + ["minute", 60], + ["second", 1] + ] + for index in range(len(units)): + unit_name, unit_value = units[index] + 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)