-
Notifications
You must be signed in to change notification settings - Fork 20
/
Sub_Thread.py
136 lines (113 loc) · 4.17 KB
/
Sub_Thread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from PyQt5.QtCore import QThread, pyqtSignal
import time
import requests
import json
import os
import subprocess
import soft_cfg
def cache_data(name, msm=None):
if msm:
with open(f"{name}.json", "w") as f:
f.write(json.dumps(msm))
else:
with open(f"{name}.json", "r") as f:
return f.readline()
class Time_Thread(QThread):
thread_signal = pyqtSignal(str)
def __init__(self):
super().__init__()
def run(self):
while True:
result = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
self.thread_signal.emit(result)
time.sleep(soft_cfg.time_thread)
class Source_Thread(QThread):
thread_signal = pyqtSignal(dict)
def __init__(self):
super().__init__()
def run(self):
while True:
try:
if soft_cfg.debug:
result = soft_cfg.source_api
else:
result = requests.get(soft_cfg.source_api_url, timeout=15).text
cache_data("source", json.loads(result))
except Exception as e:
print(f"source result info: {e}, try to use cache")
try:
result = cache_data("source")
except Exception as e:
print(f"cache source error: {e}")
result = soft_cfg.source_api
result = json.loads(result)
self.thread_signal.emit(result)
time.sleep(soft_cfg.source_thread)
class Article_Thread(QThread):
thread_signal = pyqtSignal(dict)
def __init__(self):
super().__init__()
def run(self):
while True:
try:
if soft_cfg.debug:
result = soft_cfg.article_api
else:
result = requests.get(soft_cfg.article_api_url, timeout=3).text
cache_data("article", json.loads(result))
except Exception as e:
print(f"article result info: {e}, try to use cache")
try:
result = cache_data("article")
except Exception as e:
print(f"cache article error: {e}")
result = soft_cfg.article_api
result = json.loads(result)
self.thread_signal.emit(result)
time.sleep(soft_cfg.article_thread)
class Update_Thread(QThread):
display_signal = pyqtSignal(dict)
def __init__(self, auto):
super().__init__()
self.auto = auto
def run(self):
try:
result = json.loads(requests.get(soft_cfg.update_url, timeout=3).text)
except:
result = {"Version": soft_cfg.version}
if self.auto:
result["auto"] = True
else:
result["auto"] = False
self.display_signal.emit(result)
class Downloader_Thread(QThread):
display_signal = pyqtSignal(bool)
def __init__(self):
super().__init__()
def run(self):
if os.path.exists('Download'):
pass
else:
os.mkdir('Download')
if not os.path.exists('aria2.session'):
open('aria2.session', 'w')
req = json.loads(requests.get(soft_cfg.tracks_url).text)
tracks = req['data']
with open("aria2.conf", "r", encoding='utf-8') as f:
conf = f.readlines()
del conf[-1]
conf.append(f"bt-tracker={tracks}")
with open("aria2.conf", "w", encoding='utf-8') as f:
for i in conf:
f.write(f"{i}")
# 隐藏命令提示行运行
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
subprocess.Popen("aria2c.exe --conf-path=aria2.conf", startupinfo=startupinfo)
def stop(self):
# 隐藏命令提示行运行
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
subprocess.Popen("taskkill /F /IM aria2c.exe", startupinfo=startupinfo)