Skip to content

Instantly share code, notes, and snippets.

Created Jan 23, 2014
What would you like to do?
import re
import string
import console
import urllib
import sys
import clipboard
import webbrowser
# 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:
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:
# Get the first match without redirects
cleaned = urllib.urlopen(x[0]).geturl()
# Get direct image link
cleaned = cleaned.replace('', '')
title = console.input_alert('Image text', 'Type the image alt text below.')
final = '![' + title + '](' + cleaned + ')'
print 'Done.'
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