add youtube
This commit is contained in:
@@ -1,6 +1,30 @@
|
||||
const axios = require('axios')
|
||||
const config = require('../../config')
|
||||
|
||||
const queryYoutubeName = async (id = '') => {
|
||||
if (typeof id !== 'string' || id.trim().length === 0) return null
|
||||
id = id.trim()
|
||||
let url = `https://www.googleapis.com/youtube/v3/channels`
|
||||
let params = {
|
||||
id,
|
||||
part: `snippet`,
|
||||
key: config.google.apikey
|
||||
}
|
||||
try {
|
||||
let result = await axios({
|
||||
url,
|
||||
method: 'get',
|
||||
params
|
||||
})
|
||||
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]
|
||||
if (!('snippet' in data) || !('title' in data.snippet)) return null
|
||||
return data.snippet.title
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const subYoutube = async (id = '') => {
|
||||
let url = 'https://pubsubhubbub.appspot.com/subscribe'
|
||||
if (typeof id !== 'string' || id.trim().length === 0) return null
|
||||
@@ -24,5 +48,6 @@ const subYoutube = async (id = '') => {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
subYoutube
|
||||
subYoutube,
|
||||
queryYoutubeName
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
const twitch = require('./twitch')
|
||||
const line = require('./line')
|
||||
const google = require('./google')
|
||||
|
||||
module.exports = {
|
||||
twitch,
|
||||
line
|
||||
line,
|
||||
google
|
||||
}
|
||||
|
||||
@@ -375,6 +375,74 @@ const delTwitch = async (txt = '', source = {}, db) => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* add youtube channel notify to group
|
||||
* @param {string} txt command body format => youtube tmpl
|
||||
* @param {object} source
|
||||
* @param {object} db
|
||||
*/
|
||||
const addYoutube = async (txt = '', source = {}, db) => {
|
||||
if (!db) return null
|
||||
if (!('type' in source) || !('groupId' in source) || !('userId' in source)) return null
|
||||
let {groupId, userId} = source
|
||||
let arr = txt.split(' ')
|
||||
if (arr.length < 2) return null
|
||||
let id = arr[0]
|
||||
let tmpl = arr.slice(1).join(' ')
|
||||
let text = `select "id","owner" from "public"."line_group" where "id" = $1`
|
||||
let values = [groupId]
|
||||
let result = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
if (result.rowCount === 0) {
|
||||
let reply = 'group not register'
|
||||
return { reply }
|
||||
}
|
||||
if (result.rows[0].owner !== userId) {
|
||||
let reply = 'not owner'
|
||||
return { reply }
|
||||
}
|
||||
|
||||
let youtubeName = await api.google.queryYoutubeName(id)
|
||||
if (youtubeName === null) return null
|
||||
|
||||
text = `select "id" from "public"."youtube_channel" where "id" = $1`
|
||||
values = [id]
|
||||
let ytch = await db.query({
|
||||
text,
|
||||
values
|
||||
})
|
||||
|
||||
if (ytch.rowCount === 0) {
|
||||
let text = `insert into "public"."youtube_channel" ("id", "name", "lastvideo", "expire", "ctime", "mtime") values
|
||||
($1, $2, '', -1, now(), now())`
|
||||
let values = [id, youtubeName]
|
||||
await db.query({
|
||||
text, values
|
||||
})
|
||||
text = `insert into "public"."line_youtube_rt" ("line", "youtube", "tmpl") values ($1, $2, $3)`
|
||||
values = [groupId, id, tmpl]
|
||||
await db.query({
|
||||
text, values
|
||||
})
|
||||
} else {
|
||||
let text = `insert into "public"."line_youtube_rt" ("line", "youtube", "tmpl") values ($1, $2, $3)`
|
||||
let values = [groupId, id, tmpl]
|
||||
await db.query({
|
||||
text, values
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
await api.google.subYoutube(id)
|
||||
} catch (err) {}
|
||||
|
||||
return {
|
||||
reply: 'add youtube channel success'
|
||||
}
|
||||
}
|
||||
|
||||
const hello = async () => {
|
||||
return 'Hello World'
|
||||
}
|
||||
@@ -385,5 +453,6 @@ module.exports = {
|
||||
delpage: run.bind(null, delPage),
|
||||
addtwitch: run.bind(null, addTwitch),
|
||||
deltwitch: run.bind(null, delTwitch),
|
||||
addyoutube: run.bind(null, addYoutube),
|
||||
hello: run.bind(null, hello)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user