Advertisement
htnawsaj

Bitly.py

Aug 13th, 2013
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Import the modules
  4.  
  5. import bitly_api
  6. import sys
  7. import re
  8. import string
  9. import android
  10.  
  11. # Define your API information
  12.  
  13. API_USER = "User Name"
  14. API_KEY = "Api Key"
  15.  
  16. b = bitly_api.Connection(API_USER, API_KEY)
  17.  
  18. droid = android.Android()
  19. clip = droid.getClipboard()
  20.  
  21.  
  22. # Provided by Peter Hansen on StackOverflow:
  23. # http://stackoverflow.com/questions/1986059/grubers-url-regular-expression-in-python/1986151#1986151
  24. pat = r'\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^%s\s]|/)))'
  25. pat = pat % re.escape(string.punctuation)
  26.  
  27.  
  28. match = re.findall(pat, clip.result)
  29.  
  30. if match:
  31.     for x in match:
  32.         # Get the first match without redirects
  33.         longurl = x[0]
  34.         response = b.shorten(longurl)
  35.         droid.setClipboard(response['url'])
  36. elif not match:
  37.       print('No match found')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement