Last active
November 22, 2017 09:13
-
-
Save blairg23/97d1a23962cb0dd88a799f0a56313cf1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from requests_oauthlib import OAuth2Session | |
from oauthlib.oauth2 import BackendApplicationClient | |
import json | |
CURRENT_VERSION = 'v1' | |
BASE_URL = 'https://api.everypixel.com/' | |
CURRENT_URL = BASE_URL + '/{current_version}'.format(current_version=CURRENT_VERSION) | |
def get_keywords_from_url(api, url): | |
params = {'url': url, 'num_keywords': 10} | |
keywords = api.get(CURRENT_URL + '/keywords', params=params).json() | |
return keywords | |
def get_keywords_from_file(api, filename): | |
with open(filename, 'rb') as infile: | |
data = {'data': infile} | |
response = api.post(CURRENT_URL + '/keywords', files=data) | |
if response.status_code == 200: | |
return response.json() | |
def get_quality_from_url(api, url): | |
params = {'url': url} | |
quality = api.get(CURRENT_URL + '/quality', params=params).json() | |
return quality | |
def get_quality_from_file(api, filename): | |
with open(filename, 'rb') as infile: | |
data = {'data': infile} | |
response = api.post(CURRENT_URL + '/quality', files=data) | |
if response.status_code == 200: | |
return response.json() | |
if __name__ == '__main__': | |
client_id = '<your-client-id>' | |
client_secret = '<your-client-secret>' | |
oauth = OAuth2Session(client=BackendApplicationClient(client_id)) | |
token = oauth.fetch_token(token_url=BASE_URL + '/oauth/token', auth=(client_id, client_secret)) | |
api = OAuth2Session(client_id, token=token) | |
url = 'http://image.everypixel.com/2014.12/67439828186edc79b9be81a4dedea8b03c09a12825b_b.jpg' | |
local_filename = 'image.jpg' | |
keywords = get_keywords_from_url(api=api, url=url) | |
print('keywords from URL ({url}):'.format(url=url)) | |
print(json.dumps(keywords, indent=4)) | |
print('---') | |
print('\n') | |
keywords = get_keywords_from_file(api=api, filename=local_filename) | |
print('keywords from local file ({filename}):'.format(filename=local_filename)) | |
print(json.dumps(keywords, indent=4)) | |
print('---') | |
print('\n') | |
quality = get_quality_from_url(api=api, url=url) | |
print('quality from URL ({url}):'.format(url=url)) | |
print(json.dumps(quality, indent=4)) | |
print('---') | |
print('\n') | |
quality = get_quality_from_file(api=api, filename=local_filename) | |
print('quality from local file ({filename}):'.format(filename=local_filename)) | |
print(json.dumps(quality, indent=4)) | |
print('---') | |
print('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment