Perhaps you would like to have a look at this code i wrote to minimize
some youtube loading
Have a look
#!/usr/bin/env node
/*
Constructing YouTubeStrip.js using wait.for package
for making things simple and elegant.
*/
var http = require ("http");
var url = require('url');
var fs = require("fs");
var cluster = require("cluster");
var numCPUs = require('os').cpus().length;
var Step = require('/usr/lib/node_modules/step');
var ch = require('/usr/lib/node_modules/cheerio');
// thinking of using pump package (future version)
// adding gzip module for making stream allot more compact
var gzip = require('zlib');
var SCRIPT = fs.readFileSync('./YP2.js','ascii');
SCRIPT='<script>'+SCRIPT+'</script>';
// Main Clustering Method and server start up
// Start1
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
}else {
var server = http.createServer(function (request, response) {
processRequest(request,response);
});
server.listen(8888);
}
// End1
// processRequest function that gets and delivers data
var processRequest=function(request,response){
var TempReq=url.parse(request.url,true);
//console.log(TempReq);
var ClientRequest = {
'method' : request['method'] || 'GET',
'headers' : request['headers'],
'hostname' : TempReq['hostname'],
'port' : request['port'] || 80,
'path' : TempReq['path'] || '',
'agent' : false,
};
ClientRequest['headers']['accept-encoding']='';
var ResData='';
http.request(ClientRequest,function(res){
res.on('data',function(data){
ResData+=data;
});
res.on('end',function(){
console.log('done reading data ');
// now we start Step Sequencer
Step(
function CheckHeaders(){
//checking headers for content-encoding
if
(res['headers'].hasOwnProperty('content-encoding') ){
switch(res['headers']['content-encoding']) {
case 'gzip':
return gzip.gunzip(ResData,this);
break;
case 'deflate' :
return gzip.inflate(ResData,this);
break;
default :
return ResData;
break;
}
}else {
return ResData;
}
},
function SplitEncoding(err,enc){
$ = ch.load(enc);
$('body').append(SCRIPT);
var ResponseHeaders=res['headers'];
ResponseHeaders['X-Encoded-By']='Faysal Banna';
ResponseHeaders['X-Encoded-Contact-Info']='[email protected],
+961-3-258043';
delete ResponseHeaders['content-length'];
response.writeHead(res.statusCode,ResponseHeaders);
switch(res['headers']['content-encoding']) {
case 'gzip':
// here we need to transfirm gzip encoding
console.log('gzip Encoding ');
zlib.gzip(enc,function(err,data){
response.end(data);
});
break;
case 'deflate' :
// here we need to transfer deflate encoding
zlib.deflate(enc,function(err,data){
response.end(data);
})
console.log('deflate Encoding');
break;
default :
// here no encoding done just plain text
console.log('default no-encoding');
response.end($.html(),'utf8');
enc=null;
$=null;
break;
}
}
);
});
}).end();
}
and YP2.js is just simple script taken and modified from userscripts.org
website
// ==UserScript==
// @name Youtube High Definition
// @namespace lenni
// @version 1.3.5
// @updateURL https://userscripts.org/scripts/source/127028.meta.js
// @grant none
// @include *youtube.com/watch*
// @include *youtube.com/user*
// ==/UserScript==
// Author: www.lennart-glauer.de
// Date: 02.08.13
// License: GNU General Public License v3 (GPL)
// Url: http://userscripts.org/scripts/show/127028
// contentEval (http://wiki.greasespot.net/Content_Script_Injection)
(function(source){
// Check for function input.
if ('function' == typeof source) {
// Execute this function with no arguments, by adding parentheses.
// One set around the function, required for valid syntax, and a
// second empty set calls the surrounded function.
source = '(' + source + ')();'
}
// Create a script node holding this source code.
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = source;
// Insert the script node into the page, so it will run, and
immediately
// remove it to clean up.
document.body.appendChild(script);
document.body.removeChild(script);
})
(function(){
// Dom window
var w = window,
// Dom document
d = w.document,
// No operation
nop = function(){},
// Player object
p = null,
// onYouTubePlayerReady
_onYouTubePlayerReady = w.onYouTubePlayerReady || nop,
// ytPlayerOnYouTubePlayerReady
_ytPlayerOnYouTubePlayerReady = w.ytPlayerOnYouTubePlayerReady || nop;
// Quality levels
var q = {
'tiny':0,
'small':1,
'medium':2,
'large':3,
'hd720':4,
'hd1080':5,
'highres':6
},
// Local maximum
maximum = 'highres';
// Hook YouTubePlayerReady callbacks
w.onYouTubePlayerReady = w.ytPlayerOnYouTubePlayerReady = function(){
// Get Feather player object
if(w.videoPlayer){
for(var i in w.videoPlayer){
if(w.videoPlayer[i] &&
w.videoPlayer[i].setPlaybackQuality){
p = w.videoPlayer[i];
break;
}
}
}
// Get Flash / HTML5 player object
else{
p = d.getElementById('movie_player') ||
d.getElementById('movie_player-flash') ||
d.getElementById('movie_player-html5') ||
d.getElementById('movie_player-html5-flash');
}
// Check for valid player object
if(p){
console.log('Youtube player type: ' + typeof p);
// Greasemonkey with unsafe window: Unwrap XPCNativeWrapper
//if(typeof XPCNativeWrapper === 'function'){
// p = XPCNativeWrapper.unwrap(p);
//}
// Add onStateChange listener
p.addEventListener('onStateChange','onPlayerStateChange');
// Pause video to prevent interrupts
p.pauseVideo();
}
// Call original functions
_ytPlayerOnYouTubePlayerReady();
_onYouTubePlayerReady();
};
// onStateChange callback
w.onPlayerStateChange = function(z){
console.log('Youtube player state: ' + z);
// Catch internal exceptions to prevent flash crashs
try{
// Get actual playback quality
var aq = p.getPlaybackQuality();
// Get available video quality
var testVQ=p.getAvailableQualityLevels();
//var vq = p.getAvailableQualityLevels()[0];
var vq=testVQ[testVQ.length -1];
console.log(vq, "is vq -> ", aq ," is aq");
// Set playback quality
/*if(q[aq] < q[maximum] && q[aq] < q[vq]){
p.setPlaybackQuality(q[maximum] < q[vq] ? maximum : vq);
console.log('Youtube player quality: ' + aq + ' -> ' + vq);
}*/
p.setPlaybackQuality(vq);
// Disable hook
if(z === 1)
w.onPlayerStateChange = nop;
// Play video now
else if(z === 2)
p.playVideo();
}catch(e){
console.log('Youtube player exception: ' + e);
}
};
// HTML5 fallback
//setTimeout(w.onYouTubePlayerReady, 1500);
});
Faysal Banna
Meteorological Services
Rafic Harriri International Airport
Beirut - Lebanon
Mob: +961-3-258043
On 09/20/2013 11:20 PM, Eric wrote:
Hi guys,
I'm trying to write a service that performs the following actions:
1) receive an HTTP request from client with some data payload in body.
2) immediately reply with HTTP response 200 - ok
3) perform some calculations based on the payload and than performs an
HTTP request to an external HTTP server.
To do that once my http callback is fired I call res.send() and than I
perform the calculations, then I create a new http object (var http =
require('http')) and call it's request method. At this point the
callback for this last call is never triggered.
How should I do to reply to my http client as soon as possible and
than do another http request?
Thanks in advance,
Eric
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.