-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathduokan.js
123 lines (118 loc) · 3.67 KB
/
duokan.js
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
const DUOKAN_COOKIE_KEY = 'duokan_cookie'
const DUOKAN_DEVICE_ID_KEY = 'duokan_device_id'
const API_HOST = 'https://www.duokan.com'
const TASK_NAME = '多看阅读'
let $util = init()
;(async () => {
let cookieVal = $util.getdata(DUOKAN_COOKIE_KEY)
let deviceId = $util.getdata(DUOKAN_DEVICE_ID_KEY)
if (!cookieVal || !deviceId) {
$util.msg(TASK_NAME, '⚠️ 请先获取 Cookie')
$util.done({})
return
}
await checkin(cookieVal, deviceId).then(() => {
$util.done({})
})
})()
function checkin(cookieVal, deviceId) {
return new Promise((resolve, reject) => {
let options = {
url: `${API_HOST}/checkin/v0/checkin`,
headers: {
'User-Agent':
'Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148',
Cookie: cookieVal
},
body: signature(deviceId)
}
$util.post(options, (error, response, data) => {
if (error) {
$util.log(`签到失败,error:${error}`)
$util.msg(TASK_NAME, '⚠️ 签到失败,详情请查看日志')
resolve()
return
}
let result = JSON.parse(data)
if (result && result.result === 0 && result.data) {
$util.log(`签到成功,response: ${data}`)
let subtitle = `签到成功,已连续签到 ${result.data.today} 天`
let body = ''
if (result.data.gift && Array.isArray(result.data.gift) && result.data.gift.length > 0) {
body = result.data.gift.reduce((prev, cur) => {
return (prev += `获得 ${cur.value} 个${cur.name} \n`)
}, '')
}
$util.msg(TASK_NAME, subtitle, body)
resolve()
} else {
$util.log(`签到失败,response: ${data}`)
$util.msg(TASK_NAME, `⚠️ 签到失败,${result.msg}`)
resolve()
}
})
})
}
function signature(deviceId) {
let t = parseInt(new Date().getTime() / 1000)
let c = 0
for (char of `${deviceId}&${t}`) {
c = (c * 131 + char.charCodeAt(0)) % 65536
}
return `_t=${t}&_c=${c}`
}
function init() {
isSurge = () => {
return undefined !== this.$httpClient
}
isQuanX = () => {
return undefined !== this.$task
}
getdata = (key) => {
if (isSurge()) return $persistentStore.read(key)
if (isQuanX()) return $prefs.valueForKey(key)
}
setdata = (key, val) => {
if (isSurge()) return $persistentStore.write(key, val)
if (isQuanX()) return $prefs.setValueForKey(key, val)
}
msg = (title, subtitle = '', body = '') => {
if (isSurge()) $notification.post(title, subtitle, body)
if (isQuanX()) $notify(title, subtitle, body)
}
log = (msg) => {
console.log(`${msg}\n`)
}
get = (options, callback) => {
if (isQuanX()) {
if (typeof options == 'string') options = { url: options }
options['method'] = 'GET'
return $task.fetch(options).then(
(response) => {
response['status'] = response.statusCode
callback(null, response, response.body)
},
(reason) => callback(reason.error, null, null)
)
}
if (isSurge()) return $httpClient.get(options, callback)
}
post = (options, callback) => {
if (isQuanX()) {
if (typeof options == 'string') options = { url: options }
options['method'] = 'POST'
$task.fetch(options).then(
(response) => {
response['status'] = response.statusCode
callback(null, response, response.body)
},
(reason) => callback(reason.error, null, null)
)
}
if (isSurge()) $httpClient.post(options, callback)
}
done = (value = {}) => {
$done(value)
}
return { isSurge, isQuanX, msg, log, getdata, setdata, get, post, done }
}