forked from 1069989314/fuckTodayStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliteTools.py
209 lines (186 loc) · 6.5 KB
/
liteTools.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import time
import requests
import yaml
import math
import random
import os
class TaskError(Exception):
'''目前(配置/时间/签到情况)不宜完成签到任务'''
pass
class MT:
'''MiscTools'''
@staticmethod
def geoDistance(lon1, lat1, lon2, lat2):
'''两经纬度算距离'''
# 经纬度转换成弧度
lon1, lat1, lon2, lat2 = map(math.radians, [float(
lon1), float(lat1), float(lon2), float(lat2)])
dlon = lon2-lon1
dlat = lat2-lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * \
math.cos(lat2) * math.sin(dlon/2)**2
distance = 2*math.asin(math.sqrt(a))*6371393 # 地球平均半径,6371393m
return distance
class RT:
'''RandomTools'''
default_offset = 50
default_location_round = 6
@staticmethod
def locationOffset(lon, lat, offset=default_offset, round_=default_location_round):
'''经纬度随机偏移(偏移不会累积)
lon——经度
lat——纬度
offset——偏移范围(单位m)
round_——保留位数
'''
if offset == 0:
return (lon, lat)
# 限定函数(经度-180~180,维度-90~90)
def limit(n, a, b):
if n < a:
n = a
if n > b:
n = b
return n
# 弧度=弧长/半径,角度=弧长*180°/π,某地经度所对应的圆半径=cos(|维度|)*地球半径
# ==纬度==
# 偏移大小
latOffset = offset/6371393*(180/math.pi)
# 偏移范围
lat_a = lat-lat % latOffset
lat_a = limit(lat_a, -90, 90)
lat_b = lat+0.99*latOffset-lat % latOffset
lat_b = limit(lat_b, -90, 90)
# 随机偏移
lat = random.uniform(lat_a, lat_b)
# 保留小数
lat = round(lat, round_)
# ==经度==
# 偏移大小(依赖纬度计算)
lonOffset = offset / \
(6371393*math.cos(abs(lat_a/180*math.pi)))*(180/math.pi)
# 偏移范围
lon_a = lon-lon % lonOffset
lon_b = lon+0.99*lonOffset-lon % lonOffset
lon_a = limit(lon_a, -180, 180)
lon_b = limit(lon_b, -180, 180)
# 随机偏移
lon = random.uniform(lon_a, lon_b)
# 保留小数
lon = round(lon, round_)
return (lon, lat)
@staticmethod
def choiceFile(dir):
'''从指定路径(路径列表)中随机选取一个文件路径'''
if type(dir) == list:
dir = random.choice(dir)
if os.path.isfile(dir):
return dir
else:
files = os.listdir(dir)
if len(files) == 0:
raise Exception("路径(%s)指向一个空文件夹" % dir)
return os.path.join(dir, random.choice(files))
@staticmethod
def choiceInList(item):
if type(item) == list:
return random.choice(item)
else:
return item
@staticmethod
def choicePhoto(dir):
'''从指定路径(路径列表)中随机选取一个图片路径'''
if type(dir) == list:
dir = random.choice(dir)
if os.path.isfile(dir):
return dir
else:
files = filter(lambda x: x.endswith('.jpg'), os.listdir(dir))
if len(files) == 0:
raise Exception("路径(%s)指向一个没有图片(.jpg)的文件夹" % dir)
return os.path.join(dir, random.choice(files))
@staticmethod
def randomSleep(a: int, b: int = None):
'''随机暂停一段时间'''
if b == None:
b = a/2
sleepTime = random.randint(a, b)
LL.log(0, f'程序正在暂停({sleepTime})')
time.sleep(sleepTime)
@staticmethod
def genDeviceID(seed):
'''根据种子伪随机生成uuid'''
random.seed(seed, version=2) # 种子设置
def ranHex(x): return ''.join(
random.choices('0123456789ABCDEF', k=x)) # 指定长度随机Hex字符串生成
deviceId = "-".join([ranHex(8), ranHex(4), ranHex(4),
ranHex(4), ranHex(12)]) # 拼合字符串
return deviceId
class DT:
'''DictTools'''
@staticmethod
def loadYml(ymlDir='config.yml'):
with open(ymlDir, 'r', encoding="utf-8") as f:
return yaml.load(f, Loader=yaml.FullLoader)
@staticmethod
def writeYml(item, ymlDir='config.yml'):
with open(ymlDir, 'w', encoding='utf-8') as f:
yaml.dump(item, f, allow_unicode=True)
@staticmethod
def resJsonEncode(res):
'''响应内容的json解析函数(换而言之,就是res.json()的小优化版本)'''
try:
return res.json()
except Exception as e:
raise Exception(
f'响应内容以json格式解析失败({e}),响应内容:\n\n{res.text}')
class LL:
'''LiteLog'''
startTime = time.time()
log_list = []
printLevel = 0
logTypeDisplay = ['debug', 'info', 'warn', 'error', 'critical']
@staticmethod
def formatLog(logType: str, args):
'''返回logItem[时间,类型,内容]'''
string = ''
for item in args:
if type(item) == dict or type(item) == list:
string += yaml.dump(item, allow_unicode=True)+'\n'
else:
string += str(item)+'\n'
return [time.time()-LL.startTime, logType, string]
@staticmethod
def log2FormatStr(logItem):
logType = LL.logTypeDisplay[logItem[1]]
return '|||%s|||%0.3fs|||\n%s' % (logType, logItem[0], logItem[2])
@staticmethod
def log(logType=1, *args):
'''日志函数
logType:int = debug:0|info:1|warn:2|error:3|critical:4'''
if not args:
return
logItem = LL.formatLog(logType, args)
LL.log_list.append(logItem)
if logType >= LL.printLevel:
print(LL.log2FormatStr(logItem))
@staticmethod
def getLog(level=0):
'''获取日志函数'''
string = ''
for item in LL.log_list:
if level <= item[1]:
string += LL.log2FormatStr(item)
return string
@staticmethod
def saveLog(dir, level=0):
'''保存日志函数'''
if type(dir) != str:
return
log = LL.getLog(level)
if not os.path.isdir(dir):
os.makedirs(dir)
dir = os.path.join(dir, time.strftime(
"LOG#t=%Y-%m-%d--%H-%M-%S##.txt", time.localtime()))
with open(dir, 'w', encoding='utf-8') as f:
f.write(log)