mtfosbot/background.js
2018-06-28 17:39:11 +08:00

201 lines
4.8 KiB
JavaScript

const cron = require('cron')
const DB = require('./libs/database')
const fbParser = require('./libs/facebook-pageparse')
const api = require('./libs/api-action')
new cron.CronJob({ // eslint-disable-line
cronTime: '00 */2 * * * *',
onTick: async () => {
console.log('run cron')
let db = await DB.connect()
let text = `select "id" as page, "lastpost" as post from "public"."facebook_page"`
let res = await db.query({
text
})
console.log('rows length :::: ', res.rowCount)
if (res.rows.legnth === 0) return
res.rows.forEach(t => {
runCheckPage(t)
})
db.release()
},
runOnInit: true,
start: true,
timeZone: 'Asia/Taipei'
})
const runCheckPage = async function (t) {
let db = await DB.connect()
let data = null
try {
data = await fbParser.getLastPost(t.page)
} catch (err) {
db.release()
}
if (data === null) {
db.release()
return
}
let n = Math.floor(Date.now() / 1000)
if (t.post === data.id || data.time < (n - 10 * 60)) {
db.release()
return
}
t.post = data.id
try {
let text = `update "public"."facebook_page" set "lastpost" = $1, "mtime" = now() where "id" = $2`
let values = [data.id, t.page]
await db.query({
text,
values
})
} catch (err) {
console.log(err)
}
let rt = null
try {
let text = `select rt."facebook" as page, rt."tmpl" as tmpl, rt."line" as group
from "public"."line_fb_rt" rt
left join "public"."line_group" line
on line."id" = rt."line"
where
line."notify" = true
and rt."facebook" = $1`
let values = [t.page]
rt = await db.query({
text,
values
})
} catch (err) {
console.log(err)
}
if (rt === null || rt.rowCount === 0) {
db.release()
return
}
for (let i in rt.rows) {
let tmp = rt.rows[i]
let msg = tmp.tmpl || ''
if (typeof msg !== 'string' || msg.trim().length === 0) {
msg = `${data.txt}\n${data.link}`
} else {
msg = msg.replace(/{link}/, data.link).replace(/{txt}/, data.txt).replace(/\\n/, '\n')
}
api.line.pushMessage(tmp.group, msg).then(() => { }).catch(() => { })
}
db.release()
}
// register twitch streaming webhook
new cron.CronJob({ //eslint-disable-line
cronTime: '*/20 * * * * *',
onTick: async () => {
let db = await DB.connect()
let text = `select "id" from "public"."twitch_channel"`
try {
let twch = await db.query({
text
})
console.log('check twitch channel number :::: ', twch.rowCount)
let ids = twch.rows.map(t => t.id)
let streams = await api.twitch.getUserStream(ids)
if (streams !== null && Array.isArray(streams)) {
streams.forEach(t => {
sendStreamNotify(t).then(() => {}).catch(() => {})
})
}
} catch (err) {
console.log(err)
db.release()
return
}
db.release()
},
runOnInit: true,
start: true,
timeZone: 'Asia/Taipei'
})
const sendStreamNotify = async (streamer = null) => {
console.log(streamer)
if (streamer === null || typeof streamer !== 'object' || !('user_id' in streamer) || !('id' in streamer)) return null
let db = await DB.connect()
try {
let result = await db.query({
text: `select "id" from "public"."twitch_channel" where "id" = $1 and "laststream" = $2`,
values: [streamer.user_id, streamer.id]
})
if (result.rowCount > 0) {
db.release()
return
}
} catch (err) {
console.log(err)
db.release()
return
}
try {
let text = `update "public"."twitch_channel" set "laststream" = $1, "mtime" = now() where "id" = $2`
let values = [streamer.id, streamer.user_id]
await db.query({
text,
values
})
} catch (err) {
console.log(err)
}
let rt = null
try {
let text = `select rt."tmpl" as tmpl, line."id" as group, twitch."laststream" as laststream, twitch."name" as user from "public"."line_twitch_rt" rt
left join "public"."line_group" line
on line."id" = rt."line"
left join "public"."twitch_channel" twitch
on twitch."id" = rt."twitch"
where
twitch."id" = $1
and line."notify" = true
and rt."type" = 'live'`
let values = [streamer.user_id]
rt = await db.query({
text,
values
})
} catch (err) {
console.log(err)
db.release()
return
}
if (rt === null || rt.rowCount === 0) {
db.release()
return
}
let twLink = 'https://twitch.tv/'
for (let i in rt.rows) {
let tmp = rt.rows[i]
let link = twLink + tmp.user
let msg = tmp.tmpl || ''
if (typeof msg !== 'string' || msg.trim().length === 0) {
msg = `${streamer.title || ''}\n${link}`
} else {
msg = msg.replace(/{link}/, link).replace(/{txt}/, streamer.title).replace(/\\n/, '\n')
}
api.line.pushMessage(tmp.group, msg).then(() => { }).catch(() => { })
}
db.release()
}