Jake.codes

TweetExpand

Wednesday, January 29, 2014

I wanted to make a way to grab the tweet from Twitter’s tweet direct url. So I could take this: https://twitter.com/JakeBilbrey/status/417359969214668802 and turn it into this: Steam is like my family: how old are you again? - Jake Bilbrey (@JakeBilbrey) December 29, 2013

If you run in to trouble, you may need to install the python requests package. Try pip install requests and if that doesn’t work try easy_install requests in the Terminal app on your Mac.

OS X Service

Download and open to install then you can assign a keyboard shortcut for use when you have highlighted a link to a tweet.

Python Script

# TweetExpand by Jake Bilbrey
#
# For Drafts
# pythonista://TweetExpand?action=run&argv=[[draft]]&argv=drafts
import sys
import re
import htmlentitydefs
import webbrowser
import requests
import json

# Unescape function from 
# http://effbot.org/zone/re-sub.htm#unescape-html
def unescape(text):
    def fixup(m):
        text = m.group(0)
        if text[:2] == '&#':
            # character reference
            try:
                if text[:3] == '&#x':
                    return unichr(int(text[3:-1], 16))
                else:
                    return unichr(int(text[2:-1]))
            except ValueError:
                pass
        else:
            # named entity
            try:
                text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
            except KeyError:
                pass
        return text # leave as is
    return re.sub('&#?\w+;', fixup, text)

def getTweetID(url):
    if ('twitter' in url) and ('/status/' in url):
        status = re.sub('^http(s)??://twitter.com/\\S+/status/', '', url).strip()
        return status
    else:
        print 'Oops! Not a direct link to a Twitter status.'

def getTweet(status):
    url = 'https://api.twitter.com/1/statuses/oembed.json?id=' + status
    oembed = json.loads(requests.get(url).text)
    if oembed.get('html') and len(oembed.get('html')) > 0:
        html = oembed.get('html')
        html = re.sub('—', ' -', html)
        html = re.sub('<.+?>', '', html)
        html = unescape(html)
        html = re.sub('(\\n|\\r)', ' ', html)
        html = html.encode('ascii', 'ignore')
        html = html.strip()
        return html
    else:
        print 'Oops! I could not find the tweet'

# Main
try:
    try:
        if sys.argv[2] == 'drafts':
            webbrowser.open('drafts://x-callback-url/create?text=' + getTweet(getTweetID(sys.argv[1])))
    except:
        print getTweet(getTweetID(sys.argv[1]))
except:
    print 'No input'

TextExpander Snipper

Create a new Shell Script snippet and paste this in:

#!/usr/bin/env python
# TweetExpand by Jake Bilbrey
#
import sys
import re
import htmlentitydefs
import webbrowser
import requests
import json

# Unescape function from 
# http://effbot.org/zone/re-sub.htm#unescape-html
def unescape(text):
    def fixup(m):
        text = m.group(0)
        if text[:2] == '&#':
            # character reference
            try:
                if text[:3] == '&#x':
                    return unichr(int(text[3:-1], 16))
                else:
                    return unichr(int(text[2:-1]))
            except ValueError:
                pass
        else:
            # named entity
            try:
                text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
            except KeyError:
                pass
        return text # leave as is
    return re.sub('&#?\w+;', fixup, text)

def getTweetID(url):
    if ('twitter' in url) and ('/status/' in url):
        status = re.sub('^http(s)??://twitter.com/\\S+/status/', '', url).strip()
        return status
    else:
        print 'Oops! Not a direct link to a Twitter status.'

def getTweet(status):
    url = 'https://api.twitter.com/1/statuses/oembed.json?id=' + status
    oembed = json.loads(requests.get(url).text)
    if oembed.get('html') and len(oembed.get('html')) > 0:
        html = oembed.get('html')
        html = re.sub('&mdash;', ' -', html)
        html = re.sub('<.+?>', '', html)
        html = unescape(html)
        html = re.sub('(\\n|\\r)', ' ', html)
        html = html.encode('ascii', 'ignore')
        html = html.strip()
        return html
    else:
        print 'Oops! I could not find the tweet'

# Main
try:
    sys.stdout.write(getTweet(getTweetID("%filltext:name=banglink:default=direct link to tweet%")))
except:
    sys.stdout.write('No input')