function process_bse_indexdata(baseurl,downloadpath,unzippath,outputpath,frmd,tod,logpath,count,sn)
{
try
{
Ext.getCmp('pbar3').updateText('BSE Block Deal Processing is started . . .');
var url = require('url');
var async = require('async');
var http = require('http'),fs = require('fs'),AdmZip = require('adm-zip'),request = require('request');
var datefrom=frmd;
var dateto=tod;
var fromdate=datefrom.split('/');
var todate=dateto.split('/');
var beginDate = new Date(fromdate[0]+','+fromdate[1]+','+fromdate[2]);
var endDate1 = new Date(todate[0]+','+todate[1]+','+todate[2]);
var arrCurrentDate = beginDate.toDateString().split(" ");
var arrendCurrentDate = endDate1.toDateString().split(" ");
var strMonthUpper = arrCurrentDate[1].toUpperCase();
var intMonth = beginDate.getMonth()+1;
var intendMonth = endDate1.getMonth()+1;
var stDateFormate = intMonth+"/"+arrCurrentDate[2]+"/"+arrCurrentDate[3];
var endDateFormate = intendMonth+"/"+arrendCurrentDate[2]+"/"+arrendCurrentDate[3];
var arrInd = ['BSE30','MIDCAP','SMLCAP','BSE100','BSE200','BSE500','AUTO','BANKEX','BSECD','BSECG'
,'BSEFMCG','BSEHC','BSEIT','METAL','OILGAS','BSEPOWER','BSEPSU','REALITY','TECK','DOL30'
,'DOL100','DOL200','SHA50','GREENX','BSEIPO','CARBON','SMEIPO'];
var tempPath = downloadpath+'temp/';
downloadAndProcessBSEIndexData(arrInd, 0, stDateFormate, endDateFormate, tempPath, outputpath);
function downloadAndProcessBSEIndexData(ind, n, stDateFormate, endDateFormate, tempPath, outputpath)
{
var urlStockinfo = "http://www.bseindia.com/stockinfo/indices_main_excel.aspx?ind="+ind[n]+"&fromDate="+stDateFormate+"&toDate="+endDateFormate+"&DMY=D";
var fileName = tempPath+"temp_bseindex_"+ind[n]+".csv";
var fname = "temp_bseindex_"+ind[n]+".csv";
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",
"Referer": "http://www.bseindia.com/stockinfo/indices_main_excel.aspx",
"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)
{
downloadIndexData(200, urlStockinfo, fileName, fname, ind ,n, stDateFormate, endDateFormate, tempPath, outputpath);
}
else
{
downloadIndexData(404, urlStockinfo, fileName, fname, ind, n, stDateFormate, endDateFormate, tempPath, outputpath);
}
}else
{
downloadIndexData(404, urlStockinfo, fileName, fname, ind, n, stDateFormate, endDateFormate, tempPath, outputpath);
}
}else
{
downloadIndexData(404, urlStockinfo, fileName, fname, ind, n, stDateFormate, endDateFormate, tempPath, outputpath);
}
}
);
}
function downloadIndexData(statuscode, urlStockinfo, fileName, fname, ind, n, stDateFormate, endDateFormate, tempPath, outputpath)
{
if(statuscode==200)
{
Ext.getCmp('pbar3').updateText('Downloading '+fname);
Ext.getCmp('pbar3').updateProgress(0);
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",
"Referer": "http://www.nseindia.com/products/content/all_daily_reports.htm",
"Accept-Encoding": "gzip,deflate,sdch",
"encoding": "null",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*//*;q=0.8",
"Cookie": "cookie"
}
});
req.on('error',function()
{
Ext.getCmp('pbar3').updateText('Error occurred while downloading '+fname+'');
document.getElementById("om").innerHTML+='<br/>Error occured in downloading '+fname.replace(/temp_/g,'');
downloadAndProcessBSEIndexData(ind, n, stDateFormate, endDateFormate, tempPath);
});
req.pipe(out);
req.on('end', function()
{
Ext.getCmp('pbar3').updateText(''+fname+' is downloaded');
document.getElementById("om").innerHTML+='<br>'+fname.replace(/temp_/g,'')+' is downloaded...'
var async = require('async');
async.parallel(
{
data1 : function(callback){
if (fs.existsSync(fileName)) {
fs.readFile(fileName,'utf-8', function(err, data){
if(err)
{
document.getElementById("om").innerHTML+="<br/>An error occured while reading "+fileName;
}
callback(err, data);
});
}else
{
fs.readdir(downloadpath, function(err, files){
if(err)
{
document.getElementById("om").innerHTML+="<br/>An error occured while reading "+fileName;
}
callback(err,'0');
});
}
}
}, function(err, result)
{
if(err)
{
document.getElementById("om").innerHTML+="<br/>An error occured while processing BSE Index file.";
}
if (fs.existsSync(fileName))
{
Ext.getCmp('pbar3').updateText('Processing '+fname+'');
var arrRow = new Array();
arrRow = result['data1'].split('\n');
var arrFileData = new Array();
for(var i=0 ; i<arrRow.length;i++)
{
var tempArr = arrRow[i].split(',');
arrFileData.push(tempArr);
}
var outputpath1=outputpath;
var output_filename = outputpath1+'BSE_Indices_bseindex.csv';
if(fs.existsSync(output_filename))
{
for(i=1 ; i<arrFileData.length-1;i++)
{
if(typeof arrFileData[i][0]!=="undefined"||arrFileData[i][0]!=""||arrFileData[i][0]!=" "||arrFileData[i][0]!=null)
fs.appendFileSync(output_filename, ind[n]+','+ind[n]+','+arrFileData[i][0].toString().split('-')[2]+''+getMonth(arrFileData[i][0].toString().split('-')[1].toString())+''+getDate(arrFileData[i][0].toString().split('-')[0])+','+arrFileData[i][1]+','+arrFileData[i][2]+','+arrFileData[i][3]+','+arrFileData[i][4]+','+'0'+','+'0'+'\n','utf-8', function (err) {
if(err)
{
document.getElementById("om").innerHTML+="<br/>An error occured while processing BSE Index file.";
}
});
}
Ext.getCmp('pbar3').updateText('Processing '+fname+' completed');
}
else
{
if(ind[n] == 'BSE30')
{
for(i=0 ; i<arrFileData.length-1;i++)
{
if(i > 0)
{
if(typeof arrFileData[i][0]!=="undefined"||arrFileData[i][0]!=""||arrFileData[i][0]!=" "||arrFileData[i][0]!=null)
fs.appendFileSync(output_filename, ind[n]+','+ind[n]+','+arrFileData[i][0].toString().split('-')[2]+''+getMonth(arrFileData[i][0].toString().split('-')[1].toString())+''+getDate(arrFileData[i][0].toString().split('-')[0])+','+arrFileData[i][1]+','+arrFileData[i][2]+','+arrFileData[i][3]+','+arrFileData[i][4]+','+'0'+','+'0'+'\n','utf-8', function (err) {
if(err)
{
document.getElementById("om").innerHTML+="<br/>An error occured while processing BSE Index file.";
}
});
}else
{
fs.appendFileSync(output_filename, 'TICKER,NAME,DATE,OPEN,HIGH,LOW,CLOSE,VOLUME,OPENINT'+'\n','utf-8',function (err) {
if(err)
{
document.getElementById("om").innerHTML+="<br/>An error occured while processing BSE Index file.";
}
});
}
}
}
else
{
for(var i=1 ; i<arrFileData.length-1;i++)
{
if(typeof arrFileData[i][0]!=="undefined"||arrFileData[i][0]!=""||arrFileData[i][0]!=" "||arrFileData[i][0]!=null)
fs.appendFileSync(output_filename, ind[n]+','+ind[n]+','+arrFileData[i][0].toString().split('-')[2]+''+getMonth(arrFileData[i][0].toString().split('-')[1].toString())+''+getDate(arrFileData[i][0].toString().split('-')[0])+','+arrFileData[i][1]+','+arrFileData[i][2]+','+arrFileData[i][3]+','+arrFileData[i][4]+','+'0'+','+'0'+'\n','utf-8', function (err) {
if(err)
{
document.getElementById("om").innerHTML+="<br/>An error occured while processing BSE Index file.";
}
});
}
Ext.getCmp('pbar3').updateText('Processing '+fname+' completed');
}
}
}
n++;
if(ind.length>n)
{
loadprogressbar();
setTimeout(function(){downloadAndProcessBSEIndexData(ind, n, stDateFormate, endDateFormate, tempPath, outputpath);},1000);
}else
{
Ext.getCmp('pbar3').updateProgress(0);
visit('indices.html');
sn++;
callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,frmd,tod,sn);
}
});
});
}
else
{
Ext.getCmp('pbar3').updateText('File '+fname.replace(/temp_/g,'')+' is not present on server');
document.getElementById("om").innerHTML+='<br/><font color=red>'+fname.replace(/temp_/g,'')+' is not present on server</font>';
n++;
if(ind.length>n)
{
loadprogressbar();
setTimeout(function(){downloadAndProcessBSEIndexData(ind, n, stDateFormate, endDateFormate, tempPath, outputpath);},1000);
}else
{
Ext.getCmp('pbar3').updateProgress(0);
visit('indices.html');
sn++;
callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,frmd,tod,sn);
}
}
function getDate(date)
{
if(parseInt(date) < 10)
{
var strDate = '0'+date;
return strDate;
}
return date;
}
function getMonth(mon){
switch(mon)
{
case 'January':
mon='01';
break;
case 'February':
mon='02';
break;
case 'March':
mon='03';
break;
case 'April':
mon='04';
break;
case 'May':
mon='5';
break;
case 'June':
mon='06';
break;
case 'July':
mon='07';
break;
case 'August':
mon='08';
break;
case 'September':
mon='09';
break;
case 'October':
mon='10';
break;
case 'November':
mon='11';
break;
case 'December':
mon='12';
break;
}
return mon;
}
}
}catch(err)
{
document.getElementById("om").innerHTML+="<br/>An error occured while processing BSE Index file.";
sn++;
callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,frmd,tod,sn);
}
}