Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Last active June 23, 2017 22:27
Show Gist options
  • Save PandaWhoCodes/9407780926bc0841bc36b454461e5338 to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/9407780926bc0841bc36b454461e5338 to your computer and use it in GitHub Desktop.
import json
import urllib.request
def respond(err, res=None):
return {
'statusCode': '400' if err else '200',
'body': err.message if err else json.dumps(res),
'headers': {
'Content-Type': 'application/json',
},
}
def lambda_handler(event, context):
operation = event["context"]['http-method']
if operation == "GET":
searchString = event["params"]["querystring"]["query"]
searchText = searchString.replace(" ", "+")
#formatted the search string to make sure it works with the giphy API
req = urllib.request.Request(
"http://api.giphy.com/v1/gifs/search?q={}&api_key=dc6zaTOxFJmzC".format(searchText))
with urllib.request.urlopen(req) as response:
#Read the response
the_page = response.read()
#loading the response as a json file
#you may use ast too
a = json.loads(the_page)
return respond(None,a["data"][0]["images"]["fixed_height"]["url"])
else:
return respond(ValueError('Unsupported method "{}"'.format(operation)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment