Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

adcrun.ch link shortener

By: GoldenGod on Jan 3rd, 2012  |  syntax: Python  |  size: 3.04 KB  |  views: 82  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # adcrun.ch Link Shortener v 1 by GoldenGod
  2. #
  3. #
  4. # Return short link from adcrun.ch
  5. #
  6. # Get you key and uid from http://adcrun.ch/tools.php?api and put in KEY and
  7. # UID variables (lines 15 and 16)
  8. #
  9. # Usage: python adcrunch.py http://example.com
  10. #
  11. # If you like my script please sing up with my referral: http://adcrun.ch/?r=1370
  12.  
  13. #!/usr/bin/env python
  14. # -*- coding: utf-8 -*-
  15.  
  16. KEY = ''
  17. UID = ''
  18.  
  19.  
  20. import sys
  21. import urllib
  22. import urllib2
  23. import re
  24.  
  25. class adcrunch:
  26.    
  27.     def __init__(self, key, uid):
  28.         self.key = key
  29.         self.uid = uid
  30.        
  31.    
  32.     def short(self, url, advert_type='int'):
  33.         # advert_type = 'banner'
  34.  
  35.         # print url
  36.         api_url = 'http://adcrun.ch/api.php'
  37.         user_agent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1'
  38.         values = ({'key' : self.key,
  39.                    'uid' : self.uid,
  40.                    'adtype' : advert_type})
  41.         headers = {'User-Agent' : user_agent}
  42.  
  43.         data = urllib.urlencode(values)
  44.         # print data
  45.        
  46.         full_url = api_url + '?' + data + '&url=' + url
  47.         # print full_url
  48.         req = urllib2.Request(full_url, headers=headers)
  49.         response = urllib2.urlopen(req)
  50.         source = response.read()
  51.         response.close()
  52.         # print source
  53.         prog = re.compile('(https?://adcrun.ch/(?!favicon)[-a-zA-Z0-9@:%_\+.~#//=]+|error|Please enter a valid URL)')
  54.         short_url = prog.findall(source)
  55.         # print short_url
  56.  
  57.         if short_url[0] == 'error':
  58.             return 'Wrong Key or Id', 1
  59.         elif short_url[0] == 'Please enter a valid URL':
  60.             return 'Please enter a valid URL (did you forget to add "http://"?)', 2
  61.         elif short_url[0] == '':
  62.             return 'Unknown error', 3
  63.         else:
  64.             return short_url[0], 0  # Everything OK
  65.      
  66. if __name__ == '__main__':      
  67.     #Start Program
  68.     print 'adcrun.ch Link Shortener v 1 by GoldenGod\n'
  69.  
  70.     # url = 'http://www.example.com'
  71.     if len(sys.argv) > 1:
  72.         if KEY == '' or UID == '':
  73.             print 'You must set your KEY and UID first (line 15 and 16)'
  74.             print 'you can find it here: http://adcrun.ch/tools.php?api'
  75.             print
  76.            
  77.         url = sys.argv[-1]
  78.        
  79.         ls = adcrunch(KEY, UID)
  80.        
  81.         short_link, status = ls.short(url, advert_type='int')
  82.         # If you want banner set: advert_type='banner'
  83.        
  84.         if status == 0:
  85.             print 'Shortened link:\n', short_link
  86.         else:
  87.             print 'Error:\t', short_link, '\nError code:\t', status
  88.     else:
  89.         print '%s <url>\n\n' % (sys.argv[0])
  90.         print 'You must provide proper url adress (with "http://")'
  91.         print
  92.         print 'Usage:'
  93.         print 'python adcrunch.py http://www.example.com'
  94.         print '\nIf you like my script please sing up with my referral: ' \
  95.             'http://adcrun.ch/?r=1370'
  96.         # raw_input('PRESS ENTER TO EXIT')
  97.         sys.exit()