add youtube new video notify
This commit is contained in:
parent
fe018cf9e7
commit
5ed1e462b3
@ -3,7 +3,9 @@
|
|||||||
{"file": "main.sql", "version": 1},
|
{"file": "main.sql", "version": 1},
|
||||||
{"file": "20180628-1.sql", "version": 2},
|
{"file": "20180628-1.sql", "version": 2},
|
||||||
{"file": "20180628-2.sql", "version": 3},
|
{"file": "20180628-2.sql", "version": 3},
|
||||||
{"file": "20180702-1.sql", "version": 4}
|
{"file": "20180702-1.sql", "version": 4},
|
||||||
|
{"file": "20180706-1.sql", "version": 5},
|
||||||
|
{"file": "20180710-1.sql", "version": 6}
|
||||||
],
|
],
|
||||||
"test": []
|
"test": []
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const config = require('../../config')
|
const config = require('../../config')
|
||||||
|
const qs = require('querystring')
|
||||||
|
|
||||||
const queryYoutubeName = async (id = '') => {
|
const queryYoutubeName = async (id = '') => {
|
||||||
if (typeof id !== 'string' || id.trim().length === 0) return null
|
if (typeof id !== 'string' || id.trim().length === 0) return null
|
||||||
@ -16,11 +17,14 @@ const queryYoutubeName = async (id = '') => {
|
|||||||
method: 'get',
|
method: 'get',
|
||||||
params
|
params
|
||||||
})
|
})
|
||||||
if (!result || !('data' in result) || !('items' in result.data) || Array.isArray(result.data.items) || result.data.items.length === 0) return null
|
|
||||||
|
if (!result || !('data' in result) || !('items' in result.data) || !Array.isArray(result.data.items) || result.data.items.length === 0) return null
|
||||||
let data = result.data.items[0]
|
let data = result.data.items[0]
|
||||||
|
console.log('youtube data ::: ', JSON.stringify(data, null, 2))
|
||||||
if (!('snippet' in data) || !('title' in data.snippet)) return null
|
if (!('snippet' in data) || !('title' in data.snippet)) return null
|
||||||
return data.snippet.title
|
return data.snippet.title
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -32,6 +36,7 @@ const subYoutube = async (id = '') => {
|
|||||||
|
|
||||||
let data = {
|
let data = {
|
||||||
'hub.mode': 'subscribe',
|
'hub.mode': 'subscribe',
|
||||||
|
'hub.verify': 'async',
|
||||||
'hub.topic': `https://www.youtube.com/xml/feeds/videos.xml?channel_id=${id}`,
|
'hub.topic': `https://www.youtube.com/xml/feeds/videos.xml?channel_id=${id}`,
|
||||||
'hub.callback': `${config.url.replace(/\/$/, '')}/google/youtube/webhook?id=${id}`,
|
'hub.callback': `${config.url.replace(/\/$/, '')}/google/youtube/webhook?id=${id}`,
|
||||||
'hub.lease_seconds': 86400
|
'hub.lease_seconds': 86400
|
||||||
@ -39,11 +44,16 @@ const subYoutube = async (id = '') => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await axios({
|
await axios({
|
||||||
data,
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||||||
|
},
|
||||||
|
data: qs.stringify(data),
|
||||||
url,
|
url,
|
||||||
method: 'post'
|
method: 'post'
|
||||||
})
|
})
|
||||||
} catch (err) {}
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,7 +404,9 @@ const addYoutube = async (txt = '', source = {}, db) => {
|
|||||||
return { reply }
|
return { reply }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('youtube id ::: ', id)
|
||||||
let youtubeName = await api.google.queryYoutubeName(id)
|
let youtubeName = await api.google.queryYoutubeName(id)
|
||||||
|
console.log('youtubr name :::: ', youtubeName)
|
||||||
if (youtubeName === null) return null
|
if (youtubeName === null) return null
|
||||||
|
|
||||||
text = `select "id" from "public"."youtube_channel" where "id" = $1`
|
text = `select "id" from "public"."youtube_channel" where "id" = $1`
|
||||||
@ -436,7 +438,9 @@ const addYoutube = async (txt = '', source = {}, db) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await api.google.subYoutube(id)
|
await api.google.subYoutube(id)
|
||||||
} catch (err) {}
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
reply: 'add youtube channel success'
|
reply: 'add youtube channel success'
|
||||||
|
@ -27,7 +27,7 @@ const runAct = async (cmd, txt = '', source = {}) => {
|
|||||||
if (!cmd || typeof cmd !== 'string' || cmd.length === 0) return null
|
if (!cmd || typeof cmd !== 'string' || cmd.length === 0) return null
|
||||||
if (!(cmd in cmds)) return null
|
if (!(cmd in cmds)) return null
|
||||||
let result = await cmds[cmd](txt, source)
|
let result = await cmds[cmd](txt, source)
|
||||||
return result
|
return result.reply
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = runAct
|
module.exports = runAct
|
||||||
|
@ -17,6 +17,7 @@ const textMessage = async (evt) => {
|
|||||||
let result = await commands(text, source)
|
let result = await commands(text, source)
|
||||||
if (result === null) return
|
if (result === null) return
|
||||||
if (typeof result === 'object' && 'reply' in result) {
|
if (typeof result === 'object' && 'reply' in result) {
|
||||||
|
console.log('reply message :::: ', result.reply)
|
||||||
await api.line.replyMessage(replyToken, result.reply)
|
await api.line.replyMessage(replyToken, result.reply)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,17 +5,28 @@ const r = new Router()
|
|||||||
const {
|
const {
|
||||||
getRaw
|
getRaw
|
||||||
} = require('../../libs/middleware')
|
} = require('../../libs/middleware')
|
||||||
const {
|
const api = require('../../libs/api-action')
|
||||||
textMessage
|
const DB = require('../../libs/database')
|
||||||
} = require('../../libs/line-message')
|
|
||||||
|
|
||||||
r.get('/youtube/webhook', async (c, n) => {
|
r.get('/youtube/webhook', async (c, n) => {
|
||||||
|
let db = await DB.connect()
|
||||||
let mode = c.query['hub.mode'] || ''
|
let mode = c.query['hub.mode'] || ''
|
||||||
let verifyToken = c.query['hub.verify_token'] || ''
|
// let verifyToken = c.query['hub.verify_token'] || ''
|
||||||
let challenge = c.query['hub.challenge']
|
let challenge = c.query['hub.challenge']
|
||||||
|
|
||||||
|
let id = c.query['id'] || ''
|
||||||
|
|
||||||
if (mode) {
|
if (mode) {
|
||||||
if (mode === 'subscribe') {
|
if (mode === 'subscribe' && typeof id === 'string' && id.trim().length > 0) {
|
||||||
|
let time = Math.floor((Date.now() + (86400 * 1000)) / 1000)
|
||||||
|
try {
|
||||||
|
await db.query({
|
||||||
|
text: `update "public"."youtube_channel" set "expire" = $1, "mtime" = now() where "id" = $2`,
|
||||||
|
values: [time, id]
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
c.status = 200
|
c.status = 200
|
||||||
c.body = challenge
|
c.body = challenge
|
||||||
} else {
|
} else {
|
||||||
@ -23,10 +34,66 @@ r.get('/youtube/webhook', async (c, n) => {
|
|||||||
c.body = ''
|
c.body = ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.release()
|
||||||
})
|
})
|
||||||
|
|
||||||
r.post('/youtube/webhook', getRaw, async (c, n) => {
|
r.post('/youtube/webhook', getRaw, async (c, n) => {
|
||||||
console.log(JSON.stringify(c.request.body, null, 2))
|
let db = await DB.connect()
|
||||||
|
let xmlStr = c.request.raw.toString()
|
||||||
|
// console.log(xmlStr)
|
||||||
|
let data = await new Promise((resolve) => {
|
||||||
|
xml2js.parseString(xmlStr, (err, res) => {
|
||||||
|
if (err) return resolve(null)
|
||||||
|
resolve(res)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
// console.log(JSON.stringify(data, null, 2))
|
||||||
|
|
||||||
|
try {
|
||||||
|
let ytid = data.feed.entry[0].id[0]
|
||||||
|
let title = data.feed.entry[0].title[0]
|
||||||
|
let link = data.feed.entry[0].link[0]['$']['href']
|
||||||
|
let id = c.query['id'] || ''
|
||||||
|
|
||||||
|
console.log(`yt: ${ytid} / link: ${link} / id: ${id}`)
|
||||||
|
|
||||||
|
let text = `select rt."tmpl" as tmpl, line."id" as group from "public"."line_youtube_rt" rt
|
||||||
|
left join "public"."youtube_channel" ch
|
||||||
|
on ch."id" = rt."youtube"
|
||||||
|
left join "public"."line_group" line
|
||||||
|
on line."id" = rt."line"
|
||||||
|
where
|
||||||
|
ch."id" = $1
|
||||||
|
and ch."lastvideo" != $2
|
||||||
|
and line."notify" = true`
|
||||||
|
let values = [id, ytid]
|
||||||
|
let result = await db.query({
|
||||||
|
text,
|
||||||
|
values
|
||||||
|
})
|
||||||
|
if (result.rowCount > 0) {
|
||||||
|
result.rows.forEach(t => {
|
||||||
|
let msg = t.tmpl || ''
|
||||||
|
if (typeof msg !== 'string' || msg.trim().length === 0) {
|
||||||
|
msg = `${title || ''}\n${link}`
|
||||||
|
} else {
|
||||||
|
msg = msg.replace(/{link}/, link).replace(/{txt}/, title).replace(/\\n/, '\n')
|
||||||
|
}
|
||||||
|
api.line.pushMessage(t.group, msg).then(() => {}).catch(() => {})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
await db.query({
|
||||||
|
text: `update "public"."youtube_channel" set "lastvideo" = $1, "mtime" = now() where "id" = $2`,
|
||||||
|
values: [ytid, id]
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.status = 200
|
||||||
|
c.body = 'success'
|
||||||
|
db.release()
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = r
|
module.exports = r
|
||||||
|
2
schema/20180710-1.sql
Normal file
2
schema/20180710-1.sql
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
insert into "public"."commands" ("cmd", "message", "ctime", "mtime", "group") values
|
||||||
|
('addyoutube', '{{addyoutube}}', now(), now(), '');
|
Loading…
Reference in New Issue
Block a user