Skip to content

Commit

Permalink
Add support for article previews, fixes #200
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanpdx committed Sep 15, 2024
1 parent 93960b3 commit 8e17044
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
18 changes: 17 additions & 1 deletion twitfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ def renderTextTweetEmbed(tweetData,appnameSuffix=""):
color=config['config']['color']
)

def renderArticleTweetEmbed(tweetData,appnameSuffix=""):
articlePreview=tweetData['article']["title"]+"\n\n"+tweetData['article']["preview_text"]+"…"
embedDesc = msgs.formatEmbedDesc("Image",articlePreview,None,None)
return render_template("image.html",
tweet=tweetData,
pic=[tweetData['article']["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=msgs.formatProvider(config['config']['appname']+appnameSuffix,tweetData),
color=config['config']['color']
)

@app.route('/robots.txt')
def robots():
return "User-agent: *\nDisallow: /"
Expand Down Expand Up @@ -262,7 +276,9 @@ def twitfix(sub_path):
return render_template("rawvideo.html",media=media)
else: # full embed
embedTweetData = determineEmbedTweet(tweetData)
if not embedTweetData['hasMedia']:
if "article" in embedTweetData and embedTweetData["article"] is not None:
return renderArticleTweetEmbed(tweetData," - See original tweet for full article")
elif not embedTweetData['hasMedia']:
return renderTextTweetEmbed(tweetData)
elif embedTweetData['allSameType'] and embedTweetData['media_extended'][0]['type'] == "image" and embedIndex == -1 and embedTweetData['combinedMediaUrl'] != None:
return renderImageTweetEmbed(tweetData,embedTweetData['combinedMediaUrl'],appnameSuffix=" - See original tweet for full quality")
Expand Down
16 changes: 16 additions & 0 deletions vxApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def getApiResponse(tweet,include_txt=False,include_rtf=False):
hashtags=[]
communityNote=None
oldTweetVersion = False
tweetArticle=None
#editedTweet=False
try:
if "birdwatch_pivot" in tweet:
Expand Down Expand Up @@ -96,6 +97,20 @@ def getApiResponse(tweet,include_txt=False,include_rtf=False):
media.append(vidUrl)
media_extended.append({"url":vidUrl,"type":"video","size":{"width":width,"height":height}})

if "article" in tweet:
try:
result = tweet["article"]["article_results"]["result"]
apiArticle = {
"title": result["title"],
"preview_text": result["preview_text"],
"image": None
}
if "cover_media" in result and "media_info" in result["cover_media"]:
apiArticle["image"] = result["cover_media"]["media_info"]["original_img_url"]
tweetArticle = apiArticle
except:
pass

#include_txt = request.args.get("include_txt", "false")
#include_rtf = request.args.get("include_rtf", "false") # for certain types of archival software (i.e Hydrus)

Expand Down Expand Up @@ -199,6 +214,7 @@ def getApiResponse(tweet,include_txt=False,include_rtf=False):
"hasMedia": len(media) > 0,
"combinedMediaUrl": combinedMediaUrl,
"pollData": pollData,
"article": tweetArticle,
}
try:
apiObject["date_epoch"] = int(datetime.strptime(tweetL["created_at"], "%a %b %d %H:%M:%S %z %Y").timestamp())
Expand Down
Loading

0 comments on commit 8e17044

Please sign in to comment.