Shorten URLs in Python with CoinURL


SUBMITTED BY: Guest

DATE: April 17, 2013, 11:41 p.m.

FORMAT: Python

SIZE: 1.6 kB

HITS: 1324

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # __ ___ ___ __ __
  4. # |__) | |__ |__ / \ / \
  5. # | | |___ | \__/ \__/
  6. #
  7. # - - - - - - - - - - - - - -
  8. # Title : Shorten URLs in Python with CoinURL
  9. # Site : http://piefoo.webs.com
  10. # - - - - - - - - - - - - - -
  11. # You need a CoinURL account for this to work!
  12. # You can get one here:
  13. # https://coinurl.com/index.php?ref=21f7ecbf9977416d430ac1ea7d26e005
  14. # - - - - - - - - - - - - - -
  15. import urllib
  16. import urllib2
  17. def get_coinurl(uuid, url):
  18. url = urllib2.quote(url.encode("utf8"))
  19. result = urllib.urlopen('https://coinurl.com/api.php?uuid={0}&url={1}'.format(uuid, url)).read()
  20. if result != 'error':
  21. return result
  22. # You can find your UUID @ https://coinurl.com/profile.php
  23. uuid = 'UUID Here'
  24. # Shorten only one URL
  25. url = raw_input('URL to shorten: ')
  26. coinurl = get_coinurl(uuid, url)
  27. if coinurl:
  28. print 'Success:', coinurl
  29. else:
  30. print 'Failed:', url
  31. # Shorten a list of URLs in a file
  32. '''
  33. with open('urls_to_shorten.txt') as f:
  34. coinurls = ''
  35. for line in f.readlines():
  36. url = line.strip()
  37. coinurl = get_coinurl(uuid, url)
  38. if coinurl:
  39. print 'Success:', coinurl
  40. coinurls += coinurl+'\n'
  41. else:
  42. print 'Failed:', url
  43. # Saves all CoinURLs
  44. with open('output.txt', 'w') as f:
  45. f.write(coinurls)
  46. '''

comments powered by Disqus