From 48159a246159fe62e160a97ae44755919717aa10 Mon Sep 17 00:00:00 2001 From: MarkusJoe Date: Mon, 3 Oct 2022 11:08:48 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=95=B0=E6=8D=AE=E5=BA=93=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/__init__.py | 7 ++++-- src/utils/t_download.py | 54 ----------------------------------------- 2 files changed, 5 insertions(+), 56 deletions(-) delete mode 100644 src/utils/t_download.py 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 - -