diff --git a/src/db/__init__.py b/src/db/__init__.py index 7acbd18..0b0c34b 100644 --- a/src/db/__init__.py +++ b/src/db/__init__.py @@ -7,16 +7,19 @@ import os +from urllib.request import urlretrieve from src.config import Config database = Config.database if database == 'sqlite': import sqlite3 as operator - from src.utils.t_download import download if not os.path.exists('./src/db/data.sqlite'): - download('https://static.rtast.cn/data.sqlite') + print('Downloading database file. Please wait...') + file_url = 'https://static.rtast.cn/data.sqlite' + urlretrieve(file_url, './src/db/data.sqlite') + print('Download database file successfully.') from src.db.db import SQLite as Database elif 'redis' in database: import redis as operator diff --git a/src/utils/t_download.py b/src/utils/t_download.py deleted file mode 100644 index cf09716..0000000 --- a/src/utils/t_download.py +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env python3 -# -- coding:utf-8 -- -# @Author: markushammered@gmail.com -# @Development Tool: PyCharm -# @Create Time: 2022/9/3 -# @File Name: t_download.py - - -import threading -import requests - - -class ThreadedDownload(threading.Thread): - def __init__(self, - url: str, - filename: str, - thread_id: int, - start_seek: int, - end_seek: int, - offset: int - ): - super().__init__() - self.url = url - self.filename = filename - self.thread_id = thread_id - self.start_seek = start_seek - self.end_seek = end_seek - self.offset = offset - - def download(self): - resp = requests.get(self.url, headers={'Range': f'Bytes={self.start_seek}-{self.end_seek}'}) - with open('./src/db/data.sqlite', 'rb+') as fp: - fp.seek(self.start_seek) - fp.write(resp.content) - - def run(self) -> None: - self.download() - - -def download(url: str, threads: int = 4) -> None: - filename = url.split('/')[-1] - open('./src/db/data.sqlite', 'wb').close() # create an empty file - head = requests.head(url) - if head.status_code == 301: - head = requests.head(head.headers['Location']) - filesize = int(head.headers['Content-Length']) - offset = filesize // threads - start = 0 - for i in range(threads): - end = offset + start - ThreadedDownload(url, filename, i, start, end, offset).run() - start = end - -