function process_googleeod(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,yahoo_min,days)
{
var fs = require('fs');
var csv = require('ya-csv');
var async = require('async');
try{
async.parallel({
data1 : function(callback){
if (fs.existsSync('google.csv')) {
fs.readFile('google.csv','utf-8', function(err, data){
callback(err, data);
});
}else
{
fs.readdir('./', function(err, files){
callback(err,'0');
});
}
}
}, function(err, result){
if(err) throw err;
if (fs.existsSync('google.csv')) {
var arrRow = new Array();
arrRow = result['data1'].split('\n');
if(arrRow.length>0)
{
var arrFileData = new Array();
for(var i=0 ; i<arrRow.length-1;i++)
{
arrFileData.push(arrRow[i].trim());
}
downloadAndProcessGoogleEOD(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,arrFileData, yahoo_min, 0,days);
}
else
{
sn++;
callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn);
}
}
});
}catch(err){
console.error(err);
}
}
function downloadAndProcessGoogleEOD(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,arrYahooSymbols,yahoo_min,n,days)
{
var urlStockinfo ='http://www.google.com/finance/getprices?q='+arrYahooSymbols[n].split(":")[1]+'&x='+arrYahooSymbols[n].split(":")[0]+'&i=86400&p='+days+'d&f=d,c,o,h,l,v&df=cpct&auto=1&ts=1266701290218';
var http = require('http'),
fs = require('fs'),
request = require('request');
request
(
{
method: 'POST',
uri: urlStockinfo,
headers:
{
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
"Accept-Encoding": "gzip,deflate,sdch",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Cookie": "cookie"
}
}
,function(error, response, body)
{
if(response != null)
{
if(response.statusCode != null || response.statusCode != "")
{
if (!error && response.statusCode == 200)
{
downloadgoogleeod(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,response.statusCode, urlStockinfo, arrYahooSymbols, yahoo_min, n,days);
}
else
{
downloadgoogleeod(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,404, urlStockinfo, arrYahooSymbols, yahoo_min, n,days);
}
}else
{
downloadgoogleeod(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,404, urlStockinfo, arrYahooSymbols, yahoo_min, n,days);
}
}else
{
downloadgoogleeod(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,404, urlStockinfo, arrYahooSymbols, yahoo_min, n,days);
}
});
}
function downloadgoogleeod(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,statuscode, urlStockinfo, arrYahooSymbols, yahoo_min, n,days)
{
var fileName = downloadpath+"temp_yahoo/googleeod"+arrYahooSymbols[n].split(':')[1]+".csv";
var http = require('http'),
fs = require('fs'),
request = require('request');
if(statuscode==200){
var out = fs.createWriteStream(fileName);
var req = request(
{
method: 'GET',
uri: urlStockinfo,
headers:
{
"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
"Accept-Encoding": "text/html",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Cookie": "cookie"
}
}
);
req.pipe(out);
req.on('error',function(err){
console.log('\n err'+err.stack);
});
req.on('end', function()
{
Ext.getCmp('pbar3').updateText(fileName.split("/")[fileName.split("/").length-1]+' downloaded...');
document.getElementById("om").innerHTML+="<br/>"+fileName.split('/')[fileName.split('/').length-1]+" downloaded...";
n++;
if(arrYahooSymbols.length > n)
{
loadprogressbar();
setTimeout(function(){
downloadAndProcessGoogleEOD(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,arrYahooSymbols, yahoo_min, n,days);
},100);
}else
{
loadprogressbar();
setTimeout(function(){
processGoogleEODFiles(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,arrYahooSymbols, yahoo_min, fileName, 0);
},1000);
}
});
}
else
{
console.log('\n ERROR while downloading '+arrYahooSymbols[n]);
}
}
function processGoogleEODFiles(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,arrYahooSymbols, yahoo_min, fileName, n)
{
var fileName1 = downloadpath+"temp_yahoo/googleeod"+arrYahooSymbols[n].split(':')[1]+".csv";
var fs = require('fs');
var csv = require('ya-csv');
var async = require('async');
async.parallel({
data1 : function(callback){
if (fs.existsSync(fileName1)) {
fs.readFile(fileName1,'utf-8', function(err, data){
callback(err, data);
});
}else
{
fs.readdir(downloadpath, function(err, files){
callback(err,'0');
});
}
}
}, function(err, result){
if(err) throw err;
if (fs.existsSync(fileName1)) {
var arrRow = new Array();
var strSymbol = arrYahooSymbols[n].split(":")[1];
arrRow = result['data1'].split('\n');
var arrFileData = new Array();
if(arrRow.length > 7)
{
for(var i=7 ; i<arrRow.length-1;i++)
{
var tempArr = arrRow[i].split(',');
arrFileData.push(tempArr);
}
var filename = "googleeod"+strSymbol+".csv";
var header='TICKER,NAMES,DATE,OPEN,HIGH,LOW,CLOSE,VOLUME,OPENINT';
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
today = mm+'/'+dd+'/'+yyyy;
if(!fs.existsSync(outputpath+filename)){
fs.writeFile(outputpath+filename, header+'\n', function (err)
{
if (err) throw err;
var timestamp
for(var i=0 ; i<arrFileData.length;i++)
{
if(arrFileData[i][0].search("a")>=0)
{
timestamp=arrFileData[i][0];
timestamp=timestamp.substr(1);
timestamp=parseInt(timestamp.toString());
}
var d = new Date();
var date = new Date(timestamp*1000);
var yr=date.getFullYear();
var mnth=date.getMonth()+1;
var dat=date.getDate();
if(mnth<10)
mnth='0'+mnth;
if(dat<10)
dat='0'+dat;
fs.appendFileSync(outputpath+filename,strSymbol+','+strSymbol+','+yr+''+mnth+''+dat+','+arrFileData[i][4]+','+arrFileData[i][2]+','+arrFileData[i][3]+','+arrFileData[i][1]+','+arrFileData[i][5]+','+'0'+'\n','UTF-8', function (err) {
if (err) throw err;
});
timestamp=timestamp+(parseInt(yahoo_min)*60);
}
});
}
}
Ext.getCmp('pbar3').updateText('Processing googleeod '+strSymbol+'.csv completed');
document.getElementById("om").innerHTML+='<br/>Processing googleeod '+strSymbol+'.csv completed';
n++;
if(arrYahooSymbols.length > n)
{
loadprogressbar();
setTimeout(function(){
processGoogleEODFiles(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn,arrYahooSymbols, yahoo_min, fileName, n);
},1000);
}else
{
sn++;
callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn);
}
}
} );
}