chiark / gitweb /
Expand video URLs
[irc.git] / commands.py
index 38769876bff1ff5a4a730f78f6f95c729952475c..ce6b199589c628117aa137811de023606036d929 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,29 @@ 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:
+            link = medium["media_url_https"]
+            link = re.sub(r"/tweet_video_thumb/(\w+).jpg", r"/tweet_video/\1.mp4", link)
+            tweetText = tweetText.replace(medium["url"], link)
+    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("&lt;","<")
+    tweetText = tweetText.replace("&amp;","&")
+    tweetText = tweetText.encode('UTF-8', 'replace')
+
     stringout = "tweet by %s (%s): %s" %(tweeter_screen,tweeter_name,tweetText)
   except twitter.TwitterError:
     terror = sys.exc_info()