-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example that replaces youtube video pages with a custom page that is …
- Loading branch information
Showing
5 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "unblocker-youtube-example", | ||
"private": true, | ||
"version": "0.0.0", | ||
"dependencies": { | ||
"unblocker": "*", | ||
"ytdl-core": "^4.5.0" | ||
}, | ||
"scripts": { | ||
"start": "node server.js" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"use strict"; | ||
|
||
var http = require("http"); | ||
var Unblocker = require("unblocker"); | ||
var youtube = require('./youtube.js'); | ||
|
||
var unblocker = Unblocker({ | ||
requestMiddleware: [youtube.processRequest], | ||
}); | ||
|
||
http | ||
.createServer(function(req, res) { | ||
// first let unblocker try to handle the requests | ||
unblocker(req, res, function(err) { | ||
// this callback will be fired for any request that unblocker does not serve | ||
var headers = { "content-type": "text/html" }; | ||
if (err) { | ||
res.writeHead(500, headers); | ||
return res.end(err.stack || err); | ||
} | ||
if (req.url == "/") { | ||
res.writeHead(200, headers); | ||
return res.end( | ||
'Visit a link such as <a href="/proxy/https://www.youtube.com/watch?v=dQw4w9WgXcQ"><script>document.write(window.location)</script>proxy/https://www.youtube.com/watch?v=dQw4w9WgXcQ</a> to see the magic.' | ||
); | ||
} else { | ||
res.writeHead(404, headers); | ||
return res.end("Error 404: file not found."); | ||
} | ||
}); | ||
}) | ||
.listen(8080); | ||
|
||
console.log("proxy server live at http://localhost:8080/"); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"use strict"; | ||
|
||
const ytdl = require('ytdl-core'); | ||
|
||
// handles youtube video pages and replaces them with a custom page that just streams the video | ||
function processRequest(data) { | ||
const { hostname, pathname } = new URL(data.url); | ||
if (hostname === 'www.youtube.com' && pathname === "/watch") { | ||
const res = data.clientResponse; | ||
// if we write headers, unblocker will detect that and stop trying to process this request | ||
res.writeHead(200, {"content-type": "text/html; charset=utf-8"}); | ||
|
||
|
||
// todo: use config.prefix instead of hard-coding '/proxy/' into the url | ||
ytdl.getInfo(data.url).then(info => { | ||
|
||
// only use formats with combined audio and video (note these tend to be lower resolution) | ||
const formats = ytdl.filterFormats(info.formats, 'audioandvideo'); | ||
|
||
const thumb = info.videoDetails.thumbnails.pop(); | ||
|
||
res.end( | ||
`<!DOCTYPE html> | ||
<head> | ||
<title>${info.videoDetails.title}</title> | ||
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW"/> | ||
</head> | ||
<body> | ||
<h1>${info.videoDetails.title}</h1> | ||
<video controls poster="${thumb.url}" style="width: 100%"> | ||
${formats.map(format => ` <source type="${format.mimeType.split(';').shift()}" src="/proxy/${format.url.replace(/&/g, '&')}">`).join('\n')} | ||
</video> | ||
<p>${info.videoDetails.description.replace(/[\n]/g, '\n<br>')}</p> | ||
</body> | ||
</html> | ||
`); | ||
}).catch(err => { | ||
console.error(`Error getting info for ${data.url}`, err); | ||
res.end('Error retrieving video info'); | ||
}) | ||
|
||
} | ||
} | ||
|
||
module.exports = { | ||
processRequest, | ||
} |
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