Skip to content

Instantly share code, notes, and snippets.

@clamytoe
Last active April 27, 2017 12:49
Show Gist options
  • Save clamytoe/36fa8a8b69966bd1f57fa272461abd45 to your computer and use it in GitHub Desktop.
Save clamytoe/36fa8a8b69966bd1f57fa272461abd45 to your computer and use it in GitHub Desktop.
Removes escaped characters from any quoted string that's passed to it through the command line and prints out its different components.
#!/usr/bin/env python3
import sys
from os import system
from urllib.parse import unquote
def decode(url):
"""Decodes url encoded string"""
return unquote(url)
def break_apart(decoded):
"""Attempts to break apart the url into its different parts"""
url_list = []
if '?' in decoded:
engine, parts = decoded.split('?')
url_list.append(engine)
if '&' in parts:
for part in parts.split('&'):
var_list = part.split('=')
url_list.append(var_list)
return url_list
else:
return decoded
def usage():
"""Usage statement"""
print('usage: {} "url"'.format(__file__.split('/')[-1]))
sys.exit(1)
def main():
"""Run this script from commandline"""
# check and see if we have a string to decode
if len(sys.argv) == 1:
usage()
else:
url = sys.argv[1]
# decode the url string
decoded = decode(url)
# break it apart if we can
parts = break_apart(decoded)
# clean the console
system('cls||clear')
# if we have broken it apart, display the parts
if len(parts) > 1:
print("\n{:>8}: {}".format('ENG', parts.pop(0)))
for var in parts:
print('{:>8}: {}'.format(var[0], var[1]))
print("\n")
else:
# if no parts, just display the decoded string
print(parts)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment