2018-06-26 16:59:53 +00:00
|
|
|
const cron = require('cron')
|
|
|
|
const DB = require('./libs/database')
|
2018-06-26 10:04:19 +00:00
|
|
|
const fbParser = require('./libs/facebook-pageparse')
|
2018-06-28 09:37:33 +00:00
|
|
|
const api = require('./libs/api-action')
|
2018-06-26 10:04:19 +00:00
|
|
|
|
2018-06-26 16:59:53 +00:00
|
|
|
new cron.CronJob({ // eslint-disable-line
|
|
|
|
cronTime: '00 */2 * * * *',
|
|
|
|
onTick: async () => {
|
|
|
|
console.log('run cron')
|
|
|
|
let db = await DB.connect()
|
2018-06-27 15:38:56 +00:00
|
|
|
let text = `select "id" as page, "lastpost" as post from "public"."facebook_page"`
|
2018-06-26 16:59:53 +00:00
|
|
|
let res = await db.query({
|
|
|
|
text
|
|
|
|
})
|
|
|
|
console.log('rows length :::: ', res.rowCount)
|
|
|
|
if (res.rows.legnth === 0) return
|
2018-06-27 15:38:56 +00:00
|
|
|
res.rows.forEach(t => {
|
|
|
|
runCheckPage(t)
|
2018-06-26 16:59:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
db.release()
|
2018-06-26 10:04:19 +00:00
|
|
|
},
|
2018-06-27 09:50:56 +00:00
|
|
|
runOnInit: true,
|
|
|
|
start: true,
|
|
|
|
timeZone: 'Asia/Taipei'
|
|
|
|
})
|
|
|
|
|
2018-06-27 15:38:56 +00:00
|
|
|
const runCheckPage = async function (t) {
|
|
|
|
let db = await DB.connect()
|
|
|
|
|
2018-06-28 09:37:33 +00:00
|
|
|
let data = null
|
|
|
|
try {
|
|
|
|
data = await fbParser.getLastPost(t.page)
|
|
|
|
} catch (err) {
|
|
|
|
db.release()
|
|
|
|
}
|
|
|
|
if (data === null) {
|
|
|
|
db.release()
|
|
|
|
return
|
|
|
|
}
|
2018-06-27 15:38:56 +00:00
|
|
|
|
2018-06-28 09:37:33 +00:00
|
|
|
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
|
2018-06-27 15:38:56 +00:00
|
|
|
})
|
2018-06-28 09:37:33 +00:00
|
|
|
} 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
|
2018-06-27 15:38:56 +00:00
|
|
|
})
|
2018-06-28 09:37:33 +00:00
|
|
|
} 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()
|
2018-06-27 15:38:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 09:50:56 +00:00
|
|
|
// register twitch streaming webhook
|
|
|
|
new cron.CronJob({ //eslint-disable-line
|
2018-06-28 09:37:33 +00:00
|
|
|
cronTime: '*/20 * * * * *',
|
2018-06-27 09:50:56 +00:00
|
|
|
onTick: async () => {
|
2018-06-28 09:37:33 +00:00
|
|
|
let db = await DB.connect()
|
2018-06-27 09:50:56 +00:00
|
|
|
|
2018-06-28 09:37:33 +00:00
|
|
|
let text = `select "id" from "public"."twitch_channel"`
|
|
|
|
try {
|
|
|
|
let twch = await db.query({
|
|
|
|
text
|
|
|
|
})
|
|
|
|
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()
|
2018-06-27 09:50:56 +00:00
|
|
|
},
|
|
|
|
runOnInit: true,
|
2018-06-26 10:04:19 +00:00
|
|
|
start: true,
|
|
|
|
timeZone: 'Asia/Taipei'
|
|
|
|
})
|
2018-06-28 09:37:33 +00:00
|
|
|
|
|
|
|
const sendStreamNotify = async (streamer = null) => {
|
|
|
|
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
|
2018-06-28 09:39:11 +00:00
|
|
|
and line."notify" = true
|
|
|
|
and rt."type" = 'live'`
|
2018-06-28 09:37:33 +00:00
|
|
|
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()
|
|
|
|
}
|