Skip to content

Instantly share code, notes, and snippets.

@openfly
Last active July 26, 2018 19:03
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 openfly/c2d8182fd51c7e6b0f1cd8cf21defc49 to your computer and use it in GitHub Desktop.
Save openfly/c2d8182fd51c7e6b0f1cd8cf21defc49 to your computer and use it in GitHub Desktop.
giphy upload trouble
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
giphy demo
'''
__author__ = 'Matt Joyce'
__email__ = 'matt@nycresistor.com'
__copyright__ = 'Copyright 2016-2018, BLAH'
import argparse
import requests
class NOPKCS():
def __init__(self):
self.blah = 'dummy info'
def GET_query(uri, req_args=None, headers=None):
''' Generic GET query method '''
# GET request methods only require sessionTokens
if headers is None:
headers = {'Content-Type': 'application/json'}
# headers = {'content-type': 'application/octet-stream'}
# HTTP GET query method using requests module
try:
if req_args is None:
response = requests.get(uri,
stream=True,
headers=headers,
verify=True)
else:
response = requests.get(uri + str(req_args),
headers=headers,
# settings.STATICMAP_URL.format(**data),
stream=True,
verify=True)
except requests.exceptions.RequestException as e:
print e
return '500', 'Internal Error in RESTful.GET_query()'
# return the token
return response.status_code, response.text
def POST_query(self, uri, req_args=None, files=None, headers=None,):
''' Generic POST query method '''
# HTTP POST queries require keyManagerTokens and sessionTokens
if files is None:
print 'no attachment specified'
if headers is None:
print 'headers not specified defaulting'
headers = {'Content-Type': 'application/json'}
# HTTP POST query to keymanager authenticate API
try:
response = requests.post(uri,
headers=headers,
data=req_args,
files=files,
verify=True)
except requests.exceptions.RequestException as errstr:
return '500', 'Internal Error in RESTful.POST_query(' + errstr + ')'
# return the token
return response.status_code, response
def main():
''' main loop '''
parser = argparse.ArgumentParser(description='giphy demo')
parser.add_argument(
'gif', type=str,
help='specify gif file')
args = parser.parse_args()
REST = NOPKCS()
gif = args.gif
with open(gif, 'rb') as f:
files = {'file': f}
headers = {'Content-Type': 'application/json; charset=utf-8', 'Accept': 'application/json', 'api_key': 'REDACTED'}
req_args = {'tags': 'example'}
# req_args = {'tags': 'example', 'source_image_url': 'http://i.imgur.com/JGBUyG0.mp4'}
uri = 'https://upload.giphy.com/v1/gifs'
status_code, response = REST.POST_query(uri, req_args, files=files, headers=headers)
print "(%s) : %s" % (str(status_code), response.text)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment