Skip to content

Commit

Permalink
Example that replaces youtube video pages with a custom page that is …
Browse files Browse the repository at this point in the history
…capable of streaming the video through the proxy.

Relates to #103 and #162
  • Loading branch information
nfriedly committed Mar 3, 2021
1 parent f456dc4 commit ba09b57
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 1 deletion.
114 changes: 114 additions & 0 deletions examples/youtube/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/youtube/package.json
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"
}
}
34 changes: 34 additions & 0 deletions examples/youtube/server.js
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/");
47 changes: 47 additions & 0 deletions examples/youtube/youtube.js
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, '&amp;')}">`).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,
}
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ near term.

Additionally, websockets are not currently supported. However, some websocket libraries, such as socket.io and engine.io will start with or fall back to long-poling automatically, which _is_ supported.

More advanced websites, such as Roblox, Discord, Youtube, Instagram, etc. do not currently work. At the moment, there is no timeframe for when these might be supported.
More advanced websites, such as Roblox, Discord, YouTube*, Instagram, etc. do not currently work. At the moment, there is no timeframe for when these might be supported.

* There is an example that detects YouTube video pages and [replaces them with a custom page that just streams the video](examples/youtube/).

Patches are welcome, including both general-purpose improvements to go into the main library, and site-specific
fixes to go in the examples folder.
Expand Down

0 comments on commit ba09b57

Please sign in to comment.