forked from MR5356/System-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSub_Thread.py
More file actions
99 lines (82 loc) · 2.86 KB
/
Sub_Thread.py
File metadata and controls
99 lines (82 loc) · 2.86 KB
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
from PyQt5.QtCore import QThread, pyqtSignal
import time
import requests
import json
import psutil
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:
# raise FileExistsError
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)