Skip to content

Commit

Permalink
Native Discord multi image embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanpdx committed Jan 17, 2024
1 parent 68bb83f commit e427459
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
15 changes: 14 additions & 1 deletion templates/image.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@

<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="{{ user }} (@{{ screenName }}) {% if verified %}☑️{% else %}{% endif %}" />
<meta name="twitter:image" content="{{ pic }}" />
<meta name="twitter:image" content="{{ pic[0] }}" />

{% if pic[1] %}
<meta name="twitter:image" content="{{ pic[1] }}" />
{% endif %}

{% if pic[2] %}
<meta name="twitter:image" content="{{ pic[2] }}" />
{% endif %}

{% if pic[3] %}
<meta name="twitter:image" content="{{ pic[3] }}" />
{% endif %}

<meta name="twitter:creator" content="@{{ user }}" />

<meta property="og:description" content="{{ desc }}" />
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<meta property="og:video:height" content="480" />
<meta property="og:title" content="{{ user }}" />
<!--<meta property="og:description" content="{{ desc }}" />!-->
<meta property="og:image" content="{{ pic }}" />
<meta property="og:image" content="{{ pic[0] }}" />

<!--!-->
<link rel="alternate" href="{{ url }}/oembed.json?desc={{ urlUser }}&user={{ urlDesc }}&link={{ urlUserLink }}" type="application/json+oembed" title="{{ user }}">
Expand Down
28 changes: 28 additions & 0 deletions templates/redirect_script.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script>
function androidOrIOS() {
const userAgent = navigator.userAgent;
if(/android/i.test(userAgent)){
return 'android';
}
if(/iPad|iPhone|iPod/i.test(userAgent)){
return 'ios';
}
return 'unknown';
}

function redirect() {
const os = androidOrIOS();
if(os === 'android'){
window.location.href = 'twitter://status?status_id=1674915987789950982';
window.location.href = 'https://twitter.com/pdxdylan/status/1674915987789950982'
}
if(os === 'ios'){
window.location.href = 'twitter://status?id=1674915987789950982';
setTimeout(() => {
window.location.href = 'https://twitter.com/pdxdylan/status/1674915987789950982'
}, 100);
}
}

redirect();
</script>
2 changes: 1 addition & 1 deletion templates/text.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<meta name="twitter:card" content="tweet" />
<meta name="twitter:title" content="{{ user }} (@{{ screenName }}) {% if verified %}☑️{% else %}{% endif %}" />
<meta name="twitter:image" content="{{ pic }}" />
<meta name="twitter:image" content="{{ pic[0] }}" />
<meta name="twitter:creator" content="@{{ user }}" />

<meta property="og:description" content="{{ desc }}" />
Expand Down
2 changes: 1 addition & 1 deletion templates/video.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<meta property="og:video:width" content="{{ videoSize['width'] }}" />
<meta property="og:video:height" content="{{ videoSize['height'] }}" />
<meta name="twitter:title" content="{{ user }} (@{{ screenName }}) {% if verified %}☑️{% else %}{% endif %}" />
<meta property="og:image" content="{{ pic }}" />
<meta property="og:image" content="{{ pic[0] }}" />
<meta property="og:description" content="{{ desc }}" />

<link rel="alternate" href="{{ url }}/oembed.json?desc={{ urlUser }}&user={{ urlDesc }}&link={{ urlUserLink }}&ttype=video&provider={{ appname }}" type="application/json+oembed" title="{{ user }}">
Expand Down
36 changes: 23 additions & 13 deletions twitfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import html
app = Flask(__name__)
CORS(app)

user_agent=""
pathregex = re.compile("\\w{1,15}\\/(status|statuses)\\/(\\d{2,20})")
generate_embed_user_agents = [
"facebookexternalhit/1.1",
Expand Down Expand Up @@ -58,6 +58,7 @@ def robots():

@app.route('/') # If the useragent is discord, return the embed, if not, redirect to configured repo directly
def default():
global user_agent
user_agent = request.headers.get('user-agent')
if isValidUserAgent(user_agent):
return message("TwitFix is an attempt to fix twitter video embeds in discord! created by Robin Universe :)\n\n💖\n\nClick me to be redirected to the repo!")
Expand All @@ -75,7 +76,7 @@ def oembedend():

@app.route('/<path:sub_path>') # Default endpoint used by everything
def twitfix(sub_path):

global user_agent
user_agent = request.headers.get('user-agent')
match = pathregex.search(sub_path)

Expand All @@ -92,7 +93,7 @@ def twitfix(sub_path):
if e is not None:
return message(msgs.failedToScan+msgs.failedToScanExtra+e)
return message(msgs.failedToScan)
return make_cached_vnf_response(vnf,getTemplate("rawvideo.html",vnf,"","",clean,"","","",""))
return make_cached_vnf_response(vnf,getTemplate("rawvideo.html",vnf,"",[],clean,"","","",""))
elif request.url.endswith(".txt") or request.url.endswith("%2Etxt"):
twitter_url = "https://twitter.com/" + sub_path

Expand All @@ -106,7 +107,7 @@ def twitfix(sub_path):
if e is not None:
return abort(500,"Failed to scan tweet: "+e)
return abort(500,"Failed to scan tweet")
return make_content_type_response(getTemplate("txt.html",vnf,vnf["description"],"",clean,"","","",""),"text/plain")
return make_content_type_response(getTemplate("txt.html",vnf,vnf["description"],[],clean,"","","",""),"text/plain")
elif request.url.endswith(".zip") or request.url.endswith("%2Ezip"): # for certain types of archival software (i.e Hydrus)
twitter_url = "https://twitter.com/" + sub_path

Expand Down Expand Up @@ -310,6 +311,7 @@ def twitfix(sub_path):

@app.route('/dir/<path:sub_path>') # Try to return a direct link to the MP4 on twitters servers
def dir(sub_path):
global user_agent
user_agent = request.headers.get('user-agent')
url = sub_path
match = pathregex.search(url)
Expand Down Expand Up @@ -616,7 +618,7 @@ def message(text):
repo = config['config']['repo'],
url = config['config']['url'] )

def getTemplate(template,vnf,desc,image,video_link,color,urlDesc,urlUser,urlLink,appNameSuffix="",embedVNF=None):
def getTemplate(template,vnf,desc,images,video_link,color,urlDesc,urlUser,urlLink,appNameSuffix="",embedVNF=None):
if (embedVNF is None):
embedVNF = vnf
if ('width' in embedVNF['size'] and 'height' in embedVNF['size']):
Expand All @@ -633,7 +635,7 @@ def getTemplate(template,vnf,desc,image,video_link,color,urlDesc,urlUser,urlLink
pfp = vnf['pfp'],
vidurl = embedVNF['url'],
desc = desc,
pic = image,
pic = images,
user = vnf['uploader'],
video_link = vnf,
color = color,
Expand Down Expand Up @@ -705,7 +707,7 @@ def embed(video_link, vnf, image):
if vnf['nsfw'] == True:
color = "#800020" # Red

return make_cached_vnf_response(vnf,getTemplate(template,vnf,desc,image,video_link,color,urlDesc,urlUser,urlLink,appNamePost,embedVNF))
return make_cached_vnf_response(vnf,getTemplate(template,vnf,desc,[image],video_link,color,urlDesc,urlUser,urlLink,appNamePost,embedVNF))


def embedCombined(video_link):
Expand Down Expand Up @@ -745,17 +747,25 @@ def embedCombinedVnf(video_link,vnf):
if qrt is not None:
desc=msgs.formatEmbedDesc(vnf['type'],desc,qrt,pollDisplay,likeDisplay)

host = config['config']['url']
image = f"{host}/rendercombined.jpg?imgs="
for i in range(0,int(vnf['images'][4])):
image = image + vnf['images'][i] + ","
image = image[:-1] # Remove last comma
suffix=""
if 'Discord' in user_agent:
images = []
for i in range(0,int(vnf['images'][4])):
images.append(vnf['images'][i])
else:
host = config['config']['url']
image = f"{host}/rendercombined.jpg?imgs="
for i in range(0,int(vnf['images'][4])):
image = image + vnf['images'][i] + ","
image = image[:-1] # Remove last comma
images=[image]
suffix=" - View original tweet for full quality"

color = "#7FFFD4" # Green

if vnf['nsfw'] == True:
color = "#800020" # Red
return make_cached_vnf_response(vnf,getTemplate('image.html',vnf,desc,image,video_link,color,urlDesc,urlUser,urlLink,appNameSuffix=" - View original tweet for full quality"))
return make_cached_vnf_response(vnf,getTemplate('image.html',vnf,desc,images,video_link,color,urlDesc,urlUser,urlLink,appNameSuffix=suffix))


def getPollObject(card):
Expand Down

2 comments on commit e427459

@lunaynx
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What version does this need? I'm on 212 on iOS with no updates available and it doesn't work, it only shows the first image.

@dylanpdx
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexiayaa Seems to work fine on desktop and android, I wonder if it's an iOS app bug on discord's end.
Will probably revert this until that's fixed

Please sign in to comment.