Return to Snippet

Revision: 31760
at September 12, 2010 13:16 by ksaver

Initial Code
#! /usr/bin/env python
#   A simple python script to share a link to Twitter stream :-)
#   ksaver (at, Sep 11, 2010.
#   Public Domain Code.
#   Not warranty at all.
#   In order to work, some requierements must be met:
#       BeautifulSoup:
#       tweepy:   
#   Usage:
#       ./   auth                -To get a pair of oauth keys.
#       ./   help                -To get this help output.
#       ./  -To send a new link.
#       ./                       -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 ./  auth.

CONSUMER_KEY    = 'D6gfLh3XYuyLrnlP3XYdQw'

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()

    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)

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 = ''
    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 = '''
            ./%s  auth                -To get a pair of oauth keys.
            ./%s  help                -To get this help output.
            ./%s  -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':
        elif argv[1] == 'help':
            print help()
            url = argv[1]            
        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)

    print "%s [%s chars]\n" % (tweet, len(tweet))

if __name__ == '__main__':

Initial URL

Initial Description
"Whatever you do will be insignificant, but it is very important that you do it." --Mahatma Gandhi

Initial Title
chirpyt: Simple Python script to share a link to Twitter

Initial Tags
python, twitter

Initial Language