UNPKG

shubhadownloader

Version:

There is large amount of information available in market place. The markets are always in sync. In today's world investors find it very difficult to make effective use of information available to them. Shubha Downloader is great tool which resolves this problem and helps investor to increase his productivity and stay focused on decision making. Shubha Downloader enable end user to download market data from available sources and organize it. Shubha Downloader is Open source & FREE utility for end users. Shubha Downloader have main features as follows End of the day market data from web to your favorite charting application . Fundamental market data from web to your favorite charting application. Market reports from web to your favorite charting application.

394 lines (338 loc) 13.6 kB
/***************************** * *Author:PRASHANT SHILIMKAR *COMPANY NAME XNet Inc,Pune. *last modified date:08-jan-2013 * * * *Description: * * *variables : *baseurl-->it contians all url for nse equity *downloadpath---> it contain download path of file for processing *unzippath ----> it contains unzip folder path *outputpah ----> it containds output older path * * *copyright 2013 ******************************/ /******************************** <Shubha Downloader, It downloads end of day pricing data from publicly available sites and converts it different formats....> Copyright (C) 2013 Shubhalabha.in This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/> ***********************************/ 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;//document.getElementById("datefield-1029-inputEl").value; var dateto=tod;//document.getElementById("datefield-1031-inputEl").value; 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']; //console.log('\nStartDate= '+stDateFormate); //console.log('\nendDate= '+endDateFormate); 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"; //console.log('\npath= '+fileName); //console.log('\nurl= '+urlStockinfo); 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) { //Ext.getCmp('pbar3').updateText('Downloading '+fname); 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); // For saving NSE Equity bhavcopy 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); //request pipeout 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; } //console.log('\n Reading '+fname); callback(err, data); }); }else { // Here readdir's functionaliity has no use. It is written for only to send // '0' data in its callback function because we cannot call "callback(err, data);" // function out side a callback function 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(); // arrRow.lenght-3 is for last 3 lines of no use in file 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++) { //console.log('\ni='+i+' > '+arrFileData[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); } }); // parallel }); //on end } 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); } }