# Path: modules/utils/data.py
# def download_file(url, max_length=20 * 1024 * 1024, headers={}):
#     try:
#         length = requests.head(url).headers.get('content-length', 0)
#         if int(length) > max_length:
#             return
#         r = requests.get(url, timeout=(1, 3), stream=True, headers=headers)
#         ext = url.rsplit('.', 1)[1]
#         file_name = '{}.{}'.format(uuid.uuid4(), ext)
#         with open(file_name, 'wb') as f:
#             shutil.copyfileobj(r.raw, f)
#         return file_name
#     except RequestException:
#         return


import os
import re
import time
import requests
from moviepy.editor import *
from modules.utils.data import download_file



def main(bot, *args, **kwargs):
    """
    webm
    Return random webm from csgoanime.
    See also: img
    """
    last_dt = bot.store.get('webm_dt')
    if last_dt is not None and float(last_dt.decode()) > time.time() - 60:
        return 'Let me get some rest.'
    bot.store.set('webm_dt', time.time())

    chat_id = kwargs.get('chat_id')
    resp = requests.get('http://csgoani.me')
    if resp.status_code != 200:
        return 'Error getting data'

    urls = re.findall(r'http.*\.webm', resp.content.decode())
    print(urls)
    if not urls:
        return 'Error getting data'
    url = urls[0]
    file_id = bot.store.get(url)
    data = {'chat_id': chat_id, 'disable_notification': True}
    if file_id:
        data.update(video=file_id)
        files = None
    else:

# Path: modules/utils/data.py
# def download_file(url, max_length=20 * 1024 * 1024, headers={}):
#     try:
#         length = requests.head(url).headers.get('content-length', 0)
#         if int(length) > max_length:
#             return
#         r = requests.get(url, timeout=(1, 3), stream=True, headers=headers)
#         ext = url.rsplit('.', 1)[1]
#         file_name = '{}.{}'.format(uuid.uuid4(), ext)
#         with open(file_name, 'wb') as f:
#             shutil.copyfileobj(r.raw, f)
#         return file_name
#     except RequestException:
#         return


# Path: modules/user_webm.py
import os
import re
import time
import requests
from moviepy.editor import *
from modules.utils.data import download_file




def main(bot, *args, **kwargs):
    """
    webm
    Return random webm from csgoanime.
    See also: img
    """
    last_dt = bot.store.get('webm_dt')
    if last_dt is not None and float(last_dt.decode()) > time.time() - 60:
        return 'Let me get some rest.'
    bot.store.set('webm_dt', time.time())

    chat_id = kwargs.get('chat_id')
    resp = requests.get('http://csgoani.me')
    if resp.status_code != 200:
        return 'Error getting data'

    urls = re.findall(r'http.*\.webm', resp.content.decode())
    print(urls)
    if not urls:
        return 'Error getting data'
    url = urls[0]
    file_id = bot.store.get(url)
    data = {'chat_id': chat_id, 'disable_notification': True}
    if file_id:
        data.update(video=file_id)
        files = None
    else:

        file_name = download_file(url, headers={'Referer': resp.url})