Skip to content

Commit

Permalink
Merge pull request #365 from KusStar/youtube_search
Browse files Browse the repository at this point in the history
修改 provider/youtube
  • Loading branch information
nondanee committed Jan 28, 2020
2 parents 7ded051 + 61cb020 commit d6d632e
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions provider/youtube.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const cache = require('../cache')
const request = require('../request')
const parse = query => query.split('&').map(pair => pair.split('=')).reduce((result, item) => Object.assign({}, result, {[item[0]]: item[1]}), {})
const querystring = require('querystring')

// const proxy = require('url').parse('http://127.0.0.1:1080')
const proxy = undefined
const key = 'YOUR_API_KEY'
// YouTube Data API v3
const key = undefined

const signature = (id = '-tKVN2mAKRI') => {
let url =
Expand All @@ -26,6 +27,10 @@ const signature = (id = '-tKVN2mAKRI') => {
})
}

/**
* @description 使用 Youtube Data API 搜索
* @information 需要申请 API key, 当无匹配结果时尝试调用 searchWithoutKey
*/
const search = info => {
let url =
`https://www.googleapis.com/youtube/v3/search?part=snippet&q=${encodeURIComponent(info.keyword)}&type=video&key=${key}`
Expand All @@ -34,12 +39,32 @@ const search = info => {
.then(response => response.json())
.then(jsonBody => {
let matched = jsonBody.items[0]
if(matched)
if (matched)
return matched.id.videoId
else
return Promise.reject()
return searchWithoutKey(info)
})
}

/**
* @description 爬搜索网页,正则匹配,返回第一个视频的 id
* @information 这里需要使用非 Chrome 的 User-Agent
*/
const searchWithoutKey = info => {
const query = encodeURIComponent(info.keyword)
const url = `https://www.youtube.com/results?search_query=${query}&app=desktop`
const customHeader = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1 WOW64 rv:33.0) Gecko/20120101 Firefox/33.0'}

return request('GET', url, customHeader, null, proxy)
.then(response => response.body())
.then(html => {
const matched = html.match(/data-context-item-id="(.{11})"/g)[0]
if (matched) {
matched.match(/.*="(.{11})"/)
return RegExp.$1
}
return Promise.reject()
})

}

const track = id => {
Expand All @@ -60,6 +85,9 @@ const track = id => {
})
}

const check = info => cache(search, info).then(track)
const check = info => {
const searchFunc = key ? search : searchWithoutKey
return cache(searchFunc, info).then(track)
}

module.exports = {check, track}

0 comments on commit d6d632e

Please sign in to comment.