# Path: modules/utils/data.py
# def prepare_binary_from_url(url, verify=True):
#     try:
#         content = requests.get(url, timeout=(1, 3), verify=verify).content
#     except RequestException:
#         pass
#     else:
#         return BytesIO(content)


import json
import base64
import requests
from datetime import datetime, timedelta
from requests.exceptions import RequestException
from modules.utils.data import prepare_binary_from_url
    headers = {'Authorization': 'Basic {}'.format(auth.decode('utf-8'))}

    # Cache return response to decrease number of requests to the bing api
    if last_query != query:
        # TODO: use `params` here, investigate 'Query is not of type String' error from azure
        try:
            azure_response = requests.get('https://api.datamarket.azure.com/Bing/Search/v1/Image?Market=%27ru-RU%27&Adult=%27Moderate%27&Query=%27{}%27&$format=json&$top=20'.format(query), headers=headers, timeout=(1, 2)).content
        except requests.exceptions.Timeout:
            return response(bot, 'Can not get results', chat_id)
        except RequestException:
            return response(bot, 'Can not get results')
        setattr(bot, 'img_last_response_{}'.format(chat_id), azure_response)
    else:
        azure_response = getattr(bot, 'img_last_response_{}'.format(chat_id))

    try:
        search_data = json.loads(azure_response.decode('utf-8'))
    except ValueError:
        return response(bot, 'Can not get results', chat_id)

    results = search_data.get('d', {}).get('results', [])
    if len(results) >= i + 1:
        while results[i - 1 if i > 1 else i:]:
            setattr(bot, 'img_last_num_{}'.format(chat_id), i)
            if len(results) <= i:
                return response(bot, 'No such images', chat_id)
            url = results[i]['MediaUrl']
            ext = url.rsplit('.', 1)[1]
            if ext.lower() in ('jpg', 'jpeg', 'gif', 'png'):
                file_id = check_store(bot, url)

# Path: modules/utils/data.py
# def prepare_binary_from_url(url, verify=True):
#     try:
#         content = requests.get(url, timeout=(1, 3), verify=verify).content
#     except RequestException:
#         pass
#     else:
#         return BytesIO(content)


# Path: modules/user_img.py
import json
import base64
import requests
from datetime import datetime, timedelta
from requests.exceptions import RequestException
from modules.utils.data import prepare_binary_from_url

    headers = {'Authorization': 'Basic {}'.format(auth.decode('utf-8'))}

    # Cache return response to decrease number of requests to the bing api
    if last_query != query:
        # TODO: use `params` here, investigate 'Query is not of type String' error from azure
        try:
            azure_response = requests.get('https://api.datamarket.azure.com/Bing/Search/v1/Image?Market=%27ru-RU%27&Adult=%27Moderate%27&Query=%27{}%27&$format=json&$top=20'.format(query), headers=headers, timeout=(1, 2)).content
        except requests.exceptions.Timeout:
            return response(bot, 'Can not get results', chat_id)
        except RequestException:
            return response(bot, 'Can not get results')
        setattr(bot, 'img_last_response_{}'.format(chat_id), azure_response)
    else:
        azure_response = getattr(bot, 'img_last_response_{}'.format(chat_id))

    try:
        search_data = json.loads(azure_response.decode('utf-8'))
    except ValueError:
        return response(bot, 'Can not get results', chat_id)

    results = search_data.get('d', {}).get('results', [])
    if len(results) >= i + 1:
        while results[i - 1 if i > 1 else i:]:
            setattr(bot, 'img_last_num_{}'.format(chat_id), i)
            if len(results) <= i:
                return response(bot, 'No such images', chat_id)
            url = results[i]['MediaUrl']
            ext = url.rsplit('.', 1)[1]
            if ext.lower() in ('jpg', 'jpeg', 'gif', 'png'):
                file_id = check_store(bot, url)

                photo = file_id if file_id else prepare_binary_from_url(url)