This repository has been archived on 2024-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
line-cost-js/CPBLClass.js

404 lines
16 KiB
JavaScript
Raw Permalink Normal View History

2021-04-27 00:42:22 +00:00
const dateFormat = require('dateformat');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const schedule = require('node-schedule');
const { decode } = require('querystring');
2021-12-26 07:15:28 +00:00
const LINENotifyClass = require('../api/LINENotifyClass');
2021-04-27 00:42:22 +00:00
/** CPBL */
class CPBLClass {
constructor(app) {
this.app = app;
2021-12-26 07:15:28 +00:00
this.LINENotify = new LINENotifyClass();
2021-04-27 00:42:22 +00:00
this.TimeList = [];
// var rule = new schedule.RecurrenceRule();
// // rule.minute = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
// rule.second = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
// // console.log(`設定任務 每${JSON.stringify(rule.minute)}分鐘 現在時間: ` + dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss"));
// console.log(`設定任務 每${JSON.stringify(rule.second)}秒鐘 現在時間: ` + dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss"));
// this.Timer = schedule.scheduleJob(rule, this.Update.bind(this));
}
async Update() {
// let datetime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
// console.log("執行任務 現在時間: " + datetime);
// if (dateFormat(new Date(), "ss") === "00") {
// this.Timer.cancel();
// // console.log("取消任務 現在時間: " + datetime);
// }
if (this.TimeList.length > 0) {
for (let i = 0; i < this.TimeList.length; i++) {
this.RunTime(this.TimeList[i]);
}
} else {
this.CloseTime();
}
}
async AddTime(Data, event) {
2021-12-26 07:15:28 +00:00
let replyMsg = "";
let GameSno = Data["GameSno"];
2021-04-27 00:42:22 +00:00
let LineID = Data["LineID"];
2021-12-26 07:15:28 +00:00
let access_token = Data["access_token"];
2021-04-27 00:42:22 +00:00
2021-12-26 07:15:28 +00:00
let IsHaveGameSno = false;
let IsHaveLineID = false;
2021-04-27 00:42:22 +00:00
let Time = {
2021-12-26 07:15:28 +00:00
GameSno: GameSno,
LineID: [LineID],
access_token: [access_token]
2021-04-27 00:42:22 +00:00
}
for (let i = 0; i < this.TimeList.length; i++) {
2021-12-26 07:15:28 +00:00
if (this.TimeList[i]["GameSno"] === GameSno) {
IsHaveGameSno = true;
2021-04-27 00:42:22 +00:00
Time = this.TimeList[i];
for (let j = 0; j < this.TimeList[i]["LineID"].length; j++) {
if (this.TimeList[i]["LineID"][j] === LineID) {
2021-12-26 07:15:28 +00:00
IsHaveLineID = true;
2021-04-27 00:42:22 +00:00
break;
}
}
break;
}
}
let IsRUN = true;
2021-12-26 07:15:28 +00:00
let Response = await this.GetCPBL(GameSno);
if (Response === "比賽尚未開始" || Response[Response.length - 1].indexOf("比賽結束") !== -1) {
2021-04-27 00:42:22 +00:00
IsRUN = false;
}
2021-12-26 07:15:28 +00:00
if (Response === "比賽尚未開始") {
// await this.LINENotify.Send(access_token, Response);
} else {
let PushMsg = "";
for (let i = 0; i < Response.length; i++) {
let ThisPushMsg = Response[i];
if (PushMsg.length + ThisPushMsg.length > 1000) {
// await this.LINENotify.Send(access_token, PushMsg);
PushMsg = ThisPushMsg;
} else {
PushMsg += ThisPushMsg;
}
2021-04-27 00:42:22 +00:00
}
2021-12-26 07:15:28 +00:00
// await this.LINENotify.Send(access_token, PushMsg);
2021-04-27 00:42:22 +00:00
}
if (IsRUN) {
2021-12-26 07:15:28 +00:00
if (!IsHaveGameSno) {
2021-04-27 00:42:22 +00:00
// 沒有這場賽事
if (this.TimeList.length === 0) {
this.StartTime();
}
this.TimeList.push(Time);
2021-12-26 07:15:28 +00:00
} else if (IsHaveGameSno && !IsHaveLineID) {
2021-04-27 00:42:22 +00:00
// 有這場賽事但沒這個帳號
Time["LineID"].push(LineID);
2021-12-26 07:15:28 +00:00
Time["access_token"].push(access_token);
2021-04-27 00:42:22 +00:00
}
let Extra = {
2021-12-26 07:15:28 +00:00
GameSno: GameSno,
2021-04-27 00:42:22 +00:00
count: Response.length
}
let datetime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
let Query = `UPDATE \`line-cost-status\` SET \`datetime\`='${datetime}', \`Status\`='CPBL RUN', \`Extra\`='${JSON.stringify(Extra)}' WHERE (\`userid\`='${LineID}');`;
let res_Query = await this.app.Tools_MYSQLDB.Query(Query);
} else {
2021-12-26 07:15:28 +00:00
replyMsg = "已停止中職轉播功能";
2021-04-27 00:42:22 +00:00
}
if (event) {
event.reply(replyMsg).then(function (data) {
// 當訊息成功回傳後的處理
}).catch(function (error) {
// 當訊息回傳失敗後的處理
});
}
}
StartTime() {
2021-12-26 07:15:28 +00:00
// var rule = '2 * * * * *';
2021-04-27 00:42:22 +00:00
// * * * * * *
// ┬ ┬ ┬ ┬ ┬ ┬
// │ │ │ │ │ |
// │ │ │ │ │ └ 星期几取值0 - 7其中 0 和 7 都表示是周日
// │ │ │ │ └─── 月份取值1 - 12
// │ │ │ └────── 日期取值1 - 31
// │ │ └───────── 时取值0 - 23
// │ └──────────── 分取值0 - 59
// └─────────────── 秒取值0 - 59可选
var rule = new schedule.RecurrenceRule();
2021-12-26 07:15:28 +00:00
// rule.date = "*";
// rule.dayOfWeek = "*";
// rule.hour = "*";
// rule.minute = "*";
// rule.year = "*";
// rule.month = "*";
2021-04-27 00:42:22 +00:00
let minute = 2;
let minute_arr = [];
for (let i = 0; i < 60; i++) {
if (i % minute === 0) {
minute_arr.push(i);
}
}
2021-12-26 07:15:28 +00:00
rule.second = new schedule.Range(0, 59, 2);
// rule.minute = minute_arr;
2021-04-27 00:42:22 +00:00
// rule.second = [0];
// rule.second = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55];
2021-12-26 07:15:28 +00:00
// console.log(`設定任務 每${minute}分鐘 現在時間: ` + dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss"));
console.log(`設定任務 每${minute}秒鐘 現在時間: ` + dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss"));
// this.Timer = schedule.scheduleJob(rule, this.Update.bind(this));
this.Timer = schedule.scheduleJob(rule, () => {
console.log(`現在時間: ${dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss")}`);
});
2021-04-27 00:42:22 +00:00
}
CloseTime() {
this.Timer.cancel();
}
2021-12-26 07:15:28 +00:00
async GetCPBL(GameSno) {
2021-04-27 00:42:22 +00:00
let Data = {
2021-12-26 07:15:28 +00:00
GameSno: GameSno,
KindCode: "A",
Year: dateFormat(new Date(), "yyyy")
2021-04-27 00:42:22 +00:00
};
2021-12-26 07:15:28 +00:00
let url = "https://jianmiau.ml:3333/CPBL";
2021-04-27 00:42:22 +00:00
let Response = await this.GetData(url, Data);
2021-12-26 07:15:28 +00:00
Response = JSON.parse(Response);
// Response = [].concat.apply([], JSON.parse(Response));
2021-04-27 00:42:22 +00:00
return new Promise((resolve, reject) => {
// 傳入 resolve 與 reject表示資料成功與失敗
resolve(Response);
// reject()
});
}
DelTime(game_id) {
for (let i = 0; i < this.TimeList.length; i++) {
if (this.TimeList[i]["game_id"] === game_id) {
this.TimeList.splice(i, 1);
if (this.TimeList.length === 0) {
this.CloseTime();
}
}
}
}
async RunTime(Time) {
2021-12-26 07:15:28 +00:00
let GameSno = Time["GameSno"];
2021-04-27 00:42:22 +00:00
let LineID = Time["LineID"];
2021-12-26 07:15:28 +00:00
let access_token = Time["access_token"];
let Response = await this.GetCPBL(GameSno);
2021-04-27 00:42:22 +00:00
let IsRUN = true;
2021-12-26 07:15:28 +00:00
if (Response === "比賽尚未開始" || Response[Response.length - 1].indexOf("比賽結束") !== -1) {
2021-04-27 00:42:22 +00:00
IsRUN = false;
}
for (let i = 0; i < LineID.length; i++) {
let Query = `SELECT * FROM \`line-cost-status\` WHERE \`userId\` = '${LineID[i]}' LIMIT 1;`;
let res_Query = await this.app.Tools_MYSQLDB.Query(Query);
let Data = res_Query;
let Status = Data[0]['Status'];
if (Status === "CPBL RUN") {
let Extra = JSON.parse(Data[0]["Extra"]);
2021-12-26 07:15:28 +00:00
let GameSno = Extra["GameSno"];
2021-04-27 00:42:22 +00:00
let count = Extra["count"];
if (Response.length > count) {
2021-12-26 07:15:28 +00:00
let PushMsg = "";
for (let i = count; i < Response.length; i++) {
let ThisPushMsg = Response[i];
if (PushMsg.length + ThisPushMsg.length > 1000) {
await this.LINENotify.Send(access_token, PushMsg);
PushMsg = ThisPushMsg;
} else {
PushMsg += ThisPushMsg;
2021-04-27 00:42:22 +00:00
}
}
2021-12-26 07:15:28 +00:00
await this.LINENotify.Send(access_token, PushMsg);
2021-04-27 00:42:22 +00:00
if (IsRUN) {
let Extra = {
2021-12-26 07:15:28 +00:00
GameSno: GameSno,
2021-04-27 00:42:22 +00:00
count: Response.length
}
let datetime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
let Query = `UPDATE \`line-cost-status\` SET \`datetime\`='${datetime}', \`Status\`='CPBL RUN', \`Extra\`='${JSON.stringify(Extra)}' WHERE (\`userid\`='${LineID[i]}');`;
let res_Query = await this.app.Tools_MYSQLDB.Query(Query);
} else {
let datetime = dateFormat(new Date(), "yyyy-mm-dd HH:MM:ss");
let Query = `UPDATE \`line-cost-status\` SET \`datetime\`='${datetime}', \`Status\`='', \`Extra\`='' WHERE (\`userid\`='${LineID[i]}');`;
let res_Query = await this.app.Tools_MYSQLDB.Query(Query);
2021-12-26 07:15:28 +00:00
PushMsg = "已停止中職轉播功能";
let res_Msg = this.app.bot.push(LineID[i], PushMsg);
2021-04-27 00:42:22 +00:00
}
2021-12-26 07:15:28 +00:00
// await this.LINENotify.Send(access_token, PushMsg);
2021-04-27 00:42:22 +00:00
}
}
}
if (!IsRUN) {
this.DelTime(game_id);
}
}
2021-12-26 07:15:28 +00:00
async GetCPBLList(DateTime, access_token) {
2021-04-27 00:42:22 +00:00
// let game_id = Time["game_id"];
// let cpbldata = Time["cpbldata"];
// let LineID = Time["LineID"];
let url = "https://jianmiau.ml:3333/CPBLList";
let Data = {
2021-12-26 07:15:28 +00:00
today: DateTime
// today: "2021-09-26"
2021-04-27 00:42:22 +00:00
};
let Response = await this.GetData(url, Data);
Response = [].concat.apply([], JSON.parse(Response));
let columns = [];
for (let i = 0; i < Response.length; i++) {
let img = [];
let team = [];
let data = "";
2021-12-26 07:15:28 +00:00
let game = Response[i];
let info = {
"樂天桃猿": {
img: "https://jianmiau.ml/MyWeb/Resources/CPBL/R.png"
},
"富邦悍將": {
img: "https://jianmiau.ml/MyWeb/Resources/CPBL/F.png"
},
"味全龍": {
img: "https://jianmiau.ml/MyWeb/Resources/CPBL/D.png"
},
"中信兄弟": {
img: "https://jianmiau.ml/MyWeb/Resources/CPBL/B.png"
},
"統一7-ELEVEn獅": {
img: "https://jianmiau.ml/MyWeb/Resources/CPBL/L.png"
},
2021-04-27 00:42:22 +00:00
}
2021-12-26 07:15:28 +00:00
let url = `https://karolchang.github.io/cpbl-vue/#/record/${game.GameSno}/A/2021`;
let gametime = game.PreExeDate.split("T");
// let nowUNIXtime = Date.now();
// let gameUNIXtime = new Date(`${gametime[0]} ${gametime[1]}`).getTime();
// let IsStart = nowUNIXtime - gameUNIXtime > 0;
data = `action=hasgame&game_date=${gametime[0]}&time=${gametime[1]}&GameSno=${game.GameSno}&access_token=${access_token}`;
2021-04-27 00:42:22 +00:00
let Data_columns = {
2021-12-26 07:15:28 +00:00
"thumbnailImageUrl": info[game.HomeTeamName].img,
2021-04-27 00:42:22 +00:00
"imageBackgroundColor": "#FFFFFF",
2021-12-26 07:15:28 +00:00
"title": `${game.VisitingTeamName} VS ${game.HomeTeamName}`,
"text": `比賽場地 ${game.FieldAbbe} ${gametime[0]} ${gametime[1]}`,
2021-04-27 00:42:22 +00:00
"defaultAction": {
"type": "uri",
"label": "比賽網站",
"uri": url
},
"actions": [
{
2021-12-26 07:15:28 +00:00
// "type": "uri",
// "label": "比賽網站",
// "uri": url
2021-04-27 00:42:22 +00:00
"type": "postback",
"label": "追蹤比賽",
"data": data
}
]
};
columns.push(Data_columns);
}
// columns = [
// {
// "thumbnailImageUrl": "https://jianmiau.ml/MyWeb/Resources/CPBL/L.png",
// "imageBackgroundColor": "#FFFFFF",
// "title": "this is menu",
// "text": "description",
// "defaultAction": {
// "type": "uri",
// "label": "View detail",
// "uri": "http://example.com/page/123"
// },
// "actions": [
// {
// "type": "postback",
// "label": "Buy",
// "data": "action=buy&itemid=111"
// }
// ]
// },
// {
// "thumbnailImageUrl": "https://jianmiau.ml/MyWeb/Resources/CPBL/B.png",
// "imageBackgroundColor": "#000000",
// "title": "this is menu",
// "text": "description",
// "defaultAction": {
// "type": "uri",
// "label": "View detail",
// "uri": "http://example.com/page/222"
// },
// "actions": [
// {
// "type": "postback",
// "label": "Buy",
// "data": "action=buy&itemid=222"
// }
// ]
// }
// ]
return new Promise((resolve, reject) => {
// 傳入 resolve 與 reject表示資料成功與失敗
resolve(columns);
// reject()
});
}
2021-12-26 07:15:28 +00:00
async CPBLBind(Date) {
let url = `https://notify-bot.line.me/oauth/authorize?response_type=code&scope=notify&response_mode=form_post&client_id=Jj4K1D7XxtmDeXJGF3c0vV&redirect_uri=https://jianmiau.ml:3333/LINENotify&state=${userId}$${displayName}`;
let shorturl = await this.Tools.shorturl(url);
let Data_columns = [
{
"thumbnailImageUrl": "https://www.line-community.me/awards/uimage/5b46aedf851f74a6af8eaab8",
"imageBackgroundColor": "#FFFFFF",
"title": "點擊綁定",
"text": "點擊後將會進行綁定",
"defaultAction": {
"type": "uri",
"label": "點擊綁定",
"uri": shorturl
},
"actions": [
{
"type": "uri",
"label": "點擊綁定",
"uri": shorturl
}
]
}
];
return new Promise((resolve, reject) => {
// 傳入 resolve 與 reject表示資料成功與失敗
resolve(Data_columns);
// reject()
});
}
2021-04-27 00:42:22 +00:00
/**
* 取得表
* @param Url Url
* @param arrange 是否需要整理
*/
GetData(Url, Data) {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 400) {
var response = xhr.responseText;
resolve(response);
} else {
reject("");
}
}
};
xhr.open("POST", Url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(Data));
});
}
}
2021-04-24 13:18:34 +00:00
module.exports = CPBLClass