Skip to content

Commit

Permalink
Drop python 2.7 support
Browse files Browse the repository at this point in the history
Now it is possible to upload images from files and file-like objects
Image uploader now can return json object instead of image url
  • Loading branch information
mercuree committed Jan 19, 2021
1 parent 70499fe commit 1b340ba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 8 additions & 3 deletions html_telegraph_poster/upload_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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':
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions tests/test_htp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}))
Expand Down

0 comments on commit 1b340ba

Please sign in to comment.