1
0
mirror of https://git.sr.ht/~cadence/NewLeaf synced 2024-11-14 12:07:30 +00:00

Improve recommendation collector on live videos

This commit is contained in:
Cadence Ember 2020-08-08 02:51:42 +12:00
parent 582a19bd1f
commit cb5a1d22f6
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412

View File

@ -138,6 +138,7 @@ class Second(object):
# Now try to get more stuff by manually examining the saved file # Now try to get more stuff by manually examining the saved file
# Figure out what the name of the saved file was # Figure out what the name of the saved file was
recommendations = []
possible_files = [f for f in os.listdir() if f.startswith("{}_".format(info["id"]))] possible_files = [f for f in os.listdir() if f.startswith("{}_".format(info["id"]))]
try: try:
if len(possible_files) == 1: if len(possible_files) == 1:
@ -163,31 +164,46 @@ class Second(object):
return None return None
def get_view_count(r): def get_view_count(r):
text = r["viewCountText"]["simpleText"] if "runs" in r["viewCountText"]: # has live viewers
if text == "Recommended for you": return int(r["viewCountText"]["runs"][0]["text"])
return 0 # subject to change?
else: else:
return int(text.replace(",", "").split(" ")[0]) text = r["viewCountText"]["simpleText"]
if text == "Recommended for you":
return 0 # subject to change?
else:
return int(text.replace(",", "").split(" ")[0])
def get_view_count_text(r): def get_view_count_text(r):
text = r["viewCountText"]["simpleText"] if "runs" in r["viewCountText"]: # has live viewers
if text == "Recommended for you": text = "".join([x["text"] for x in r["viewCountText"]["runs"]])
return "Recommended for you" # subject to change? else: # has past views
else: text = r["viewCountText"]["simpleText"]
return text if text == "Recommended for you":
return "Recommended for you" # subject to change?
else:
return text
# result["recommendedVideos"] = recommendations def get_length(r):
# return result if "lengthText" in r:
return length_text_to_seconds(r["lengthText"]["simpleText"])
else:
return -1
def get_length_text(r):
if "lengthText" in r:
return r["lengthText"]["simpleText"]
else:
return "Live now"
result["recommendedVideos"] = list({ result["recommendedVideos"] = list({
"videoId": r["videoId"], "videoId": r["videoId"],
"title": r["title"]["simpleText"], "title": r["title"]["simpleText"],
"videoThumbnails": [], "videoThumbnails": [],
"author": r["longBylineText"]["runs"][0]["text"], "author": r["longBylineText"]["runs"][0]["text"],
"authorUrl": r["longBylineText"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["canonicalBaseUrl"], "authorUrl": r["longBylineText"]["runs"][0]["navigationEndpoint"]["commandMetadata"]["webCommandMetadata"]["url"],
"authorId": r["longBylineText"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["browseId"], "authorId": r["longBylineText"]["runs"][0]["navigationEndpoint"]["browseEndpoint"]["browseId"],
"lengthSeconds": length_text_to_seconds(r["lengthText"]["simpleText"]), "lengthSeconds": get_length(r),
"second__lengthText": r["lengthText"]["simpleText"], "second__lengthText": get_length_text(r),
"viewCountText": get_view_count_text(r), "viewCountText": get_view_count_text(r),
"viewCount": get_view_count(r) "viewCount": get_view_count(r)
} for r in [get_useful_recommendation_data(r) for r in recommendations if get_useful_recommendation_data(r)]) } for r in [get_useful_recommendation_data(r) for r in recommendations if get_useful_recommendation_data(r)])
@ -199,6 +215,7 @@ class Second(object):
for file in possible_files: for file in possible_files:
os.unlink(file) os.unlink(file)
# return recommendations
return result return result
except youtube_dl.DownloadError: except youtube_dl.DownloadError: