More bugfixes

This commit is contained in:
Dylan 2024-05-02 19:42:21 +01:00
parent 0858f6263b
commit 2d51ac1671
6 changed files with 40 additions and 16 deletions

10
msgs.py
View File

@ -22,11 +22,11 @@ def genQrtDisplay(qrt):
def genPollDisplay(poll):
pctSplit=10
output="\n\n"
for choice in poll["choices"]:
output+=choice["text"]+"\n"+(""*int(choice["percent"]/pctSplit)) +" "+str(choice["percent"])+"%\n"
for choice in poll["options"]:
output+=choice["name"]+"\n"+(""*int(choice["percent"]/pctSplit)) +" "+str(choice["percent"])+"%\n"
return output
def formatEmbedDesc(type,body,qrt,pollDisplay,likesDisplay):
def formatEmbedDesc(type,body,qrt,pollData,likesDisplay):
# Trim the embed description to 248 characters, prioritizing poll and likes
qrtType=None
@ -36,8 +36,10 @@ def formatEmbedDesc(type,body,qrt,pollDisplay,likesDisplay):
limit = videoDescLimit if type=="Text" or type=="Video" or (qrt!=None and (qrtType=="Text" or qrtType=="Video")) else tweetDescLimit
output = ""
if pollDisplay==None:
if pollData==None:
pollDisplay=""
else:
pollDisplay=genPollDisplay(pollData)
if qrt!=None:

View File

@ -18,5 +18,5 @@
<meta property="og:image" content="{{ media['thumbnail_url'] }}" />
<meta property="og:description" content="{{ desc }}" />
<link rel="alternate" href="{{ host }}/oembed.json?desc={{ urlUser }}&user={{ desc }}&link={{ tweetLink }}&ttype=video&provider={{ appname }}" type="application/json+oembed" title="{{ tweet['user_name'] }}">
<link rel="alternate" href="{{ host }}/oembed.json?desc={{ urlUser }}&user={{ urlEncodedDesc }}&link={{ tweetLink }}&ttype=video&provider={{ appname }}" type="application/json+oembed" title="{{ tweet['user_name'] }}">
<meta http-equiv="refresh" content="0; url = {{ tweetLink }}" /> {% endblock %} {% block body %} Redirecting you to the tweet in a moment. <a href="{{ tweetLink }}">Or click here.</a> {% endblock %}

View File

@ -158,4 +158,15 @@ def test_embed_mixedMedia():
assert img1 in str(resp.data) and img2 not in str(resp.data)
resp = client.get(testMixedMediaTweet.replace("https://twitter.com","")+"/2",headers={"User-Agent":"test"})
assert resp.status_code==200
assert img1 not in str(resp.data) and img2 in str(resp.data)
assert img1 not in str(resp.data) and img2 in str(resp.data)
def test_embed_poll():
resp = client.get(testPollTweet.replace("https://twitter.com",""),headers={"User-Agent":"test"})
assert resp.status_code==200
assert "Mean one thing" in str(resp.data)
assert "78.82%" in str(resp.data)
def test_embed_stripLastUrl():
resp = client.get(testMediaTweet.replace("https://twitter.com",""),headers={"User-Agent":"test"})
assert resp.status_code==200
assert "HgLAbiXw2E" not in str(resp.data)

View File

@ -4,7 +4,8 @@ from flask_cors import CORS
import re
import os
import combineImg
from io import BytesIO, StringIO
from io import BytesIO
import urllib
import msgs
import twExtract as twExtract
from configHandler import config
@ -54,38 +55,38 @@ def message(text):
def renderImageTweetEmbed(tweetData,image,appnameSuffix=""):
qrt = tweetData['qrt']
pollData = None
embedDesc = msgs.formatEmbedDesc("Image",tweetData['text'],qrt,pollData,msgs.genLikesDisplay(tweetData))
embedDesc = msgs.formatEmbedDesc("Image",tweetData['text'],qrt,tweetData['pollData'],msgs.genLikesDisplay(tweetData))
return render_template("image.html",
tweet=tweetData,
pic=[image],
host=config['config']['url'],
desc=embedDesc,
urlEncodedDesc=urllib.parse.quote(embedDesc),
tweetLink=f'https://twitter.com/{tweetData["user_screen_name"]}/status/{tweetData["tweetID"]}',
appname=config['config']['appname']+appnameSuffix,
)
def renderVideoTweetEmbed(tweetData,mediaInfo,appnameSuffix=""):
qrt = tweetData['qrt']
pollData = None
embedDesc = msgs.formatEmbedDesc("Video",tweetData['text'],qrt,pollData,msgs.genLikesDisplay(tweetData))
embedDesc = msgs.formatEmbedDesc("Video",tweetData['text'],qrt,tweetData['pollData'],msgs.genLikesDisplay(tweetData))
return render_template("video.html",
tweet=tweetData,
media=mediaInfo,
host=config['config']['url'],
desc=embedDesc,
urlEncodedDesc=urllib.parse.quote(embedDesc),
tweetLink=f'https://twitter.com/{tweetData["user_screen_name"]}/status/{tweetData["tweetID"]}',
appname=config['config']['appname']+appnameSuffix,
)
def renderTextTweetEmbed(tweetData,appnameSuffix=""):
qrt = tweetData['qrt']
pollData = None
embedDesc = msgs.formatEmbedDesc("Text",tweetData['text'],qrt,pollData,msgs.genLikesDisplay(tweetData))
embedDesc = msgs.formatEmbedDesc("Text",tweetData['text'],qrt,tweetData['pollData'],msgs.genLikesDisplay(tweetData))
return render_template("text.html",
tweet=tweetData,
host=config['config']['url'],
desc=embedDesc,
urlEncodedDesc=urllib.parse.quote(embedDesc),
tweetLink=f'https://twitter.com/{tweetData["user_screen_name"]}/status/{tweetData["tweetID"]}',
appname=config['config']['appname']+appnameSuffix,
)

View File

@ -1,11 +1,19 @@
import re
pathregex = re.compile("\\w{1,15}\\/(status|statuses)\\/(\\d{2,20})")
endTCOregex = re.compile("(^.*?) +https:\/\/t.co\/.*?$")
def getTweetIdFromUrl(url):
match = pathregex.search(url)
if match is not None:
return match.group(2)
else:
return None
return None
def stripEndTCO(text):
# remove t.co links at the end of a string
match = endTCOregex.search(text)
if match is not None:
return match.group(1)
else:
return text

View File

@ -1,6 +1,7 @@
import html
from datetime import datetime
from configHandler import config
from utils import stripEndTCO
def getApiResponse(tweet,include_txt=False,include_zip=False):
tweetL = tweet["legacy"]
@ -89,6 +90,7 @@ def getApiResponse(tweet,include_txt=False,include_zip=False):
twText = twText.replace(eurl["url"], "")
else:
twText = twText.replace(eurl["url"],eurl["expanded_url"])
twText = stripEndTCO(twText)
# check if all extended media are the same type
sameMedia = False
@ -102,7 +104,7 @@ def getApiResponse(tweet,include_txt=False,include_zip=False):
sameMedia = True
combinedMediaUrl = None
if sameMedia and media_extended[0]["type"] == "image" and len(media) > 1:
if len(media_extended) > 0 and sameMedia and media_extended[0]["type"] == "image" and len(media) > 1:
host=config['config']['url']
combinedMediaUrl = f'{host}/rendercombined.jpg?imgs='
for i in media: