add youtube

This commit is contained in:
Jay 2018-07-06 17:57:49 +08:00
parent 653dbe5fb6
commit fe018cf9e7
6 changed files with 166 additions and 3 deletions

View File

@ -92,7 +92,7 @@ const runCheckPage = async function (t) {
db.release()
}
// register twitch streaming webhook
// check twitch streaming status
new cron.CronJob({ //eslint-disable-line
cronTime: '*/20 * * * * *',
onTick: async () => {
@ -196,3 +196,38 @@ const sendStreamNotify = async (streamer = null) => {
db.release()
}
// check youtube webhook
new cron.CronJob({ //eslint-disable-line
cronTime: '00 00 */3 * * *',
onTick: async () => {
let db = await DB.connect()
try {
let time = Date.now() + (4 * 1000 * 60 * 60)
let text = `select "id", "name", "lastvideo", "expire" from "public"."youtube_channel" where "expire" = -1 or "expire" < $1`
let values = [time]
let result = await db.query({
text, values
})
result.rows.forEach(t => {
regYoutubeWebhook(t).then(() => {}).catch(() => {})
})
} catch (err) {}
db.release()
},
runOnInit: true,
start: true,
timeZone: 'Asia/Taipei'
})
const regYoutubeWebhook = async (yt = null) => {
if (yt === null || typeof yt !== 'object' || !('id' in yt)) return null
try {
await api.google.subYoutube(yt.id)
} catch (err) {
console.log(err)
}
}

View File

@ -9,6 +9,9 @@ module.exports = {
clientid: process.env.TWITCH_CLIENT_ID || '',
subsecret: process.env.TWITCH_SUB_SECRET || ''
},
google: {
apikey: process.env.GOOGLE_API_KEY || ''
},
database: {
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 5432,

View File

@ -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
}

View File

@ -1,7 +1,9 @@
const twitch = require('./twitch')
const line = require('./line')
const google = require('./google')
module.exports = {
twitch,
line
line,
google
}

View File

@ -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)
}

29
schema/20180706-1.sql Normal file
View File

@ -0,0 +1,29 @@
-- auto-generated definition
create table public.youtube_channel
(
id varchar(150) not null
constraint youtube_channel_pkey
primary key,
name varchar(150) not null,
lastvideo varchar(150) default '' :: character varying not null,
expire integer default '-1' :: integer not null,
ctime timestamp with time zone default now() not null,
mtime timestamp with time zone default now() not null
);
-- auto-generated definition
create table public.line_youtube_rt
(
line varchar(100) not null
constraint line_youtube_rt_line_group_id_fk
references public.line_group
on delete cascade,
youtube varchar(150) not null
constraint line_youtube_rt_youtube_channel_id_fk
references public.youtube_channel
on delete cascade,
tmpl varchar(300) default '' :: character varying not null,
constraint line_youtube_rt_line_youtube_pk
primary key (line, youtube)
);