Created
September 19, 2019 12:22
-
-
Save kimjisub/fc623d9e5a02c33500c5e39f159e7a95 to your computer and use it in GitHub Desktop.
Youtube parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const req = require('request-promise'); | |
module.exports = async (query) => { | |
const html = await req.get(`https://www.youtube.com/results?search_query=${ encodeURI(query) }`); | |
const result = { list: [] }; | |
const s = html.toString().split('/watch?v=') | |
const limit = 10; | |
let cnt = 0, data = {}; | |
for (let i = 1; cnt < limit && i < s.length; i++) { | |
const id = s[i].split('"')[0]; | |
if (id !== data.id) { | |
if (data.title && data.id && !data.id.includes('&')) { | |
result.list.push(data); | |
cnt++; | |
} | |
data = { id: id }; | |
continue; | |
} | |
try { | |
data.title = s[i].split('title="')[1].split('"')[0]; | |
} catch (error) { | |
} | |
} | |
result.count = result.list.length; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment