+
+ 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("<","<")
+ tweetText = tweetText.replace("&","&")
+ tweetText = tweetText.encode('UTF-8', 'replace')
+