diff --git a/.travis.yml b/.travis.yml index e07b73a..3e7793e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: python python: - - "2.7" - "3.6" - "3.7" - "3.8" + - "3.9" # command to install dependencies install: - pip install -r requirements.txt diff --git a/html_telegraph_poster/upload_images.py b/html_telegraph_poster/upload_images.py index aaccabb..475114f 100644 --- a/html_telegraph_poster/upload_images.py +++ b/html_telegraph_poster/upload_images.py @@ -37,9 +37,12 @@ def _get_mimetype_from_response_headers(headers): return '' -def upload_image(file_name_or_url, user_agent='Python_telegraph_poster/0.1'): +def upload_image(file_name_or_url, user_agent='Python_telegraph_poster/0.1', return_json=False): - if re.match(r'^https?://', file_name_or_url, flags=re.IGNORECASE): + if hasattr(file_name_or_url, 'read') and hasattr(file_name_or_url, 'name'): + img = file_name_or_url + img_content_type = mimetypes.guess_type(file_name_or_url.name)[0] + elif re.match(r'^https?://', file_name_or_url, flags=re.IGNORECASE): img = requests.get(file_name_or_url, headers={'User-Agent': user_agent}) if img.status_code != 200 or 'Content-Type' not in img.headers: @@ -73,7 +76,9 @@ def upload_image(file_name_or_url, user_agent='Python_telegraph_poster/0.1'): if json_response.status_code == requests.codes.ok and json_response.content: json_response = json_response.json() - if type(json_response) is list and len(json_response): + if return_json: + return json_response + elif type(json_response) is list and len(json_response): return 'src' in json_response[0] and base_url + json_response[0]['src'] or '' elif type(json_response) is dict: if json_response.get('error') == 'File type invalid': diff --git a/setup.py b/setup.py index acb677b..0ddda41 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = fh.read() setup(name='html_telegraph_poster', - version='0.2.31', + version='0.3.0', description='Posts your html to telegra.ph blogging service', long_description=long_description, long_description_content_type="text/markdown", diff --git a/tests/test_htp.py b/tests/test_htp.py index 6d838f0..2f7621e 100644 --- a/tests/test_htp.py +++ b/tests/test_htp.py @@ -10,6 +10,22 @@ def test_upload(self): telegraph_url = upload_image('http://httpbin.org/image/jpeg') self.assertIn('http://telegra.ph/file/', telegraph_url) + def test_upload_return_json(self): + telegraph_response = upload_image('http://httpbin.org/image/jpeg', return_json=True) + self.assertIsInstance(telegraph_response, list) + self.assertTrue(len(telegraph_response), 1) + self.assertNotEqual(telegraph_response[0].get('src'), None) + + def test_upload_from_file_object(self): + b64file = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=' + from io import BytesIO + from base64 import b64decode + with BytesIO(b64decode(b64file)) as file_to_upload: + # emulate filename for mimetype detection + file_to_upload.name = 'sample.png' + telegraph_image_url = upload_image(file_to_upload) + self.assertIn('http://telegra.ph/file/', telegraph_image_url) + def test_mime_headers(self): self.assertEqual('image/jpeg', _get_mimetype_from_response_headers({'Content-Type': 'image/jpg'})) self.assertEqual('image/jpeg', _get_mimetype_from_response_headers({'Content-Type': 'image/jpeg'}))