diff --git a/test_vx_embeds.py b/test_vx_embeds.py index 919b85c..46f1020 100644 --- a/test_vx_embeds.py +++ b/test_vx_embeds.py @@ -29,7 +29,7 @@ def test_embed_qrtVideoTweet(): # this is an incredibly lazy test, todo: improve it in the future resp = client.get(testQrtVideoTweet.replace("https://twitter.com",""),headers={"User-Agent":"test"}) assert resp.status_code==200 - qtd_tweet=cache.getVnfFromLinkCache("https://twitter.com/i/status/1674197531301904388") + qtd_tweet=videoRedirect(cache.getVnfFromLinkCache("https://twitter.com/i/status/1674197531301904388")) vurl = qtd_tweet["mediaURLs"][0] assert f"twitter:player:stream\" content=\"{vurl}" in str(resp.data) @@ -65,22 +65,22 @@ def test_embed_Suggestive(): def test_embed_video_direct(): resp = client.get(testVideoTweet.replace("https://twitter.com","")+".mp4",headers={"User-Agent":"test"}) assert resp.status_code==200 - assert testVideoTweet_compare["mediaURLs"][0] in str(resp.data) + assert videoRedirect(testVideoTweet_compare)["mediaURLs"][0] in str(resp.data) def test_embed_video_direct_subdomain(): resp = client.get(testVideoTweet.replace("https://twitter.com","https://d.vxtwitter.com"),headers={"User-Agent":"test"}) assert resp.status_code==200 - assert testVideoTweet_compare["mediaURLs"][0] in str(resp.data) + assert videoRedirect(testVideoTweet_compare)["mediaURLs"][0] in str(resp.data) def test_embed_img_direct(): resp = client.get(testMediaTweet.replace("https://twitter.com","")+".png",headers={"User-Agent":"test"}) assert resp.status_code==200 - assert testMediaTweet_compare["mediaURLs"][0] in str(resp.data) + assert videoRedirect(testMediaTweet_compare)["mediaURLs"][0] in str(resp.data) def test_embed_img_direct_subdomain(): resp = client.get(testMediaTweet.replace("https://twitter.com","https://d.vxtwitter.com"),headers={"User-Agent":"test"}) assert resp.status_code==200 - assert testMediaTweet_compare["mediaURLs"][0] in str(resp.data) + assert videoRedirect(testMediaTweet_compare)["mediaURLs"][0] in str(resp.data) def test_embed_multi_direct(): # embed first item @@ -143,7 +143,7 @@ def test_embed_multimedia_single(): assert img1 not in str(resp.data) and img2 in str(resp.data) def test_embed_mixedMedia(): - twt = twitfix.getTweetData(testMixedMediaTweet) + twt = videoRedirect(twitfix.getTweetData(testMixedMediaTweet)) img1 = twt["mediaURLs"][0] img2 = twt["mediaURLs"][1] resp = client.get(testMixedMediaTweet.replace("https://twitter.com",""),headers={"User-Agent":"test"}) diff --git a/twitfix.py b/twitfix.py index 0282a13..91cd842 100644 --- a/twitfix.py +++ b/twitfix.py @@ -48,6 +48,11 @@ def isValidUserAgent(user_agent): return True return False +def fixMedia(mediaInfo): + # This is for the iOS Discord app, which has issues when serving URLs ending in .mp4 (https://github.com/dylanpdx/BetterTwitFix/issues/210) + mediaInfo['url'] = mediaInfo['url'].replace("https://video.twimg.com",f"{config['config']['url']}/tvid").replace(".mp4","") + return mediaInfo + def message(text): return render_template( 'default.html', @@ -73,6 +78,8 @@ def renderImageTweetEmbed(tweetData,image,appnameSuffix=""): def renderVideoTweetEmbed(tweetData,mediaInfo,appnameSuffix=""): qrt = tweetData['qrt'] embedDesc = msgs.formatEmbedDesc("Video",tweetData['text'],qrt,tweetData['pollData'],msgs.genLikesDisplay(tweetData)) + + mediaInfo=fixMedia(mediaInfo) return render_template("video.html", tweet=tweetData, media=mediaInfo, @@ -224,6 +231,7 @@ def twitfix(sub_path): if embedIndex == -1: # if the user didn't specify an index, we'll just use the first one embedIndex = 0 media = tweetData['media_extended'][embedIndex] + media=fixMedia(media) if media['type'] == "image": return render_template("rawimage.html",media=media) elif media['type'] == "video" or media['type'] == "gif": @@ -258,6 +266,11 @@ def favicon(): # pragma: no cover def apple_touch_icon(): # pragma: no cover return send_from_directory(os.path.join(app.root_path, 'static'), 'apple-touch-icon.png',mimetype='image/png') +@app.route('/tvid/') +def tvid(vid_path): + url = f"https://video.twimg.com/{vid_path}.mp4" + return redirect(url, 302) + @app.route("/rendercombined.jpg") def rendercombined(): # get "imgs" from request arguments diff --git a/vx_testdata.py b/vx_testdata.py index d08258d..b2681ca 100644 --- a/vx_testdata.py +++ b/vx_testdata.py @@ -38,4 +38,11 @@ def compareDict(original,compare): continue # does not match as test data was from before verification changes assert compare[key]==original[key] else: - compareDict(original[key],compare[key]) \ No newline at end of file + compareDict(original[key],compare[key]) + +def videoRedirect(tweetData): + for media in tweetData["media_extended"]: + media["url"] = media["url"].replace("https://video.twimg.com",f"https://vxtwitter.com/tvid").replace(".mp4","") + for media in range(len(tweetData["mediaURLs"])): + tweetData["mediaURLs"][media] = tweetData["mediaURLs"][media].replace("https://video.twimg.com",f"https://vxtwitter.com/tvid").replace(".mp4","") + return tweetData \ No newline at end of file