Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ChiChou
Last active June 2, 2016 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChiChou/897e98591f5b9a4c1bd6fb4238bd3bd3 to your computer and use it in GitHub Desktop.
Save ChiChou/897e98591f5b9a4c1bd6fb4238bd3bd3 to your computer and use it in GitHub Desktop.
Dead simple python script to parse Google style search query
#!/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