Skip to content

Instantly share code, notes, and snippets.

@jkishner
Created December 4, 2013 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkishner/7792404 to your computer and use it in GitHub Desktop.
Save jkishner/7792404 to your computer and use it in GitHub Desktop.
import re
import string
import console
import urllib
import sys
import clipboard
import webbrowser
console.clear()
# Count arguments passed to script, if less than 2 run regex against clipboard
numArgs = len(sys.argv)
if numArgs == 2:
redirect = sys.argv[1]
elif numArgs < 2:
redirect = clipboard.get()
# Provided by Peter Hansen on StackOverflow:
# http://stackoverflow.com/questions/1986059/grubers-url-regular-expression-in-python/1986151#1986151
pat = r'\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^%s\s]|/)))'
pat = pat % re.escape(string.punctuation)
match = re.findall(pat, redirect)
if match:
for x in match:
console.show_activity()
# Get the first match without redirects
cleaned = urllib.urlopen(x[0]).geturl()
final = urllib.quote(cleaned, safe='')
clipboard.set(cleaned)
console.hide_activity()
webbrowser.open('texttool://x-callback-url/transform?text=' + final + '&method=replace&find=www&replace=dl&x-success=texttool%3A%2F%2Fx-callback-url%2Ftransform%3Ftext%3D%5B%5Boutput%5D%5D%26method%3Daffix%26prefix%3D!%255B%255D%2528%26suffix%3D%2529')
elif not match:
console.alert('No match found')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment