Skip to content

Instantly share code, notes, and snippets.

@adam-phillipps
Created October 21, 2018 19:38
Show Gist options
  • Save adam-phillipps/8b508d3443d2a8ee0a34684f91c840ae to your computer and use it in GitHub Desktop.
Save adam-phillipps/8b508d3443d2a8ee0a34684f91c840ae to your computer and use it in GitHub Desktop.
import json
import webrecon.gcse as gcse
def handler(context, event):
return build_response(gsce.search(event))
def build_response(body=[], status=200):
response_object = {
"isBase64Encoded": "false",
"statusCode": status,
"body": json.dumps(body),
"headers": {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods":
"DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT",
"Access-Control-Allow-Headers":
"Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token"}}
return response_object
# Format the query and response objects into something a bit easier to process
# with computermabobs.
def search(q, key, cx, **kwargs):
"""
execute a robust search against google cse and tokanize (process) the results into something we can use
**kwargs is extra searching arguments you pass in as key-value pairs.
"""
api = build("customsearch", "v1", developerKey=key, kwargs)
res = api.cse().list(q=q, cx=cx, **kwargs).execute() # add in extra searching params for smash needs
return map(_tokanize, res)
def _tokanize(doc):
"""
clean out any useless data and create a plain-word document of the body.
put the plain-word document along with other things like price into a list of result dictionaries
"""
tokens = doc # do actual work to get required information from response
return doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment