chiark / gitweb /
Expand URLs and unescape &<> in tweets
authorMatthew Vernon <matthewv@chiark.greenend.org.uk>
Thu, 19 May 2016 20:22:26 +0000 (21:22 +0100)
committerMatthew Vernon <matthewv@chiark.greenend.org.uk>
Thu, 19 May 2016 20:22:26 +0000 (21:22 +0100)
This expands the URLs in tweets (instead of the t.co shorturls that
you get currently).

Patch by adamb

commands.py

index 38769876bff1ff5a4a730f78f6f95c729952475c..ec133f581869e488cd492a733582c24676997a75 100755 (executable)
@@ -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("&gt;",">")
+    tweetText = tweetText.replace("&lt;","<")
+    tweetText = tweetText.replace("&amp;","&")
+
     stringout = "tweet by %s (%s): %s" %(tweeter_screen,tweeter_name,tweetText)
   except twitter.TwitterError:
     terror = sys.exc_info()