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.

349 lines (290 loc) 15.2 kB
/***************************** * *Author:JAYANT PATIL *COMPANY NAME XNet Inc,Pune. *last modified date:05-jan-2013 * * * *Description: *This will process NSE options file and generate output file * *variables : *count --->contains array of date in which we take one date process files regarding to that *n --->it is a counter variable *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_nseoption(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn) { try { var mon=count[n].split('-')[0]; var m=count[n].split('-')[1]; var d=count[n].split('-')[2]; var yy=count[n].split('-')[3]; var y=yy.substr(2,2); //import files from modules var fs = require('fs'); var csv = require('ya-csv'); var async = require('async'); //var path = require('./node_modules/path'); //var writeFileStream = fs.createWriteStream('./tempData/output.txt',{flag:'a',encoding:'utf-8'}); async.parallel({ data1 : function(callback){ if (fs.existsSync(unzippath+'unzip1/op'+d+m+yy+'.csv')) { Ext.getCmp('pbar3').updateText('Processing op'+d+m+yy+'.csv'); fs.readFile(unzippath+'unzip1/op'+d+m+yy+'.csv','utf-8', function(err, data){ if(err) { document.getElementById("om").innerHTML+="<br/>An error occured while reading "+unzippath+"unzip1/op"+d+m+yy+".csv"; } //console.log('\n Reading fo.csv'); 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 "+unzippath+"unzip1/op"+d+m+yy+".csv"; } callback(err,'0'); }); } }, data2 : function(callback){ if (fs.existsSync(downloadpath+'sec_list.csv')) { fs.readFile(downloadpath+'sec_list.csv','utf-8', function(err, data){ if(err) { document.getElementById("om").innerHTML+="<br/>An error occured while reading "+downloadpath+"sec_list.csv."; } 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 "+downloadpath+"sec_list.csv."; } callback(err,'0'); }); } } }, function(err, result){ if(err) { document.getElementById("om").innerHTML+="<br/>An error occured while processing "+unzippath+"unzip1/op"+d+m+yy+".csv"; } if (fs.existsSync(unzippath+'unzip1/op'+d+m+yy+'.csv')) { Ext.getCmp('pbar3').updateText('Processing op'+d+m+yy+'.csv'); 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-3;i++) { var tempArr = arrRow[i].split(','); arrFileData.push(tempArr); } // Read Sec_List if (fs.existsSync(unzippath+'unzip1/op'+d+m+yy+'.csv')) { var arrRow1 = new Array(); arrRow1 = result['data2'].split('\n'); var arrFileData1 = new Array(); for(i=0 ; i<arrRow1.length-1 ;i++) { var tempArr1 = arrRow1[i].split(','); arrFileData1.push(tempArr1); } for(i=1; i<arrFileData.length; i++) { for(var j=1; j<arrFileData1.length; j++) { if(arrFileData[i][1].toString().trim()==arrFileData1[j][0].toString().trim()) { arrFileData[i][15] = arrFileData1[j][2].toString().trim(); // addding name to file at column 15 } } } } // add blank space where cell of company name col not have value for(i=0; i<arrFileData.length; i++) { if(arrFileData[i][15] == null || arrFileData[i][15] == "") { arrFileData[i][15] = ''; } } var filename = outputpath+'NSE_Equity_Options_op'+d+m+yy+'.csv'; var header='TICKER,NAME,DATE,OPEN,HIGH,LOW,CLOSE,VOLUME,OPENINT'; if(!fs.existsSync(filename)){ fs.writeFile(filename, header+'\n', function (err) { if(err) { document.getElementById("om").innerHTML+="<br/>An error occured while processing "+unzippath+"unzip1/op"+d+m+yy+".csv"; } var dates = new Array(); for(i=0;i<arrFileData.length;i++) { if(typeof arrFileData[i][2]!=="undefined" && arrFileData[i][2]!=="" && arrFileData[i][2]!==null && arrFileData[i][2]!= "\"N/A\"" && arrFileData[i][2]!= "N/A" && arrFileData[i][2]!= "NaN" && arrFileData[i][2]!= "undefined") dates.push(new Date(arrFileData[i][2].split('/')[2],arrFileData[i][2].split('/')[1]-1,arrFileData[i][2].split('/')[0])); } function sortDates(a, b) { return a.getTime() - b.getTime(); } var sorted = dates.sort(sortDates); var m1=sorted[1].getMonth()+1; var day=sorted[1].getFullYear()+'/'+m1+'/'+sorted[1].getDate(); var date = yy+m+d; // console.log(date); // Writing data to output file for(i=1;i<arrFileData.length;i++) { var date1 = new Date(day); var date2 = new Date(arrFileData[i][2].split('/')[2]+'/'+arrFileData[i][2].split('/')[1]+'/'+arrFileData[i][2].split('/')[0]); m1=monthDiff(date1,date2); if(m1==0) { //if(arrFileData[i][1]!=''||arrFileData[i][1]!=null) { fs.appendFileSync(filename,arrFileData[i][1].trim()+'-I'+parseInt(arrFileData[i][3])+arrFileData[i][4].toString().trim()+','+arrFileData[i][15]+','+date+','+arrFileData[i][5]+','+arrFileData[i][6]+','+arrFileData[i][7]+','+arrFileData[i][8]+','+arrFileData[i][10].toString().trim()+','+arrFileData[i][9]+'\n','UTF-8', function (err) { if(err) { document.getElementById("om").innerHTML+="<br/>An error occured while processing "+unzippath+"unzip1/op"+d+m+yy+".csv"; } //console.log(parseInt(arrFileData[i][3])+arrFileData[i][4]+'\n'); }); } } if(m1==1) { //if(arrFileData[i][1]!=''||arrFileData[i][1]!=null) { fs.appendFileSync(filename,arrFileData[i][1].trim()+'-II'+parseInt(arrFileData[i][3])+arrFileData[i][4].toString().trim()+','+arrFileData[i][15]+','+date+','+arrFileData[i][5]+','+arrFileData[i][6]+','+arrFileData[i][7]+','+arrFileData[i][8]+','+arrFileData[i][10].toString().trim()+','+arrFileData[i][9]+'\n','UTF-8', function (err) { if(err) { document.getElementById("om").innerHTML+="<br/>An error occured while processing "+unzippath+"unzip1/op"+d+m+yy+".csv"; } ////console.log(parseInt(arrFileData[i][3])+arrFileData[i][4]+'\n'); }); } } if(m1==2) { //if(arrFileData[i][1]!=''||arrFileData[i][1]!=null) { fs.appendFileSync(filename,arrFileData[i][1].trim()+'-III'+parseInt(arrFileData[i][3])+arrFileData[i][4].toString().trim()+','+arrFileData[i][15]+','+date+','+arrFileData[i][5]+','+arrFileData[i][6]+','+arrFileData[i][7]+','+arrFileData[i][8]+','+arrFileData[i][10].toString().trim()+','+arrFileData[i][9]+'\n','UTF-8', function (err) { if(err) { document.getElementById("om").innerHTML+="<br/>An error occured while processing "+unzippath+"unzip1/op"+d+m+yy+".csv"; } //console.log(parseInt(arrFileData[i][3])+arrFileData[i][4]+'\n'); }); } } if(m1==3) { //if(arrFileData[i][1]!=''||arrFileData[i][1]!=null) { fs.appendFileSync(filename,arrFileData[i][1].trim()+'-IV'+parseInt(arrFileData[i][3])+arrFileData[i][4].toString().trim()+','+arrFileData[i][15]+','+date+','+arrFileData[i][5]+','+arrFileData[i][6]+','+arrFileData[i][7]+','+arrFileData[i][8]+','+arrFileData[i][10].toString().trim()+','+arrFileData[i][9]+'\n','UTF-8', function (err) { if(err) { document.getElementById("om").innerHTML+="<br/>An error occured while processing "+unzippath+"unzip1/op"+d+m+yy+".csv"; } //console.log(parseInt(arrFileData[i][3])+arrFileData[i][4]+'\n'); }); } } }// end of for loop }); } } }); //Ext.getCmp('pbar3').updateText('Processing op'+d+m+yy+'.csv completed'); document.getElementById("om").innerHTML+="<br/>op"+m+d+yy+".csv is processed.."; addlog(logpath, 'op'+d+mon+yy+'.csv is processed..'); n++; if(count.length>n) { loadprogressbar(); setTimeout(function (){ process_nseoption(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod, sn) }, 3000); document.getElementById("om").innerHTML+='..'; } else { loadprogressbar(); /************/ visit('nseo.html'); /***************/ sn++; callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } } catch(err){ document.getElementById("om").innerHTML+="<br/>An error occured while reading "+unzippath+"unzip1/op"+d+m+yy+".csv"; n++; if(count.length>n) { loadprogressbar(); setTimeout(function (){ process_nseoption(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod, sn) }, 3000); document.getElementById("om").innerHTML+='..'; } else { loadprogressbar(); /************/ visit('nseo.html'); /***************/ sn++; callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } } } function monthDiff(d1, d2) { try { var months; months = (d2.getFullYear() - d1.getFullYear()) * 12; months -= d1.getMonth() + 1; months += d2.getMonth(); return months+1; }catch(err) { console.log(err); } } // return numeric month for symbolic month arg