Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改 provider/youtube #365

Merged
merged 2 commits into from
Jan 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}