2018-07-03 02:27:24 +00:00
|
|
|
const axios = require('axios')
|
|
|
|
const config = require('../../config')
|
|
|
|
|
2018-07-06 09:57:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 02:27:24 +00:00
|
|
|
const subYoutube = async (id = '') => {
|
|
|
|
let url = 'https://pubsubhubbub.appspot.com/subscribe'
|
|
|
|
if (typeof id !== 'string' || id.trim().length === 0) return null
|
|
|
|
id = id.trim()
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
'hub.mode': 'subscribe',
|
|
|
|
'hub.topic': `https://www.youtube.com/xml/feeds/videos.xml?channel_id=${id}`,
|
|
|
|
'hub.callback': `${config.url.replace(/\/$/, '')}/google/youtube/webhook?id=${id}`,
|
|
|
|
'hub.lease_seconds': 86400
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await axios({
|
|
|
|
data,
|
|
|
|
url,
|
|
|
|
method: 'post'
|
|
|
|
})
|
|
|
|
} catch (err) {}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2018-07-06 09:57:49 +00:00
|
|
|
subYoutube,
|
|
|
|
queryYoutubeName
|
2018-07-03 02:27:24 +00:00
|
|
|
}
|