/ Published in: Python
"Whatever you do will be insignificant, but it is very important that you do it." --Mahatma Gandhi
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#! /usr/bin/env python # # chirpyt-0.1.py # http://twitter.com/chirpyt # A simple python script to share a link to Twitter stream :-) # ksaver (at identi.ca), Sep 11, 2010. # Public Domain Code. # Not warranty at all. # # In order to work, some requierements must be met: # BeautifulSoup: http://www.crummy.com/software/BeautifulSoup/ # tweepy: http://github.com/joshthecoder/tweepy # # Usage: # ./chirpyt.py auth -To get a pair of oauth keys. # ./chirpyt.py help -To get this help output. # ./chirpyt.py http://example.com -To send a new link. # ./chirpyt.py -Without arguments. # # Elliot, be good. import string import sys import tweepy import urllib import urllib2 from BeautifulSoup import BeautifulSoup as cooksoup # Edit this two lines, and add your own keys. # To get a key pair, run ./chirpyt.py auth. #------------------------------------------------ ACCESS_KEY = 'PASTE_HERE_YOUR_ACCESS_KEY' ACCESS_SECRET = 'PASTE_HERE_YOUR_ACCESS_SECRET' #------------------------------------------------ CONSUMER_KEY = 'D6gfLh3XYuyLrnlP3XYdQw' CONSUMER_SECRET = '7fete8FUruFaJPAl4NFVg3jF2TZDiNUG5FkW2ob0oM' def authorize(): auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth_url = auth.get_authorization_url() print "----------------------------" print "Chirpyt authorization module" print "----------------------------\n" print "Please login to Twitter in your browser,", print "then open this URL and authorize (by clicking 'Allow'):" print auth_url verifier = '' while not verifier: verifier = raw_input("Enter here the numeric PIN: ").strip() auth.get_access_token(verifier) print "Now, copy the next pair of keys, and paste them into the script." print "ACCESS_KEY = '%s'" % auth.access_token.key print "ACCESS_SECRET = '%s'" % auth.access_token.secret def chirpyt(tweet): auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth) api.update_status(tweet) def get_html(url, params): uagent = 'Opera/9.80 (X11; FreeBSD 8.1-RELEASE i386; U; en)\ Presto/2.6.30 Version/10.62' headers = {'User-Agent': uagent} request = urllib2.Request(url, params, headers) return urllib2.urlopen(request).read() def get_title(url): htmldoc = get_html(url, None) soup = cooksoup(htmldoc) return soup.title.string.strip() def short_url(longurl): shortener = 'http://ur1.ca/' webparams = {'longurl': longurl} encparams = urllib.urlencode(webparams) htmldoc = get_html(shortener, encparams) soup = cooksoup(htmldoc) return soup.p.text[string.find(soup.p.text, ':')+1:] def help(): myname = sys.argv[0] helpstring = ''' Usage: ./%s auth -To get a pair of oauth keys. ./%s help -To get this help output. ./%s http://example.com -To tweet a new link. ./%s -Without arguments. ''' % (myname, myname, myname, myname) return helpstring def main(argv): TITMAXLEN = 100 TWTMAXLEN = 140 if len(argv) > 1: if argv[1] == 'auth': authorize() exit() elif argv[1] == 'help': print help() exit() else: url = argv[1] else: url = raw_input("Please enter a url: ") page_title = get_title(url)[:TITMAXLEN] tweet = "%s: %s" % (page_title, url) if len(tweet) > TWTMAXLEN: url = short_url(url) tweet = "%s: %s" % (page_title, url) chirpyt(tweet) print "%s [%s chars]\n" % (tweet, len(tweet)) if __name__ == '__main__': main(sys.argv)
URL: http://twitter.com/chirpyt