Script/Bilibili-DailyBonus/Manga.js

117 lines
11 KiB
JavaScript
Raw Normal View History

/*
2020-08-29 22:26:49 +08:00
哔哩哔哩漫画签到
2020-08-29 22:26:49 +08:00
脚本兼容: QuantumultX, Surge, Loon
电报频道@NobyDa
问题反馈@NobyDa_bot
如果转载请注明出处
2020-08-29 22:26:49 +08:00
说明
打开哔哩哔哩漫画后 (AppStore中国区)单击"我的", 如果通知获取cookie成功, 则可以使用此脚本.
2020-08-29 22:26:49 +08:00
脚本将在每天上午9点执行 您可以修改执行时间
~~~~~~~~~~~~~~~~
2020-08-29 22:26:49 +08:00
Surge 4.2.0+ :
[Script]
2020-08-29 22:26:49 +08:00
Bili漫画签到 = type=cron,cronexp=0 9 * * *,wake-system=1,script-path=https://raw.githubusercontent.com/NobyDa/Script/master/Bilibili-DailyBonus/Manga.js
2021-09-22 23:55:44 +08:00
Bili漫画Cookie = type=http-request,pattern=^https:\/\/app\.bilibili\.com\/x\/v2\/account\/myinfo,script-path=https://raw.githubusercontent.com/NobyDa/Script/master/Bilibili-DailyBonus/Manga.js
2020-08-29 22:26:49 +08:00
[MITM]
2021-09-22 23:55:44 +08:00
hostname = app.bilibili.com
~~~~~~~~~~~~~~~~
2020-08-29 22:26:49 +08:00
QX 1.0.10+ :
[task_local]
2020-08-29 22:26:49 +08:00
0 9 * * * https://raw.githubusercontent.com/NobyDa/Script/master/Bilibili-DailyBonus/Manga.js, tag=Bili漫画签到
[rewrite_local]
2020-08-29 22:26:49 +08:00
#获取Bili漫画Cookie
2021-09-22 23:55:44 +08:00
^https:\/\/app\.bilibili\.com\/x\/v2\/account\/myinfo url script-request-header https://raw.githubusercontent.com/NobyDa/Script/master/Bilibili-DailyBonus/Manga.js
2020-08-29 22:26:49 +08:00
[mitm]
2021-09-22 23:55:44 +08:00
hostname = app.bilibili.com
2020-08-29 22:26:49 +08:00
~~~~~~~~~~~~~~~~
*/
2021-09-22 23:55:44 +08:00
const $ = new Env(`哔哩哔哩漫画`);
2021-09-22 23:55:44 +08:00
const cookie = $.getdata("CookieBM") || ''; // 哔哩哔哩漫画Cookie
2021-09-22 23:55:44 +08:00
const barkKey = ''; //Bark APP 通知推送Key
if (typeof $request !== 'undefined') {
GetCookie()
} else {
checkin()
}
function checkin() {
const bilibili = {
url: 'https://manga.bilibili.com/twirp/activity.v1.Activity/ClockIn',
headers: {
2021-09-22 23:55:44 +08:00
Cookie: cookie,
},
body: "platform=ios"
};
2021-09-22 23:55:44 +08:00
$.post(bilibili, async function(error, response, data) {
if (error && !data) {
$.msgBody = `请求失败!\n${error}`;
} else if (parseInt(response.status) == 200) {
$.msgBody = "签到成功!🎉";
} else if (/duplicate/.test(data)) {
$.msgBody = "今日已签过 ⚠️";
} else if (/uid must/.test(data)) {
$.msgBody = "Cookie失效 ‼️‼️";
} else {
2021-09-22 23:55:44 +08:00
$.msgBody = `签到失败 ‼️\n${data}`;
}
if (barkKey) {
await BarkNotify($, barkKey, $.name, $.msgBody);
}
2021-09-22 23:55:44 +08:00
$.msg($.name, ``, $.msgBody);
$.done();
})
}
function GetCookie() {
2020-01-25 13:31:21 +08:00
var CookieName = "B站漫画";
var CookieKey = "CookieBM";
var regex = /SESSDATA=.+?;/;
if ($request.headers) {
2022-06-15 21:16:31 +08:00
var header = Object.keys($request.headers).reduce((t, i) => (t[i.toLowerCase()] = $request.headers[i], t), {})['cookie'] || '';
2020-01-25 13:31:21 +08:00
if (header.indexOf("SESSDATA=") != -1) {
var CookieValue = regex.exec(header)[0];
2021-09-22 23:55:44 +08:00
if ($.getdata(CookieKey)) {
if ($.getdata(CookieKey) != CookieValue) {
var cookie = $.setdata(CookieValue, CookieKey);
2020-01-25 13:31:21 +08:00
if (!cookie) {
2021-09-22 23:55:44 +08:00
$.msg("更新" + CookieName + "Cookie失败‼", "", "");
2020-01-25 13:31:21 +08:00
} else {
2021-09-22 23:55:44 +08:00
$.msg("更新" + CookieName + "Cookie成功 🎉", "", "");
2020-01-25 13:31:21 +08:00
}
}
} else {
2021-09-22 23:55:44 +08:00
var cookie = $.setdata(CookieValue, CookieKey);
if (!cookie) {
2021-09-22 23:55:44 +08:00
$.msg("首次写入" + CookieName + "Cookie失败‼", "", "");
} else {
2021-09-22 23:55:44 +08:00
$.msg("首次写入" + CookieName + "Cookie成功 🎉", "", "");
}
}
} else {
2021-09-22 23:55:44 +08:00
$.msg("写入" + CookieName + "Cookie失败‼", "", "Cookie关键值缺失");
}
} else {
2021-09-22 23:55:44 +08:00
$.msg("写入" + CookieName + "Cookie失败‼", "", "配置错误, 无法读取请求头,");
}
2021-09-22 23:55:44 +08:00
$.done()
}
2021-09-22 23:55:44 +08:00
//Bark APP notify
async function BarkNotify(c,k,t,b){for(let i=0;i<3;i++){console.log(`🔷Bark notify >> Start push (${i+1})`);const s=await new Promise((n)=>{c.post({url:'https://api.day.app/push',headers:{'Content-Type':'application/json'},body:JSON.stringify({title:t,body:b,device_key:k,ext_params:{group:t}})},(e,r,d)=>r&&r.status==200?n(1):n(d||e))});if(s===1){console.log('✅Push success!');break}else{console.log(`❌Push failed! >> ${s.message||s}`)}}};
// https://github.com/chavyleung/scripts/blob/master/Env.min.js
function Env(t,e){class s{constructor(t){this.env=t}send(t,e="GET"){t="string"==typeof t?{url:t}:t;let s=this.get;return"POST"===e&&(s=this.post),new Promise((e,i)=>{s.call(this,t,(t,s,r)=>{t?i(t):e(s)})})}get(t){return this.send.call(this.env,t)}post(t){return this.send.call(this.env,t,"POST")}}return new class{constructor(t,e){this.name=t,this.http=new s(this),this.data=null,this.dataFile="box.dat",this.logs=[],this.isMute=!1,this.isNeedRewrite=!1,this.logSeparator="\n",this.startTime=(new Date).getTime(),Object.assign(this,e),this.log("",`\ud83d\udd14${this.name}, \u5f00\u59cb!`)}isNode(){return"undefined"!=typeof module&&!!module.exports}isQuanX(){return"undefined"!=typeof $task}isSurge(){return"undefined"!=typeof $httpClient&&"undefined"==typeof $loon}isLoon(){return"undefined"!=typeof $loon}toObj(t,e=null){try{return JSON.parse(t)}catch{return e}}toStr(t,e=null){try{return JSON.stringify(t)}catch{return e}}getjson(t,e){let s=e;const i=this.getdata(t);if(i)try{s=JSON.parse(this.getdata(t))}catch{}return s}setjson(t,e){try{return this.setdata(JSON.stringify(t),e)}catch{return!1}}getScript(t){return new Promise(e=>{this.get({url:t},(t,s,i)=>e(i))})}runScript(t,e){return new Promise(s=>{let i=this.getdata("@chavy_boxjs_userCfgs.httpapi");i=i?i.replace(/\n/g,"").trim():i;let r=this.getdata("@chavy_boxjs_userCfgs.httpapi_timeout");r=r?1*r:20,r=e&&e.timeout?e.timeout:r;const[o,h]=i.split("@"),a={url:`http://${h}/v1/scripting/evaluate`,body:{script_text:t,mock_type:"cron",timeout:r},headers:{"X-Key":o,Accept:"*/*"}};this.post(a,(t,e,i)=>s(i))}).catch(t=>this.logErr(t))}loaddata(){if(!this.isNode())return{};{this.fs=this.fs?this.fs:require("fs"),this.path=this.path?this.path:require("path");const t=this.path.resolve(this.dataFile),e=this.path.resolve(process.cwd(),this.dataFile),s=this.fs.existsSync(t),i=!s&&this.fs.existsSync(e);if(!s&&!i)return{};{const i=s?t:e;try{return JSON.parse(this.fs.readFileSync(i))}catch(t){return{}}}}}writedata(){if(this.isNode()){this.fs=this.fs?this.fs:require("fs"),this.path=this.path?this.path:require("path");const t=this.path.resolve(this.dataFile),e=this.path.resolve(process.cwd(),this.dataFile),s=this.fs.existsSync(t),i=!s&&this.fs.existsSync(e),r=JSON.stringify(this.data);s?this.fs.writeFileSync(t,r):i?this.fs.writeFileSync(e,r):this.fs.writeFileSync(t,r)}}lodash_get(t,e,s){const i=e.replace(/\[(\d+)\]/g,".$1").split(".");let r=t;for(const t of i)if(r=Object(r)[t],void 0===r)return s;return r}lodash_set(t,e,s){return Object(t)!==t?t:(Array.isArray(e)||(e=e.toString().match(/[^.[\]]+/g)||[]),e.slice(0,-1).reduce((t,s,i)=>Object(t[s])===t[s]?t[s]:t[s]=Math.abs(e[i+1])>>0==+e[i+1]?[]:{},t)[e[e.length-1]]=s,t)}getdata(t){let e=this.getval(t);if(/^@/.test(t)){const[,s,i]=/^@(.*?)\.(.*?)$/.exec(t),r=s?this.getval(s):"";if(r)try{const t=JSON.parse(r);e=t?this.lodash_get(t,i,""):e}catch(t){e=""}}return e}setdata(t,e){let s=!1;if(/^@/.test(e)){const[,i,r]=/^@(.*?)\.(.*?)$/.exec(e),o=this.getval(i),h=i?"null"===o?null:o||"{}":"{}";try{const e=JSON.parse(h);this.lodash_set(e,r,t),s=this.setval(JSON.stringify(e),i)}catch(e){const o={};this.lodash_set(o,r,t),s=this.setval(JSON.stringify(o),i)}}else s=this.setval(t,e);return s}getval(t){return this.isSurge()||this.isLoon()?$persistentStore.read(t):this.isQuanX()?$prefs.valueForKey(t):this.isNode()?(this.data=this.loaddata(),this.data[t]):this.data&&this.data[t]||null}setval(t,e){return this.isSurge()||this.isLoon()?$persistentStore.write(t,e):this.isQuanX()?$prefs.setValueForKey(t,e):this.isNode()?(this.data=this.loaddata(),this.data[e]=t,this.writedata(),!0):this.data&&this.data[e]||null}initGotEnv(t){this.got=this.got?this.got:require("got"),this.cktough=this.cktough?this.cktough:require("tough-cookie"),this.ckjar=this.ckjar?this.ckjar:new this.cktough.CookieJar,t&&(t.headers=t.headers?t.headers:{},void 0===t.headers.Cookie&&void 0===t.cookieJar&&(t.cookieJar=this.ckjar))}get(t,e=(()=>{})){t.headers&&(delete t.headers["Content-Type"],delete t.headers["Content-Length"]),this.isSurge()||this.isLoon()?(this.isSurge()&&this.isNeedRewrite&&(t.headers=t.