/ Published in: Python
None.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/ 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)