Support for ad cards

This commit is contained in:
Xeukxz
2025-10-12 21:41:42 +01:00
parent a27b768ff3
commit c37d7195a3

128
vxApi.py
View File

@@ -1,5 +1,6 @@
import html import html
from datetime import datetime from datetime import datetime
from flask import json
from configHandler import config from configHandler import config
from utils import stripEndTCO from utils import stripEndTCO
@@ -20,6 +21,48 @@ def getApiUserResponse(user):
"fetched_on": int(datetime.now().timestamp()), "fetched_on": int(datetime.now().timestamp()),
} }
def getBestMediaUrl(mediaList):
# find the highest bitrate
best_bitrate = -1
besturl=""
for j in mediaList:
if j['content_type'] == "video/mp4" and '/hevc/' not in j["url"] and j['bitrate'] > best_bitrate:
besturl = j["url"]
best_bitrate = j['bitrate']
if "?tag=" in besturl:
besturl = besturl[:besturl.index("?tag=")]
return besturl
def getExtendedVideoOrGifInfo(mediaEntry):
videoInfo = mediaEntry["video_info"]
info = {
"url": getBestMediaUrl(videoInfo["variants"]),
"type": "gif" if mediaEntry.get("type", "") == "animated_gif" else "video",
"size": {
"width": mediaEntry['original_info']["width"],
"height": mediaEntry['original_info']["height"]
},
"duration_millis": videoInfo.get("duration_millis", 0),
"thumbnail_url": mediaEntry.get("media_url_https", None),
"altText": mediaEntry.get("ext_alt_text", None),
"id_str": mediaEntry.get("id_str", None)
}
return info
def getExtendedImageInfo(mediaEntry):
info = {
"url": mediaEntry.get("media_url_https", None),
"type": "image",
"size": {
"width": mediaEntry["original_info"]["width"],
"height": mediaEntry["original_info"]["height"]
},
"thumbnail_url": mediaEntry.get("media_url_https", None),
"altText": mediaEntry.get("ext_alt_text", None),
"id_str": mediaEntry.get("id_str", None)
}
return info
def getApiResponse(tweet,include_txt=False,include_rtf=False): def getApiResponse(tweet,include_txt=False,include_rtf=False):
tweetL = tweet["legacy"] tweetL = tweet["legacy"]
if "user_result" in tweet["core"]: if "user_result" in tweet["core"]:
@@ -69,63 +112,46 @@ def getApiResponse(tweet,include_txt=False,include_rtf=False):
for i in tmedia: for i in tmedia:
extendedInfo={} extendedInfo={}
if "video_info" in i: if "video_info" in i:
# find the highest bitrate extendedInfo = getExtendedVideoOrGifInfo(i)
best_bitrate = -1 media.append(extendedInfo["url"])
besturl=""
for j in i["video_info"]["variants"]:
if j['content_type'] == "video/mp4" and '/hevc/' not in j["url"] and j['bitrate'] > best_bitrate:
besturl = j['url']
best_bitrate = j['bitrate']
if "?tag=" in besturl:
besturl = besturl[:besturl.index("?tag=")]
media.append(besturl)
extendedInfo["url"] = besturl
extendedInfo["type"] = "video"
if (i["type"] == "animated_gif"):
extendedInfo["type"] = "gif"
altText = None
extendedInfo["size"] = {"width":i["original_info"]["width"],"height":i["original_info"]["height"]}
if "ext_alt_text" in i:
altText=i["ext_alt_text"]
if "duration_millis" in i["video_info"]:
extendedInfo["duration_millis"] = i["video_info"]["duration_millis"]
else:
extendedInfo["duration_millis"] = 0
extendedInfo["thumbnail_url"] = i["media_url_https"]
extendedInfo["altText"] = altText
extendedInfo["id_str"] = i["id_str"]
media_extended.append(extendedInfo) media_extended.append(extendedInfo)
else: else:
media.append(i["media_url_https"]) media_extended.append(getExtendedImageInfo(i))
extendedInfo["url"] = i["media_url_https"]
altText=None
if "ext_alt_text" in i:
altText=i["ext_alt_text"]
extendedInfo["altText"] = altText
extendedInfo["type"] = "image"
extendedInfo["size"] = {"width":i["original_info"]["width"],"height":i["original_info"]["height"]}
extendedInfo["thumbnail_url"] = i["media_url_https"]
extendedInfo["id_str"] = i["id_str"]
media_extended.append(extendedInfo)
if "hashtags" in tweetL["entities"]: if "hashtags" in tweetL["entities"]:
for i in tweetL["entities"]["hashtags"]: for i in tweetL["entities"]["hashtags"]:
hashtags.append(i["text"]) hashtags.append(i["text"])
elif "card" in tweet and 'name' in tweet['card'] and tweet['card']['name'] == "player": elif "card" in tweet:
width = None if 'name' in tweet['card'] and tweet['card']['name'] == "player":
height = None width = None
vidUrl = None height = None
for i in tweet['card']['binding_values']: vidUrl = None
if i['key'] == 'player_stream_url': for i in tweet['card']['binding_values']:
vidUrl = i['value']['string_value'] if i['key'] == 'player_stream_url':
elif i['key'] == 'player_width': vidUrl = i['value']['string_value']
width = int(i['value']['string_value']) elif i['key'] == 'player_width':
elif i['key'] == 'player_height': width = int(i['value']['string_value'])
height = int(i['value']['string_value']) elif i['key'] == 'player_height':
if vidUrl != None and width != None and height != None: height = int(i['value']['string_value'])
media.append(vidUrl) if vidUrl != None and width != None and height != None:
media_extended.append({"url":vidUrl,"type":"video","size":{"width":width,"height":height}}) media.append(vidUrl)
media_extended.append({"url":vidUrl,"type":"video","size":{"width":width,"height":height}})
else:
for i in tweet['card']['binding_values']:
if i['key'] == 'unified_card' and 'value' in i and 'string_value' in i['value']:
card = json.loads(i['value']['string_value'])
media_key = card['component_objects']['media_1']['data']['id']
media_entry = card['media_entities'][media_key]
extendedInfo = getExtendedVideoOrGifInfo(media_entry)
media.append(extendedInfo['url'])
media_extended.append(extendedInfo)
break
elif i['key'] == 'photo_image_full_size_large' and 'value' in i and 'image_value' in i['value']:
imgData = i['value']['image_value']
imgurl = imgData['url']
media.append(imgurl)
media_extended.append({"url":imgurl,"type":"image","size":{"width":imgData['width'],"height":imgData['height']}})
break
if "article" in tweet: if "article" in tweet:
try: try:
result = tweet["article"]["article_results"]["result"] result = tweet["article"]["article_results"]["result"]