Return to Snippet

Revision: 31965
at September 16, 2010 17:17 by ksaver


Initial Code
#!/ usr/bin/env python
#
#   fun.py
#   short a url using http://fun.ly 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 = 'http://fun.ly/'
    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)

Initial URL
http://identi.ca/ksaver

Initial Description
None.

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

Initial Tags
python

Initial Language
Python