update: 修改了下载数据库的方式

This commit is contained in:
MarkusJoe
2022-10-03 11:08:48 +08:00
parent 01ecd72304
commit 48159a2461
2 files changed
+5 -56

No files matched your search

+5 -2
View File
@@ -7,16 +7,19 @@
import os import os
from urllib.request import urlretrieve
from src.config import Config from src.config import Config
database = Config.database database = Config.database
if database == 'sqlite': if database == 'sqlite':
import sqlite3 as operator import sqlite3 as operator
from src.utils.t_download import download
if not os.path.exists('./src/db/data.sqlite'): 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 from src.db.db import SQLite as Database
elif 'redis' in database: elif 'redis' in database:
import redis as operator import redis as operator
-54
View File
@@ -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