-
-
Save stav/b71d1f643b0a5e25c89991d21cbba74a to your computer and use it in GitHub Desktop.
Querystring prettification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
IFS=$'\n' # Allow spaces and other white spaces. | |
stty -icanon # Disable canonical mode. | |
stty eof ^D | |
python3 ~/bin/qs.py | |
stty icanon # Re-enable canonical mode (assuming it was enabled to begin with). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import ast | |
import sys | |
import pprint | |
import urllib.parse | |
input = sys.stdin.readline() | |
try: | |
# Convert repr of bytes into bytes | |
o = ast.literal_eval(input) | |
if isinstance(o, bytes): | |
o = o.decode() | |
else: | |
o = input.strip() | |
except (ValueError, SyntaxError): | |
o = input.strip() | |
if not isinstance(o, str): | |
print(f'Input {type(o)} {repr(o)} must be a string') | |
exit(1) | |
# o is now a string | |
print('Input has length', len(o)) | |
if len(o) is 0: | |
exit(1) | |
# metal = urllib.parse.unquote(o) | |
metal = o | |
# print(f'Metal {type(metal)} has length of:', len(metal)) | |
if '?' in metal: | |
metal = urllib.parse.urlparse(metal) | |
query = metal.query | |
else: | |
query = metal | |
print('Query has length', len(query)) | |
parsed = urllib.parse.parse_qs(query, keep_blank_values=True) | |
parsed = {k: v[0] if len(v) is 1 else v for k, v in parsed.items()} | |
print(len(parsed.keys()), 'keys') | |
pprint.pprint(parsed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prettify a URI's Querystring