Last active
June 2, 2016 11:58
-
-
Save ChiChou/897e98591f5b9a4c1bd6fb4238bd3bd3 to your computer and use it in GitHub Desktop.
Dead simple python script to parse Google style search query
This file contains 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 python | |
import shlex | |
def parse_query(query): | |
tokens = shlex.split(query) | |
dsl = {'search': []} | |
key = '' | |
for token in tokens: | |
if token.endswith(':'): | |
key = token[:-1] | |
else: | |
if ':' in token: | |
key, word = token.split(':', 1) | |
dsl[key] = word | |
elif key: | |
dsl[key] = token or '' | |
else: | |
dsl['search'].append(token) | |
key = '' | |
if key: | |
dsl[key] = '' | |
return dsl | |
if __name__ == '__main__': | |
print(parse_query('site:wooyun.org keyword1 author: "White Hat"')) | |
# {'search': ['keyword1'], 'site': 'wooyun.org', 'author': 'White Hat'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment