9

Latest Tweet as A Quote

Posted in Messaging by dozdozdoz 1152 days ago

The following Python script cycles through your Twitter timeline and takes the most recent Tweet that fits certain critera.

It will only show tweets that do not include a @reply, a link or a hashtag. It then displays the Tweet as a Quote, neatly divided over as many lines as is necessary, with all HTML entities replaced by their respective Unicode characters, and the name of the poster appended neatly underneath.

Demo

Demo

Installation.

  1. Get your API details from Twitter. Twitter has a set of documentation that walks you through this process. Fill in the form on the Twitter developer site and you will receive a CONSUMERKEY, CONSUMERSECRET, ACCESSKEY and ACCESSSECRET.

  2. Add these details into the text below where they are requested and copy/paste the script into Notepad.

  3. Save as TweetQuote.py. Make a note of the path.

  4. Load Terminal and type: 'easy_install tweepy' without the quotes. (tweepy powers this script, if this install fails then you may need to install setuptools for Python)

  5. Add the following as a GeekTool shell command

'python /Users..[insert full file path here]../TweetQuote.py'

Script


import tweepy, os, sys, re

# Declare OAuth varibles.
CONSUMER_KEY = '...'
CONSUMER_SECRET = '...'
ACCESS_KEY = '...'
ACCESS_SECRET = '...'
keyfile = "authkeys.dat"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

# Break string into multiple lines
def para(text):
    return reduce(lambda line, word, width=50: '%s%s%s' %(line, ' \n'[(len(line)-line.rfind('\n')-1
    + len(word.split('\n',1)[0]) >= 50)], word), text.split(' '))

# Convert html entities into Unicode
def unescape(text):
    def fixup(m):
        text = m.group(0)
        if text[:2] == "&#":
            try:
                if text[:3] == "&#x":
                    return unichr(int(text[3:-1], 16))
                else:
                    return unichr(int(text[2:-1]))
            except ValueError:
                pass
        else:
            try:
                text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
            except KeyError:
                pass
        return text
    return re.sub("&#?\w+;", fixup, text)

# Script
type = api.home_timeline(count=30)
for result in type:
                if result.text.find('@')==-1 and result.text.find('http://')==-1:
                        twtu = result.user.screen_name
                        twtt = result.text
                        print para(unescape(twtt)) + "\n" + "\n- " + twtu
                        break

Comments

User Avatar
gergh 1149 days ago
ImportError: No module named tweepy... So.. what should I do... :S
User Avatar
GBStudios 1149 days ago
Any chance we could get that background?
User Avatar
rprebel 1136 days ago
found it on wallbase

http://wallbase.cc/wallpaper/521109
User Avatar
yarkot 1141 days ago
It's python: if you don't have a module, then the command: "pip search twipy" ... should confirm you can find it (on the net, in Pypi), and "pip install twipy" will install it on your computer.

There are other ways - Google for "python twipy" for more.
User Avatar
ren7on 1139 days ago
how can you show accented characters like àèìòù?!?!
User Avatar
sakrist 1027 days ago
my twitter time line on ruby http://www.macosxtips.co.uk/geeklets/internet/twitter-timeline-1/

just copy code

Log in to comment or register here.