Tag Archive for funly

Fun.py: Short a url from command line, using fun.ly service

#!/ usr/bin/env python
#
#   fun.py
#   short a url using <a href="http://fun.ly" >http://fun.ly</a> shortener service.
#   ksaver (at identi.ca)
#   Public Domain Code.

import urllib
import urllib2
import sys

from BeautifulSoup import BeautifulSoup as cooksoup

def shorten(longurl):
    uagent    = 'Opera/9.80 (X11; FreeBSD 8.1-RELEASE i386; U; en)
    Presto/2.6.30 Version/10.62'
    headers   = {'User-Agent': uagent}
    shortener = '<a href="http://fun.ly/'" >http://fun.ly/'</a>
    webparams = {'funly': longurl}
    encparams = urllib.urlencode(webparams)
    urlreqst  = urllib2.Request(shortener, encparams, headers)
    htmlpage  = urllib2.urlopen(urlreqst).read()
    soup      = cooksoup(htmlpage)
    shorturl  = soup.findAll('p')[1].text
    return shorturl

def main(argv):
    if len(argv) > 1:
        shorturl = shorten(argv[1])
    else:
        longurl  = raw_input("Give me a URL: ")
        shorturl = shorten(longurl)
        
    print "%s" % shorturl
    
if __name__ == '__main__':
    main(sys.argv)

source