forked from veda0307/scripts-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activeId.py
155 lines (143 loc) · 5.74 KB
/
activeId.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
import requests, re, json, os
def GET_TUAN_ID():
m = 5
try:
while m:
# 基于 178 大佬的 activeId.py 改写,感谢大佬!
url = 'https://wqsd.jd.com/pingou/dream_factory/index.html'
r = requests.get(url)
if r.ok:
resp = r.text
TUAN_ACTIVEID = re.findall('((?<=.)activeId.*?==)(?:.*?)("start".*?(?=,))(?:.*?)("end".*?(?=,))', re.sub('%3D%3D', '==', ''.join(re.findall('({"width".*?})', resp))))[0][0].split("=",1)[1]
return TUAN_ACTIVEID
else:
m -= 1
except:
n = 5
try:
while n:
# 参考 https://github.com/qqwas/JD_Diy/blob/master/jbot/tuan.py 使用的链接
url = 'https://cdn.jsdelivr.net/gh/gitupdate/updateTeam@master/shareCodes/jd_updateFactoryTuanId.json'
r = requests.get(url)
if r.ok:
TUAN_ACTIVEID = r.json()['tuanActiveId']
return TUAN_ACTIVEID
else:
n -= 1
except:
return False
def TUAN_ACTIVEID():
TUAN_ACTIVEID = GET_TUAN_ID()
if TUAN_ACTIVEID:
msg = f"京喜工厂团ID:{TUAN_ACTIVEID}\n"
with open(f"{env}/config/config.sh", 'r', encoding='utf-8') as f1:
configs = f1.read()
if "export TUAN_ACTIVEID=" in configs:
if TUAN_ACTIVEID in configs:
msg += "京喜工厂团ID相同,取消替换"
return msg
configs = re.sub(f'TUAN_ACTIVEID=(\"|\').*(\"|\')', f'TUAN_ACTIVEID="{TUAN_ACTIVEID}"', configs)
if TUAN_ACTIVEID in configs:
msg += "替换京喜工厂团ID成功"
else:
msg += "替换京喜工厂团ID失败,请手动替换"
else:
msg += "程序没有找到设置京喜工厂团的变量值,将自动添加进配置"
export = f"export TUAN_ACTIVEID={TUAN_ACTIVEID} # 京喜工厂团ID\n"
if 'jd' in env:
with open(f"{env}/config/config.sh", 'r', encoding='utf-8') as f3:
configs = f3.readlines()
for config in configs:
if config.find("第五区域") != -1 and config.find("↓") != -1:
end_line = configs.index(config)
break
configs.insert(end_line + 4, export)
configs = ''.join(configs)
elif 'ql' in env:
configs += export
with open(f"{env}/config/config.sh", 'w', encoding='utf-8') as f2:
f2.write(configs)
return msg
else:
msg = "无法从页面读取京喜工厂团ID,无法完成替换"
return msg
def findCrontab():
crontab_list = f'{env}/config/crontab.list'
with open(crontab_list, 'r', encoding='utf-8') as f1:
crontabs = f1.readlines()
for crontab in crontabs:
if crontab.find("jd_dreamFactory") != -1:
cron = ' '.join(crontab.split(" ")[:5])
return cron
return False
def checkCrontab():
storage = '/' + path_list[-2]
file = '/' + path_list[-1]
crontab_list = f'{env}/config/crontab.list'
key = '# 获取京喜工厂团ID(请勿删除此行)\n'
new = f'{cron} python /jd{storage}{file} >> /jd/log{file.split(".")[0]}.log 2>&1\n'
with open(crontab_list, 'r', encoding='utf-8') as f1:
crontab = f1.readlines()
if crontab[-1] == '\n':
del (crontab[-1])
if key in crontab:
m = crontab.index(key) + 1
if crontab[m] != new:
crontab[m] = new
with open(crontab_list, 'w', encoding='utf-8') as f2:
print(''.join(crontab), file=f2)
else:
return
else:
crontab.append(f'\n{key}{new}')
with open(crontab_list, 'w', encoding='utf-8') as f2:
print(''.join(crontab), file=f2)
def tgNofity(user_id, bot_token, text):
TG_API_HOST = 'api.telegram.org'
url = f'https://{TG_API_HOST}/bot{bot_token}/sendMessage'
body = {
"chat_id": user_id,
"text": text,
"disable_web_page_preview": True
}
headers = {
"ontent-Type": "application/x-www-form-urlencoded"
}
try:
r = requests.post(url, data=body, headers=headers)
if r.ok:
print("Telegram发送通知消息成功🎉。\n")
elif r.status_code == '400':
print("请主动给bot发送一条消息并检查接收用户ID是否正确。\n")
elif r.status_code == '401':
print("Telegram bot token 填写错误。\n")
except Exception as error:
print(f"telegram发送通知消息失败!!\n{error}")
# 开始执行主程序
if __name__ == '__main__':
path_list = os.path.realpath(__file__).split('/')[1:]
env = '/' + '/'.join(path_list[:-2])
if os.path.isfile('/ql/config/cookie.sh') or os.path.isfile(f'{env}/config/cookie.sh'): # 青龙
isv4 = False
if not os.path.isfile(f'{env}/config/cookie.sh'): # 青龙容器内
env = '/ql'
else: # v4-bot
isv4 = True
if not os.path.isfile(f'{env}/config/config.sh'): # v4-bot 容器内
env = '/jd'
cron = '2 0,7,20 * * *' # 此处 V4 用户需要自行设置 cron 表达式,否则程序自动设置为 jd_dreamFactory.js 的运行时间
if 'jd' in env:
if len(cron) < 9:
cron = findCrontab()
if not cron:
cron = "0 0,7,20 * * *"
checkCrontab()
msg = TUAN_ACTIVEID()
print(msg)
try:
bot = f'{env}/config/bot.json'
with open(bot, 'r', encoding='utf-8') as botSet:
bot = json.load(botSet)
tgNofity(bot['user_id'], bot['bot_token'], msg)
except:
None