|
1 | | -const cookieName = '网易云音乐' |
2 | | -const cookieKey = 'chavy_cookie_neteasemusic' |
3 | | -const cookieVal = $persistentStore.read(cookieKey) |
| 1 | +const $ = new Env('网易云音乐') |
| 2 | +$.VAL_session = $.getdata('chavy_cookie_neteasemusic') |
| 3 | +$.CFG_retryCnt = ($.getdata('CFG_neteasemusic_retryCnt') || '10') * 1 |
| 4 | +$.CFG_retryInterval = ($.getdata('CFG_neteasemusic_retryInterval') || '500') * 1 |
4 | 5 |
|
5 | | -const pc = `http://music.163.com/api/point/dailyTask?type=1` |
6 | | -const mobile = `http://music.163.com/api/point/dailyTask?type=0` |
7 | | - |
8 | | -function sign() { |
9 | | - let url = { |
10 | | - url: null, |
11 | | - headers: { |
12 | | - Cookie: cookieVal |
13 | | - } |
14 | | - } |
15 | | - |
16 | | - let signinfo = {} |
17 | | - |
18 | | - url.url = pc |
19 | | - $httpClient.post(url, (error, response, data) => { |
20 | | - let result = JSON.parse(data) |
21 | | - signinfo.pc = { |
22 | | - title: `网易云音乐(PC)`, |
23 | | - success: result.code == 200 || result.code == -2 ? true : false, |
24 | | - skiped: result.code == -2 ? true : false, |
25 | | - resultCode: result.code, |
26 | | - resultMsg: result.msg |
27 | | - } |
28 | | - console.log(`开始签到: ${signinfo.pc.title}, 编码: ${result.code}, 原因: ${result.msg}`) |
| 6 | +!(async () => { |
| 7 | + $.log('', `🔔 ${$.name}, 开始!`, '') |
| 8 | + init() |
| 9 | + await signweb() |
| 10 | + await signapp() |
| 11 | + await getInfo() |
| 12 | + await showmsg() |
| 13 | +})() |
| 14 | + .catch((e) => { |
| 15 | + $.log('', `❌ ${$.name}, 失败! 原因: ${e}!`, '') |
29 | 16 | }) |
30 | | - |
31 | | - url.url = mobile |
32 | | - $httpClient.post(url, (error, response, data) => { |
33 | | - let result = JSON.parse(data) |
34 | | - signinfo.app = { |
35 | | - title: `网易云音乐(APP)`, |
36 | | - success: result.code == 200 || result.code == -2 ? true : false, |
37 | | - skiped: result.code == -2 ? true : false, |
38 | | - resultCode: result.code, |
39 | | - resultMsg: result.msg |
40 | | - } |
41 | | - console.log(`开始签到: ${signinfo.app.title}, 编码: ${result.code}, 原因: ${result.msg}`) |
| 17 | + .finally(() => { |
| 18 | + $.msg($.name, $.subt, $.desc), $.log('', `🔔 ${$.name}, 结束!`, ''), $.done() |
42 | 19 | }) |
43 | | - check(signinfo) |
| 20 | + |
| 21 | +function init() { |
| 22 | + $.isNewCookie = /https:\/\/music.163.com\/weapi\/user\/level/.test($.VAL_session) |
| 23 | + $.Cookie = $.isNewCookie ? JSON.parse($.VAL_session).headers.Cookie : $.VAL_session |
44 | 24 | } |
45 | 25 |
|
46 | | -function check(signinfo, checkms = 0) { |
47 | | - if (signinfo.pc && signinfo.app) { |
48 | | - log(signinfo) |
49 | | - $done({}) |
50 | | - } else { |
51 | | - if (checkms > 5000) { |
52 | | - $done({}) |
53 | | - } else { |
54 | | - setTimeout(() => check(signinfo, checkms + 100), 100) |
55 | | - } |
| 26 | +async function signweb() { |
| 27 | + for (let signIdx = 0; signIdx < $.CFG_retryCnt; signIdx++) { |
| 28 | + await new Promise((resove) => { |
| 29 | + const url = { url: `http://music.163.com/api/point/dailyTask?type=1`, headers: {} } |
| 30 | + url.headers['Cookie'] = $.Cookie |
| 31 | + url.headers['Host'] = 'music.163.com' |
| 32 | + url.headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15' |
| 33 | + $.get(url, (error, response, data) => { |
| 34 | + try { |
| 35 | + $.isWebSuc = JSON.parse(data).code === -2 |
| 36 | + $.log(`[Web] 第 ${signIdx + 1} 次: ${data}`) |
| 37 | + } catch (e) { |
| 38 | + $.isWebSuc = false |
| 39 | + $.log(`❗️ ${$.name}, 执行失败!`, ` error = ${error || e}`, `response = ${JSON.stringify(response)}`, '') |
| 40 | + } finally { |
| 41 | + resove() |
| 42 | + } |
| 43 | + }) |
| 44 | + }) |
| 45 | + await new Promise($.wait($.CFG_retryInterval)) |
| 46 | + if ($.isWebSuc) break |
56 | 47 | } |
57 | 48 | } |
58 | 49 |
|
59 | | -function log(signinfo) { |
60 | | - let title = `${cookieName}` |
61 | | - let subTitle = `` |
62 | | - let detail = `今日共签: ${signinfo.signedCnt}, 本次成功: ${signinfo.successCnt}, 本次失败: ${signinfo.failedCnt}` |
63 | | - |
64 | | - if (signinfo.pc.success && signinfo.app.success) { |
65 | | - subTitle = `签到结果: 全部成功` |
66 | | - detail = `PC: ${signinfo.pc.success ? '成功' : '失败'}, APP: ${signinfo.app.success ? '成功' : '失败'}` |
67 | | - } else if (!signinfo.pc.success && !signinfo.app.success) { |
68 | | - subTitle = `签到结果: 全部失败` |
69 | | - detail = `PC: ${signinfo.pc.success ? '成功' : '失败'}, APP: ${signinfo.app.success ? '成功' : '失败'}, 详见日志!` |
70 | | - } else { |
71 | | - subTitle = `` |
72 | | - detail = `PC: ${signinfo.pc.success ? '成功' : '失败'}, APP: ${signinfo.app.success ? '成功' : '失败'}, 详见日志!` |
| 50 | +async function signapp() { |
| 51 | + for (let signIdx = 0; signIdx < $.CFG_retryCnt; signIdx++) { |
| 52 | + await new Promise((resove) => { |
| 53 | + const url = { url: `http://music.163.com/api/point/dailyTask?type=0`, headers: {} } |
| 54 | + url.headers['Cookie'] = $.Cookie |
| 55 | + url.headers['Host'] = 'music.163.com' |
| 56 | + url.headers['User-Agent'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1' |
| 57 | + $.get(url, (error, response, data) => { |
| 58 | + try { |
| 59 | + $.isAppSuc = JSON.parse(data).code === -2 |
| 60 | + $.log(`[App] 第 ${signIdx + 1} 次: ${data}`) |
| 61 | + } catch (e) { |
| 62 | + $.isAppSuc = false |
| 63 | + $.log(`❗️ ${$.name}, 执行失败!`, ` error = ${error || e}`, `response = ${JSON.stringify(response)}`, '') |
| 64 | + } finally { |
| 65 | + resove() |
| 66 | + } |
| 67 | + }) |
| 68 | + }) |
| 69 | + await new Promise($.wait($.CFG_retryInterval)) |
| 70 | + if ($.isAppSuc) break |
73 | 71 | } |
74 | | - $notification.post(title, subTitle, detail) |
75 | 72 | } |
76 | 73 |
|
77 | | -sign() |
| 74 | +function getInfo() { |
| 75 | + if (!$.isNewCookie) return |
| 76 | + return new Promise((resove) => { |
| 77 | + $.post(JSON.parse($.VAL_session), (error, response, data) => { |
| 78 | + try { |
| 79 | + $.userInfo = JSON.parse(data) |
| 80 | + } catch (e) { |
| 81 | + $.log(`❗️ ${$.name}, 执行失败!`, ` error = ${error || e}`, `response = ${JSON.stringify(response)}`, '') |
| 82 | + } finally { |
| 83 | + resove() |
| 84 | + } |
| 85 | + }) |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +function showmsg() { |
| 90 | + return new Promise((resove) => { |
| 91 | + $.subt = $.isWebSuc ? 'PC: 成功' : 'PC: 失败' |
| 92 | + $.subt += $.isAppSuc ? ', APP: 成功' : ', APP: 失败' |
| 93 | + if ($.isNewCookie && $.userInfo) { |
| 94 | + $.desc = `等级: ${$.userInfo.data.level}, 听歌: ${$.userInfo.data.nowPlayCount} => ${$.userInfo.data.nextPlayCount} 升级 (首)` |
| 95 | + } |
| 96 | + resove() |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +// prettier-ignore |
| 101 | +function Env(t){this.name=t,this.logs=[],this.isSurge=(()=>"undefined"!=typeof $httpClient),this.isQuanX=(()=>"undefined"!=typeof $task),this.log=((...t)=>{this.logs=[...this.logs,...t],t?console.log(t.join("\n")):console.log(this.logs.join("\n"))}),this.msg=((t=this.name,s="",i="")=>{this.isSurge()&&$notification.post(t,s,i),this.isQuanX()&&$notify(t,s,i);const e=["","==============\ud83d\udce3\u7cfb\u7edf\u901a\u77e5\ud83d\udce3=============="];t&&e.push(t),s&&e.push(s),i&&e.push(i),console.log(e.join("\n"))}),this.getdata=(t=>this.isSurge()?$persistentStore.read(t):this.isQuanX()?$prefs.valueForKey(t):void 0),this.setdata=((t,s)=>this.isSurge()?$persistentStore.write(t,s):this.isQuanX()?$prefs.setValueForKey(t,s):void 0),this.get=((t,s)=>this.send(t,"GET",s)),this.wait=((t,s=t)=>i=>setTimeout(()=>i(),Math.floor(Math.random()*(s-t+1)+t))),this.post=((t,s)=>this.send(t,"POST",s)),this.send=((t,s,i)=>{if(this.isSurge()){const e="POST"==s?$httpClient.post:$httpClient.get;e(t,(t,s,e)=>{s&&(s.body=e,s.statusCode=s.status),i(t,s,e)})}this.isQuanX()&&(t.method=s,$task.fetch(t).then(t=>{t.status=t.statusCode,i(null,t,t.body)},t=>i(t.error,t,t)))}),this.done=((t={})=>$done(t))} |
0 commit comments