diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..cd12a9d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: python +script: + - python -m compileall +deploy: + provider: pypi + user: imgurops + password: + secure: DBBXzMOm037T4XUmfo0Gu9mAytw2DCYJT8i0KgihKYxS+uslF+dwHf2clBEWDLUE0xkXhqXetq+sNgfshovGKIqZanASYZ/6Zf5ikg10ApgaBidObv2XMYNyuQxL8Gqv9l2tdlWqdUoOJzRBMV2Nh0B3BJ9hG7V5NFMDcfG/qyo= + on: + tags: true + repo: Imgur/imgurpython diff --git a/README.md b/README.md index e38054f..c89b846 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# The imgurpython project is no longer supported. + imgurpython =========== @@ -22,9 +24,7 @@ Our developer documentation can be found [here](https://api.imgur.com/). Community --------- -The best way to reach out to Imgur for API support would be our -[Google Group](https://groups.google.com/forum/#!forum/imgur), [Twitter](https://twitter.com/imgurapi), or via - api@imgur.com. +The best way to reach out to Imgur for API support is emailing us at api@imgur.com. Installation ------------ diff --git a/imgurpython/client.py b/imgurpython/client.py index f247d73..9c41ea6 100644 --- a/imgurpython/client.py +++ b/imgurpython/client.py @@ -17,6 +17,7 @@ from .imgur.models.account_settings import AccountSettings API_URL = 'https://api.imgur.com/' +MASHAPE_URL = 'https://imgur-apiv3.p.mashape.com/' class AuthWrapper(object): @@ -72,10 +73,11 @@ class ImgurClient(object): 'album', 'name', 'title', 'description' } - def __init__(self, client_id, client_secret, access_token=None, refresh_token=None): + def __init__(self, client_id, client_secret, access_token=None, refresh_token=None, mashape_key=None): self.client_id = client_id self.client_secret = client_secret self.auth = None + self.mashape_key = mashape_key if refresh_token is not None: self.auth = AuthWrapper(access_token, refresh_token, client_id, client_secret) @@ -103,20 +105,27 @@ def authorize(self, response, grant_type='pin'): }, True) def prepare_headers(self, force_anon=False): + headers = {} if force_anon or self.auth is None: if self.client_id is None: raise ImgurClientError('Client credentials not found!') else: - return {'Authorization': 'Client-ID %s' % self.get_client_id()} + headers['Authorization'] = 'Client-ID %s' % self.get_client_id() else: - return {'Authorization': 'Bearer %s' % self.auth.get_current_access_token()} + headers['Authorization'] = 'Bearer %s' % self.auth.get_current_access_token() + + if self.mashape_key is not None: + headers['X-Mashape-Key'] = self.mashape_key + + return headers + def make_request(self, method, route, data=None, force_anon=False): method = method.lower() method_to_call = getattr(requests, method) header = self.prepare_headers(force_anon) - url = API_URL + ('3/%s' % route if 'oauth2' not in route else route) + url = (MASHAPE_URL if self.mashape_key is not None else API_URL) + ('3/%s' % route if 'oauth2' not in route else route) if method in ('delete', 'get'): response = method_to_call(url, headers=header, params=data, data=data) @@ -175,15 +184,15 @@ def get_account(self, username): account_data['pro_expiration'], ) - def get_gallery_favorites(self, username): + def get_gallery_favorites(self, username, page=0): self.validate_user_context(username) - gallery_favorites = self.make_request('GET', 'account/%s/gallery_favorites' % username) + gallery_favorites = self.make_request('GET', 'account/%s/gallery_favorites/%d' % (username, page)) return build_gallery_images_and_albums(gallery_favorites) - def get_account_favorites(self, username): + def get_account_favorites(self, username, page=0): self.validate_user_context(username) - favorites = self.make_request('GET', 'account/%s/favorites' % username) + favorites = self.make_request('GET', 'account/%s/favorites/%d' % (username, page)) return build_gallery_images_and_albums(favorites) @@ -571,19 +580,21 @@ def get_image(self, image_id): return Image(image) def upload_from_path(self, path, config=None, anon=True): + with open(path, 'rb') as fd: + self.upload(fd, config, anon) + + def upload(self, fd, config=None, anon=True): if not config: config = dict() - fd = open(path, 'rb') contents = fd.read() b64 = base64.b64encode(contents) - data = { 'image': b64, 'type': 'base64', } - data.update({meta: config[meta] for meta in set(self.allowed_image_fields).intersection(config.keys())}) + return self.make_request('POST', 'upload', data, anon) def upload_from_url(self, url, config=None, anon=True): diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f229360 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests diff --git a/setup.py b/setup.py index 46982b1..300e786 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # http://packaging.python.org/en/latest/tutorial.html#version - version='1.1.5', + version='1.1.7', description='Official Imgur python library with OAuth2 and samples', long_description='',