Making Safe URLs

December 31, 2011 by Gabe | [mmd] |

I need to do this often enough, I wrote a macro with some Python for it. The macro converts a url with invalid characters (such as spaces) to a safe encoded url. Importantly, the forward slashes, colons, question marks, equal signs, quotes and hashes that are necessary to pass queries and parameters are untouched by the conversion.

A common example for me is as follows. Select a URL that has bad actors in it:$$shomer fucking?.jpg

Run the macro and the idealized URL is inserted in its place:

Here’s the Keyboard Maestro Macro:

Here’s the Python script that does all of the work:

#!/usr/bin/env python
from urlparse import urlparse, urlunparse
import urllib
import os

# Get values from Keyboard Maestro workflow
kmVarClipBoard = os.getenv('KMVAR_tempClip')

# Create a parsed url. See:
o = urlparse(kmVarClipBoard)

# Remove unsupported characters. '/' is a safe character
newPath = urllib.quote(o.path, '/')
newParams =  urllib.quote(o.params, '/')

# Create a new tuple with our new quoted path as a replacement
n = o[0:2] + (newPath ,) + o[3:]

# Reassemble the entire url
baseUrl = urlunparse(n)
print baseUrl

Of course, if this is passed something that is not a url, then it will throw an exception. If that's a concern, then check one or more of the url parse values to make sure there is a domain.