This repository has been archived on 2026-08-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
RequestCounter/bin/__init__.py
T

45 lines
1.1 KiB
Python
Raw Normal View History

2022-02-18 19:30:54 +08:00
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/2/17
2022-02-18 19:36:09 +08:00
# @File Name: __init__.py
import os
2022-03-04 18:14:34 +08:00
import time
2022-03-03 21:21:45 +08:00
import requests
2022-02-23 22:03:39 +08:00
from bin.utils.logger import logger
2022-02-18 19:36:09 +08:00
2022-03-04 18:14:34 +08:00
def cost(func):
def wrapper(*args, **kwargs):
logger.info('开始下载')
2022-03-04 18:14:34 +08:00
s = time.time()
execute = func(*args, **kwargs)
e = time.time()
logger.info(f'下载结束, 用时: {round(e - s, 2)} s')
return execute
return wrapper
2022-03-12 19:11:09 +08:00
@cost
def download(url: str) -> None:
"""
单线程下载资源
:param url:
:return:
"""
content = requests.get(url, timeout=10, stream=True).content
with open('./bin/db/data.db', 'wb') as fp:
fp.write(content)
2022-02-23 22:03:39 +08:00
if __name__ != '__main__':
2022-03-02 21:51:23 +08:00
if not os.path.exists('./bin/db/data.db'):
2022-03-03 21:21:45 +08:00
logger.warning('数据库文件不存在, 即将开始下载')
2022-03-12 19:11:09 +08:00
try:
download('https://resource-base.herokuapp.com/download/data.db')
2022-03-12 19:11:09 +08:00
except requests.Timeout:
logger.critical('连接超时')