htnawsaj

ExpandURL

Aug 7th, 2013
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # This script is modified version of the one available at
  3. # http://www.macstories.net/tutorials/resolve-short-urls-with-pythonista-on-ios/
  4. #-------------------------------------------------------------------------------
  5. #!/usr/bin/env python
  6.  
  7. import re
  8. import string
  9. import urllib
  10. import sys
  11. import webbrowser
  12. import android
  13.  
  14. droid = android.Android()
  15.  
  16. redirect = droid.getClipboard()
  17.  
  18. # Provided by Peter Hansen on StackOverflow:
  19. # http://stackoverflow.com/questions/1986059/grubers-url-regular-expression-in-python/1986151#1986151
  20. pat = r'\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^%s\s]|/)))'
  21. pat = pat % re.escape(string.punctuation)
  22.  
  23.  
  24. match = re.findall(pat, redirect.result)
  25.  
  26. if match:
  27.     for x in match:
  28.         # Get the first match without redirects
  29.         cleaned = urllib.urlopen(x[0]).geturl()
  30.         droid.setClipboard(cleaned)
  31.  
  32. elif not match:
  33.       print('No match found')
Advertisement
Add Comment
Please, Sign In to add comment