def upload_to_imgur(image_path, album_id): fd = open(image_path, 'rb') contents = fd.read() b64 = base64.b64encode(contents) data = {'image': b64, 'type': 'base64', 'album': album_id} fd.close() url = "https://imgur-apiv3.p.rapidapi.com/3/image" headers = client.prepare_headers() # Set Auth Bearer header headers['x-rapidapi-host'] = RAPID_API_HOST headers['x-rapidapi-key'] = RAPID_API_KEY headers['content-type'] = "application/x-www-form-urlencoded" response = requests.request("POST", url, data=data, headers=headers) print(response.text) # Rate-limit check if response.status_code == 429: raise ImgurClientRateLimitError() try: response_data = response.json() except Exception as e: print(e) raise ImgurClientError('JSON decoding of response failed.') if 'data' in response_data and isinstance( response_data['data'], dict) and 'error' in response_data['data']: raise ImgurClientError(response_data['data']['error'], response.status_code) return response_data['data'] if 'data' in response_data else response_data
def test_imgur_does_not_exist_error(self, img_mock): img_mock.get_image.side_effect = ImgurClientError(status_code=404, error_message='error') ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) ie.extract_content() failed_post = ie.failed_extract_posts[0] self.assertTrue('Content does not exist' in failed_post.status) self.assertFalse(len(ie.failed_extracts_to_save) > 0)
def test_imgur_over_capacity_error(self, img_mock): img_mock.get_image.side_effect = ImgurClientError(status_code=500, error_message='Over capacity') ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) ie.extract_content() failed_post = ie.failed_extract_posts[0] self.assertTrue('over capacity' in failed_post.status) self.assertTrue(len(ie.failed_extracts_to_save) > 0)
def test_multiple_imgur_rate_limit_exceeded_with_timeout_dict( self, img_mock): img_mock.get_image.side_effect = ImgurClientError( status_code=429, error_message='error') img_mock.credits = {'UserRemaining': None} increment = Const.TIMEOUT_INCREMENT ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) ie.extract_content() self.assertEqual(ExtractorUtils.time_limit_dict[type(ie).__name__], increment) increment += Const.TIMEOUT_INCREMENT ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) ie.extract_content() self.assertEqual(ExtractorUtils.time_limit_dict[type(ie).__name__], increment) increment += Const.TIMEOUT_INCREMENT ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) ie.extract_content() self.assertEqual(ExtractorUtils.time_limit_dict[type(ie).__name__], increment)
def test_imgur_client_connection_error_unknown(self, img_mock, util_mock): img_mock.side_effect = ImgurClientError(status_code=900, error_message='Unknown') ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) failed_post = ie.failed_extract_posts[0] self.assertTrue('Unknown imgur connection error' in failed_post.status) self.assertTrue(len(ie.failed_extracts_to_save) > 0)
def test_imgur_failed_to_locate_general(self, img_mock): img_mock.get_image.side_effect = ImgurClientError(status_code=403, error_message='error') img_mock.credits = {'ClientRemaining': 900} ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) ie.extract_content() failed_post = ie.failed_extract_posts[0] self.assertTrue('Failed to locate content' in failed_post.status) self.assertFalse(len(ie.failed_extracts_to_save) > 0)
def test_imgur_no_credit_error(self, img_mock): img_mock.get_image.side_effect = ImgurClientError(status_code=403, error_message='error') img_mock.credits = {'ClientRemaining': 0} ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) ie.extract_content() failed_post = ie.failed_extract_posts[0] self.assertTrue('Not enough imgur credits to extract post' in failed_post.status) self.assertTrue(len(ie.failed_extracts_to_save) > 0)
def test_imgur_rate_limit_exceeded_credit_dict_is_null(self, img_mock): img_mock.get_image.side_effect = ImgurClientError(status_code=429, error_message='error') img_mock.credits = {'UserRemaining': None} ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) ie.extract_content() failed_post = ie.failed_extract_posts[0] self.assertTrue('rate limit exceeded' in failed_post.status) self.assertTrue(len(ie.failed_extracts_to_save) > 0)
def test_imgur_rate_limit_exceeded_error_no_remaining_credits(self, img_mock): TIME = 600000 img_mock.get_image.side_effect = ImgurClientError(status_code=429, error_message='error') img_mock.credits = {'UserRemaining': 0, 'UserReset': TIME} ie = ImgurExtractor(self.get_single_post(), MockObjects.get_blank_user()) ie.extract_content() failed_post = ie.failed_extract_posts[0] self.assertTrue('Out of user credits' in failed_post.status) self.assertTrue(len(ie.failed_extracts_to_save) > 0) self.assertTrue(ImgurUtils.credit_time_limit == TIME)
def refresh(self): data = { 'refresh_token': self.refresh_token, 'client_id': self.client_id, 'client_secret': self.client_secret, 'grant_type': 'refresh_token' } url = API_URL + 'oauth2/token' response = requests.post(url, data=data) if response.status_code != 200: raise ImgurClientError('Error refreshing access token!', response.status_code) response_data = response.json() self.current_access_token = response_data['access_token'] CredentialsLoader.set_credential('imgur', 'access_token', response_data['access_token'])