From 94aac51364d05d2f7c7521d781b6b6d381dbd0b0 Mon Sep 17 00:00:00 2001 From: Matthew Vernon Date: Thu, 19 May 2016 21:22:26 +0100 Subject: [PATCH] Expand URLs and unescape &<> in tweets This expands the URLs in tweets (instead of the t.co shorturls that you get currently). Patch by adamb --- commands.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/commands.py b/commands.py index 3876987..ec133f5 100755 --- a/commands.py +++ b/commands.py @@ -1,5 +1,5 @@ # Part of Acrobat. -import string, cPickle, random, urllib, sys, time, re, os, twitter, subprocess, datetime +import string, cPickle, random, urllib, sys, time, re, os, twitter, subprocess, datetime, urlparse from irclib import irc_lower, nm_to_n # query karma @@ -492,7 +492,7 @@ def nsfwify(match): def twitterq(bot,cmd,nick,conn,public,twitapi): if (not urlre.search(cmd)): - bot.automsg(False,nick,"Please use 'twit' only with http URLs") + bot.automsg(False,nick,"Please use 'twit' only with http or https URLs") return urlstring = urlre.search(cmd).group(1) @@ -505,7 +505,6 @@ def getTweet(urlstring,twitapi): tweetID = parts[-1] try: status = twitapi.GetStatus(tweetID) - #print status, type(status), status=={} if status == {}: return "twitapi.GetStatus returned nothing :-(" if status.user == None and status.text == None: @@ -517,6 +516,26 @@ def getTweet(urlstring,twitapi): tweeter_screen = "[not returned]" ; tweeter_name = "[not returned]" tweetText = status.text.encode('UTF-8', 'replace') tweetText = tweetText.replace('\n',' ') + + for medium in status.media: + if "media_url_https" in medium: + tweetText = tweetText.replace(medium["url"], medium["media_url_https"]) + for url in status.urls: + toReplace = url.expanded_url + + # remove tracking utm_ query parameters, for privacy and brevity + # code snippet from https://gist.github.com/lepture/5997883 + rv = urlparse.urlparse(toReplace) + if rv.query: + query = re.sub(r'utm_\w+=[^&]+&?', '', rv.query) + toReplace = '%s://%s%s?%s' % (rv.scheme, rv.hostname, rv.path, query) + + tweetText = tweetText.replace(url.url, toReplace) + + tweetText = tweetText.replace(">",">") + tweetText = tweetText.replace("<","<") + tweetText = tweetText.replace("&","&") + stringout = "tweet by %s (%s): %s" %(tweeter_screen,tweeter_name,tweetText) except twitter.TwitterError: terror = sys.exc_info() -- 2.30.2