Posted By

ksaver on 09/16/10


Tagged

python shorten funly


Versions (?)

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


 / Published in: Python
 

URL: http://identi.ca/ksaver

None.

  1. #!/ usr/bin/env python
  2. #
  3. # fun.py
  4. # short a url using http://fun.ly shortener service.
  5. # ksaver (at identi.ca)
  6. # Public Domain Code.
  7.  
  8. import urllib
  9. import urllib2
  10. import sys
  11.  
  12. from BeautifulSoup import BeautifulSoup as cooksoup
  13.  
  14. def shorten(longurl):
  15. uagent = 'Opera/9.80 (X11; FreeBSD 8.1-RELEASE i386; U; en)\
  16. Presto/2.6.30 Version/10.62'
  17. headers = {'User-Agent': uagent}
  18. shortener = 'http://fun.ly/'
  19. webparams = {'funly': longurl}
  20. encparams = urllib.urlencode(webparams)
  21. urlreqst = urllib2.Request(shortener, encparams, headers)
  22. htmlpage = urllib2.urlopen(urlreqst).read()
  23. soup = cooksoup(htmlpage)
  24. shorturl = soup.findAll('p')[1].text
  25. return shorturl
  26.  
  27. def main(argv):
  28. if len(argv) > 1:
  29. shorturl = shorten(argv[1])
  30. else:
  31. longurl = raw_input("Give me a URL: ")
  32. shorturl = shorten(longurl)
  33.  
  34. print "%s" % shorturl
  35.  
  36. if __name__ == '__main__':
  37. main(sys.argv)

Report this snippet  

You need to login to post a comment.