This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
index.py
159 lines (137 loc) · 4.6 KB
/
index.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
# -*- coding: utf-8 -*-
"""
cron: 35 8 * * *
new Env('网易云音乐自动任务');
"""
from utils import updateConfig
import time
import requests
import json
import json5
import re
import os
from user import User
from pusher import Pusher
from utils import append_environ
import random
runtime = 'tencent-scf'
def md2text(data):
data = re.sub(r'\[(.*?)\]\((.*?)\)', r'\1: \2 ', data)
data = re.sub(r'- ', r' •', data)
data = re.sub(r'#### (.*?)\n', r'【\1】\n\n', data)
data = re.sub(r'### ', r'\n', data)
data = data.replace('`', '')
return data
def getSongNumber():
res = {}
if runtime == 'tencent-scf':
if "SONG_NUMBER" in os.environ:
sp1 = os.environ.get("SONG_NUMBER").split("#")
if len(sp1) != 2:
return res
if sp1[0] != time.strftime("%Y-%m-%d", time.gmtime(time.time()+28800)):
print("环境变量 SONG_NUMBER 已过期。是否未开启定时触发器 timer-songnumber")
return res
for number in sp1[1].split(";"):
sp2 = number.split(":")
if len(sp2) == 2:
res[sp2[0]] = int(sp2[1])
else:
print(
"环境变量 SONG_NUMBER 不存在。项目地址: https://github.com/chen310/NeteaseCloudMusicTasks")
return res
def start(event={}, context={}):
with open('config.json', 'r', encoding='utf-8') as f:
config = json5.load(f)
print('Version:', config['version'])
if 'sha' in config:
print('Commit ID:', config['sha'])
# 公共配置
setting = config['setting']
songnumber = getSongNumber()
# 推送
pusher = Pusher()
saved_environs = {}
for user_config in config['users']:
if not user_config['enable']:
continue
# 获取账号配置
if "setting" in user_config:
user_setting = updateConfig(user_config["setting"], setting)
else:
user_setting = setting
user = User()
user.runtime = runtime
user.setUser(user_config, user_setting)
if user.isLogined:
user.songnumber = songnumber.get(str(user.uid), -1)
user.startTask()
for push in user_setting['push'].values():
if not push['enable']:
continue
data = {
'title': user.title,
'mdmsg': user.msg,
'textmsg': md2text(user.msg),
'config': push
}
pusher.append(data)
saved_environs.update(user.saved_environs)
if len(saved_environs) > 0:
res = append_environ(saved_environs)
if res:
print('已成功保存环境变量')
else:
print('环境变量保存失败')
pusher.push()
def setSongNumber():
with open('config.json', 'r', encoding='utf-8') as f:
config = json5.load(f)
setting = config['setting']
songNumber = ""
timer_enable = False
time.sleep(random.randint(5, 20))
for user_config in config['users']:
if not user_config['enable']:
continue
if "setting" in user_config:
user_setting = updateConfig(user_config["setting"], setting)
else:
user_setting = setting
if (not user_setting['daka']['enable']) or (not user_setting['daka']['auto']):
continue
user = User()
user.runtime = runtime
user.setUser(user_config, user_setting)
if user.isLogined:
if user.listenSongs == 0:
resp = user.music.user_detail(user.uid)
listenSongs = resp['listenSongs']
else:
listenSongs = user.listenSongs
# if user_setting['daka']['full_stop'] == True and (resp['level'] == 10 or resp['listenSongs'] >= 20000):
# continue
songNumber += str(user.uid) + ":" + str(listenSongs) + ";"
timer_enable = True
time.sleep(2)
if not timer_enable:
# TODO
pass
if not songNumber:
print('未更新歌曲播放数量')
return
songNumber = time.strftime(
"%Y-%m-%d", time.gmtime(time.time()+28800)) + "#" + songNumber
res = append_environ({"SONG_NUMBER": songNumber})
if res:
print("已更新歌曲播放数量")
else:
print("歌曲播放数量更新失败")
def main_handler(event, context):
if event.get("Type") == "Timer" and event.get("TriggerName") == "timer-songnumber":
setSongNumber()
return
start(event, context)
if __name__ == '__main__':
runtime = 'local'
start()