Skip to content

Commit

Permalink
Do not prepare binary data if file_id is known
Browse files Browse the repository at this point in the history
  • Loading branch information
aluminiumgeek committed Dec 18, 2015
1 parent 4084fd2 commit 8e41c5a
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions modules/user_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

from modules.utils.data import prepare_binary_from_url

def check_store(bot, url):
if bot.store:
return bot.store.get(url)


def main(bot, *args):
"""
Expand Down Expand Up @@ -55,22 +59,19 @@ def main(bot, *args):
url = results[i]['MediaUrl']
ext = url.rsplit('.', 1)[1]
if ext.lower() in ('jpg', 'jpeg', 'gif', 'png'):
photo = prepare_binary_from_url(url)
file_id = check_store(bot, url)
photo = file_id if file_id else prepare_binary_from_url(url)
if photo:
break
i += 1
else:
return 'No such images'

data = {'chat_id': bot.chat_id}
file_in_store = False
if bot.store:
file_id = bot.store.get(url)
if file_id:
data.update(photo=file_id)
file_in_store = True
files = None
if not file_in_store:
if file_id:
data.update(photo=file_id)
files = None
else:
files = {'photo': ('file.{}'.format(ext), photo, results[i]['ContentType'])}

response = bot.call(
Expand All @@ -79,7 +80,7 @@ def main(bot, *args):
data=data,
files=files
)
if response and response.get('photo') and not file_in_store and bot.store:
if response and response.get('photo') and not file_id and bot.store:
bot.store.set(url, response['photo'][-1]['file_id'])
else:
return 'No such images'
Expand Down

0 comments on commit 8e41c5a

Please sign in to comment.