-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
418 lines (364 loc) · 15.2 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import datetime
import json
import os
import random
import string
from typing import List
import requests
import config.config as config
import alist.alist as alist
from mail.mail import create_one_mail, get_new_mail_code
import time
import logging
from pikpak.pikpak_super import HandleSuper, PikPakSuper
from proxy_ip import pop_prxy_pikpak
from rclone import PikPakJsonData, PikPakRclone, RCloneManager
from tools import set_def_callback
logger = logging.getLogger("main")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
class BasePikpakData(PikPakSuper):
name = None
disabled = False
def __init__(self, mail: str = None, pd: str = None, name=None, disabled: bool = False):
super().__init__(mail, pd)
self.name = name
self.disabled = disabled
class SingletonMeta(type):
"""自定义元类,用于创建单例类"""
_instances = {} # 用于存储每个类的单例实例
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
class Singleton(metaclass=SingletonMeta):
"""单例基类,使用 SingletonMeta 元类"""
pass
class ManagerPikPak(Singleton):
opation_index: int = -1
pikpak_go_list: List[BasePikpakData] = []
def __init__(self) -> None:
self.opation_index = 0
def change_opation_2(self, pikpak_data: BasePikpakData):
"""
替换操作的pikpak为次pikpak
"""
try:
self.opation_index = self.pikpak_go_list.index(pikpak_data)
except:
for pikpak in self.pikpak_go_list:
if pikpak.name == pikpak_data.name:
self.opation_index = self.pikpak_go_list.index(pikpak)
break
def change_opation_storage_name_2(self, storage_name: str):
"""
替换操作的pikpak为此名字的存储
"""
for pikpak in self.pikpak_go_list:
if pikpak.name == storage_name:
self.opation_index = self.pikpak_go_list.index(pikpak)
return
raise Exception(f"替换操作的pikpak:{storage_name}失败")
class ManagerAlistPikpak(ManagerPikPak, alist.Alist):
def __init__(self):
alist.Alist.__init__(self)
self.saveToNowConif()
for pikpak_data in self.get_all_pikpak_storage():
# if not pikpak_data.get("disabled"):
pikpak_go = BasePikpakData(
mail=pikpak_data.get("username"),
pd=pikpak_data.get("password"),
name=pikpak_data.get("name"),
disabled=pikpak_data.get("disabled"),
)
self.pikpak_go_list.append(pikpak_go)
self.opation_index = 0
# 直接pop一个Alsit中的一个pikpak登陆
def get_opation_pikpak(self) -> BasePikpakData:
return self.pikpak_go_list[self.opation_index]
def update_opation_pikpak_go(self, pikpak_go: BasePikpakData):
alist_storage = None
for data in self.get_storage_list().get('content'):
if data.get("mount_path")[1:] == self.get_opation_pikpak().name:
alist_storage = data
break
remark_str = alist_storage.get("remark")
if remark_str == "":
remark_str = '{}'
remark_json = json.loads(remark_str,)
share = remark_json.get("share")
if share:
# 拥有分享地址 直接分享
pass
else:
# 没有分享地址 开始分享opatins Pikpak
share = self.get_opation_pikpak().start_share_self_files()
remark_json["share"] = share
share_task = pikpak_go.save_share(share.get("share_id"))
time.sleep(10) # 等待10秒保证分享保存成功
addition = json.loads(alist_storage.get("addition"))
old_username = addition.get('username')
old_password = addition.get('password')
pikpak_user = remark_json.get("pikpak_user")
if not old_username and pikpak_user:
old_username = pikpak_user.get('username')
old_password = pikpak_user.get('password')
logger.info(
f"更新Alist中的pikpak\npath:{self.get_opation_pikpak().name}\n原账户:{old_username}\n原密码{old_password}")
addition["username"] = pikpak_go.mail
addition["password"] = pikpak_go.pd
addition['refresh_token'] = pikpak_go.refresh_token
addition['platform'] = "web"
remark_json['pikpak_user'] = {
'username': pikpak_go.mail,
'password': pikpak_go.pd,
}
alist_storage["addition"] = json.dumps(addition)
alist_storage['remark'] = json.dumps(
remark_json, ensure_ascii=False, indent=4)
logger.debug(alist_storage)
self.update_storage(alist_storage)
class ManagerRclonePikpak(ManagerPikPak, RCloneManager):
pass
# def __init__(self) -> None:
# ManagerPikPak.__init__(self)
# RCloneManager.__init__(self)
# pass
# def pop_not_vip_pikpak(self) -> BasePikpakData:
# self.opation_pikpak_go = None
# ManagerPikPak.pop_not_vip_pikpak(self)
# if self.opation_index >= len(self.json_config):
# return None
# pikpak_rclone: PikPakRclone = self.conifg_2_pikpak_rclone(
# self.json_config[self.opation_index])
# # 尝试直接重rclone获取pikpak的vip状态
# rclone_service_info = pikpak_rclone.get_info()
# if rclone_service_info:
# if rclone_service_info.get("VIPType") == "novip":
# self.opation_pikpak_go = BasePikpakData(
# pikpak_rclone.user, pikpak_rclone.password, name=pikpak_rclone.remote)
# else:
# opation_pikpak_go = BasePikpakData(
# mail=pikpak_rclone.user, pd=pikpak_rclone.password, name=pikpak_rclone.remote)
# if opation_pikpak_go.get_vip_day_time_left() <= 0:
# self.opation_pikpak_go = opation_pikpak_go
# if self.opation_pikpak_go:
# return self.opation_pikpak_go
# else:
# return self.pop_not_vip_pikpak()
# def save_pikpak_2(self, pikpak_go: BasePikpakData):
# if self.opation_pikpak_go.mail == pikpak_go.mail:
# logger.info(f"保存pikpak rclone中的账户和现在的账户时同一个这里不做修改")
# return
# data = self.conifg_2_pikpak_rclone[self.opation_index]
# data["pikpak_user"] = pikpak_go.mail
# data["pikpak_password"] = pikpak_go.pd
# logger.debug(self.conifg_2_pikpak_rclone)
# self.save_config()
def radom_password():
chars = string.ascii_letters+string.digits
# 得出的结果中字符会有重复的
return ''.join([random.choice(chars) for i in range(random.randint(8, 11))])
def get_proxy():
logger.info("获取代理地址中。。。。。")
ip, proxy_type = pop_prxy_pikpak()
logger.info(f"获取到的代理:{ip}")
return ip, proxy_type
def pikpakdata_2_pikpakdata(old_pikpak: BasePikpakData, new_pikpak: BasePikpakData):
"""
旧账户的资源复制到新账户中
"""
if new_pikpak.get_vip_day_time_left() > 0:
share = old_pikpak.start_share_self_files()
logger.info(
f"分享原账户:\nemail: {old_pikpak.mail}\npd: {old_pikpak.pd}\n分享代码是: {share}")
time.sleep(10)
share_id = share.get("share_id", None)
if not share_id:
raise Exception("分享错误")
new_pikpak.save_share(share_id)
logger.info(f"保存原账户的资源到新账户:\n{new_pikpak.mail}\n{new_pikpak.pd}")
else:
raise Exception("新账户没有vip")
def change_all_pikpak():
"""
注册新的pikpak替换原来的pikpak
"""
alistPikpak: ManagerPikPak = ManagerAlistPikpak()
for pikpak_go in alistPikpak.pikpak_go_list:
if pikpak_go.disabled:
continue
error = None
pikpak: BasePikpakData = None
for count in range(3):
try:
if not pikpak:
handler = HandleSuper(
get_token=config.get_captcha_callback(),
get_mailcode=config.get_email_verification_code_callback(),
email_address=create_one_mail,
get_password=radom_password,
get_proxy=get_proxy,
)
pikpak = BasePikpakData.create(handler)
else:
logger.info("pikpak 已经注册成功 但是后续报错 这里继续使用pikpak")
time.sleep(60)
pikpak.try_get_vip()
# pikpakdata_2_pikpakdata(pikpak_go, pikpak)
alistPikpak.change_opation_2(pikpak_go)
alistPikpak.update_opation_pikpak_go(pikpak)
logger.info(f"替换原账户的alit或者rclone中")
pikpak = None
error = None
break
except Exception as e:
error = e
if error:
raise error
logger.info('注册新的pikpak替换原来的pikpak over')
def check_all_pikpak_vip():
"""运行所有的pikpak账户检测
"""
logger.info("开始执行系统中的会员状态检测")
alistPikpak: ManagerPikPak = ManagerAlistPikpak()
for pikpak_go in alistPikpak.pikpak_go_list:
logger.info(f"正在整理的pikpak\n {pikpak_go.mail}")
pikpak_go.set_proxy(get_proxy())
if pikpak_go.try_get_vip():
vip_day = pikpak_go.get_vip_day_time_left()
logger.info(f"尝试获取vip成功 当前vip剩余天数{vip_day}")
continue
handler = HandleSuper(
get_token=config.get_captcha_callback(),
get_mailcode=config.get_email_verification_code_callback(),
email_address=create_one_mail,
get_password=radom_password,
get_proxy=get_proxy,
)
pikpak: BasePikpakData = BasePikpakData.create(handler)
time.sleep(60)
pikpak.try_get_vip()
# pikpakdata_2_pikpakdata(pikpak_go, pikpak)
alistPikpak.change_opation_2(pikpak_go)
alistPikpak.update_opation_pikpak_go(pikpak)
logger.info(f"替换原账户的alit或者rclone中")
logger.info("Over")
def 替换Alist储存库(email, pd, name):
pikpak = BasePikpakData(email, pd, name)
if pikpak.try_get_vip():
alist = ManagerAlistPikpak()
alist.change_opation_storage_name_2(name)
alist.update_opation_pikpak_go(pikpak)
else:
raise Exception("传入的账户没有vip")
def 刷新PikPakToken(alist_storage):
name = alist_storage.get('name')
logger.info(f"开始刷新{name}的pikpak_token")
alist = ManagerAlistPikpak()
storage_data = next(x for x in alist.get_storage_list().get(
'content') if x.get("mount_path")[1:] == name)
pikpak = next(x for x in alist.pikpak_go_list if x.name == name)
pikpak.login_out()
pikpak.login()
addition = json.loads(storage_data.get("addition"))
old_token = addition.get('refresh_token')
logger.debug(f"pikpak原token:{old_token}")
logger.debug(f"pikpak新token:{pikpak.refresh_token}")
addition['refresh_token'] = pikpak.refresh_token
storage_data["addition"] = json.dumps(addition)
logger.debug(storage_data)
alist.update_storage(storage_data)
def 所有Alist的储存库() -> List[BasePikpakData]:
logger.info("开始获取本地所有的配置")
base_pikpak: ManagerPikPak = ManagerAlistPikpak()
return base_pikpak.get_all_pikpak_storage()
def 激活存储库vip(alist_storage) -> BasePikpakData:
base_pikpak: ManagerPikPak = ManagerAlistPikpak()
base_pikpak.change_opation_storage_name_2(alist_storage.get('name'))
pikpak_ = base_pikpak.get_opation_pikpak()
# pikpak_.login()
# pikpak_.set_proxy(get_proxy())
try:
if pikpak_.try_get_vip():
return pikpak_
except Exception as e:
if str(e).startswith("网络连接错误"):
logger.error("网络连接错误 重新获取新代理")
pikpak_.set_proxy(get_proxy())
pikpak_.save_self()
激活存储库vip(alist_storage)
else:
raise e
return 注册新号激活(alist_storage)
def 注册新号激活(alist_storage) -> BasePikpakData:
logger.info(f"正在整理的存储\n {alist_storage.get('name')}")
# if isTryGetVip pikpak_go.try_get_vip():
# vip_day = pikpak_go.get_vip_day_time_left()
# logger.info(f"尝试获取vip成功 当前vip剩余天数{vip_day}")
# return
handler = HandleSuper(
get_token=config.get_captcha_callback(),
get_mailcode=config.get_email_verification_code_callback(),
email_address=create_one_mail,
get_password=radom_password,
get_proxy=get_proxy,
)
pikpak: BasePikpakData = BasePikpakData.create(handler)
time.sleep(60)
try:
pikpak.try_get_vip()
except Exception as e:
if str(e).startswith("网络连接错误"):
logger.error("网络连接错误 重新获取新代理")
pikpak.set_proxy(get_proxy())
pikpak.save_self()
pikpak.try_get_vip()
# pikpakdata_2_pikpakdata(pikpak_go, pikpak)
ManagerAlistPikpak().change_opation_storage_name_2(alist_storage.get('name'))
ManagerAlistPikpak().update_opation_pikpak_go(pikpak)
logger.info(f"替换原账户的alit或者rclone中")
return pikpak
def copye_list_2_rclone_config():
"""复制alist的配置到rclone的配置json配置中
"""
alist_server = alist.Alist()
rclone_manager = ManagerRclonePikpak()
# alist_server.saveToNowConif()
rclone_configs: List[PikPakJsonData] = []
for _alist in alist_server.get_storage_list()["content"]:
if _alist.get("driver") == "PikPak":
alist_mount_path = _alist.get("mount_path")[1:]
addition = json.loads(_alist.get("addition"))
rclone_json_data = PikPakJsonData({
"remote": "pikpak_" + alist_mount_path,
"pikpak_user": addition.get("username"),
"pikpak_password": addition.get("password"),
"mout_path": os.path.join(config.rclone_mount, alist_mount_path)
})
rclone_configs.append(rclone_json_data)
logger.debug(rclone_configs)
rclone_manager.json_config = rclone_configs
rclone_manager.save_config()
def 注册并填写邀请(邀请码: str = ""):
if not 邀请码 or 邀请码 == "":
logger.error("邀请码不正确")
logger.error(f"注册并邀请中{邀请码}")
handler = HandleSuper(
get_token=config.get_captcha_callback(),
get_mailcode=config.get_email_verification_code_callback(),
email_address=create_one_mail,
get_password=radom_password,
get_proxy=get_proxy,
)
pikpak: BasePikpakData = BasePikpakData.create(handler)
time.sleep(60)
pikpak.set_activation_code(邀请码)
if __name__ == "__main__":
set_def_callback()
os.environ['TWOCAPTCHA_KEY'] = config.twocapctha_api
os.environ['RAPIDAPI_KEY'] = config.mail_api
# change_all_pikpak()
注册并填写邀请("85147854")