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.

352 lines (299 loc) 13.4 kB
function downloadAndProcessYahooRT(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn) { 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'}); try{ async.parallel({ data1 : function(callback){ if (fs.existsSync('yahoo.csv')) { fs.readFile('yahoo.csv','utf-8', function(err, data){ 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('./', function(err, files){ callback(err,'0'); }); } } }, function(err, result){ if(err) throw err; if (fs.existsSync('yahoo.csv')) { var arrRow = new Array(); arrRow = result['data1'].split('\n'); if(arrRow.length>0) { var strSymbols = ""; // arrRow.lenght-3 is for last 3 lines of no use in file for(var i=0 ; i<arrRow.length-1;i++) { if(i == 0) { var tempArr = arrRow[i].split(',')[0]; strSymbols = tempArr; continue; }else { var tempArr = arrRow[i].split(',')[0]; strSymbols = strSymbols+'+'+tempArr; } } //console.log('\n str > '+strSymbols); if(strSymbols != null && strSymbols !="") { downloadYahoo(strSymbols.trim(),count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); }else { document.getElementById("om").innerHTML+="<br/><font color=lightcoral>Please add yahoo symbols to download yahoo data</font>"; sn++; callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } } else { document.getElementById("om").innerHTML+="<br/><font color=lightcoral>Please add yahoo symbols to download yahoo data</font>"; sn++; callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } } else { document.getElementById("om").innerHTML+="<br/><font color=lightcoral>Please add yahoo symbols to download yahoo data</font>"; sn++; callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } }); }catch(err){ console.error(err); } } function downloadYahoo(strSymbols,count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn) { var urlStockinfo = "http://download.finance.yahoo.com/d/quotes.csv?s="+strSymbols+"&f=snl1d1t1c1ohgv&e=.csv"; //console.log('\n check for '+urlStockinfo); var http = require('http'), fs = require('fs'), request = require('request'); // For saving NSE Equity bhavcopy 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", "Referer": "http://download.finance.yahoo.com", "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) { downloadyahooeod(response.statusCode, urlStockinfo,count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } else { downloadyahooeod(404, urlStockinfo,count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } }else { downloadyahooeod(404, urlStockinfo,count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } }else { downloadyahooeod(404, urlStockinfo,count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } }); } function downloadyahooeod(statuscode, urlStockinfo,count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn) { var fileName = downloadpath+"temp/yahoo_rt.csv"; var http = require('http'), fs = require('fs'), request = require('request'); var out = fs.createWriteStream(fileName); if(statuscode==200){ //console.log('\n ok 200.'); 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://download.finance.yahoo.com", "Accept-Encoding": "text/html", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Cookie": "cookie" } } );//.pipe(out); req.pipe(out); req.on('error',function(err){ console.log('\n err'+err.stack); }); req.on('end', function() { Ext.getCmp('pbar3').updateText('Downloading yahoo_rt.csv completed'); document.getElementById("om").innerHTML+="<br/>Downloading yahoo_rt.csv completed.."; processYahooRT(fileName,count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); }); } //if 200 else { console.log('\n ERROR while downloading '); } } function processYahooRT(fileName,count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn) { var fs = require('fs'); var async = require('async'); async.parallel({ data1 : function(callback){ if (fs.existsSync(fileName)) { fs.readFile(fileName,'utf-8', function(err, data){ //console.log('\n Reading '+fileName); 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('./', function(err, files){ callback(err,'0'); }); } } }, function(err, result){ if(err) throw err; if (fs.existsSync(fileName)) { 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-1;i++) { var tempArr = arrRow[i].split(','); arrFileData.push(tempArr); } var yahoohr = "0"; var yahoomin = "0"; for(var i=0;i<baseurl.length;i++) { if(baseurl[i].toString().split(',')[0] == "yahoohr") yahoohr = baseurl[i].toString().split(',')[1].toString(); if(baseurl[i].toString().split(',')[0] == "yahoomin") yahoomin = baseurl[i].toString().split(',')[1].toString(); } var header='TICKER,NAME,DATE,TIME,OPEN,HIGH,LOW,CLOSE,VOLUME,OPENINT'; var outputFile = outputpath+'yahoort.csv'; if(fs.existsSync(outputFile)) { for(var i=-0; i<arrFileData.length; i++) { if(arrFileData[i].length != 10) { continue; } var arrYrtDate = ""; var yrtday = ""; var yrtmonth = ""; var yrtyear = ""; if(arrFileData[i][3] != "\"N/A\"" && arrFileData[i][3] != "" && arrFileData[i][3] != null && arrFileData[i][3] != "N/A" && arrFileData[i][3] != "NaN" && arrFileData[i][3] != "undefined" && typeof arrFileData[i][3] != "undefined") { arrYrtDate= arrFileData[0][3].replace("\"", "").trim().split('/'); yrtday = arrYrtDate[1]; if(parseInt(yrtday) < 10) yrtday = '0'+yrtday; yrtmonth = arrYrtDate[0]; if(parseInt(yrtmonth) < 10) yrtmonth = '0'+yrtmonth; //console.log('\n valyear >> '+arrYrtDate[2]); if(arrYrtDate[2] != "\"N/A\"" && arrYrtDate[2] != "" && arrYrtDate[2] != null && arrYrtDate[2] != "N/A" && arrYrtDate[2] != "NaN" && arrYrtDate[2] != "undefined") yrtyear = arrYrtDate[2].replace("\"", "").trim() } var yrtTime =""; var yrthr ="00"; var yrtmin ="00"; if(arrFileData[i][4] != "\"N/A\"" && arrFileData[i][4] != "" && arrFileData[i][4] != null && arrFileData[i][4] != "N/A" && arrFileData[i][4] != "NaN" && arrFileData[i][4] != "undefined") { yrtTime = arrFileData[i][4].toString().replace("\"", "").split(':'); yrthr = parseInt(yrtTime[0].toString().trim())+5; if(yrtTime[1] != "\"N/A\"" && yrtTime[1] != "" && yrtTime[1] != null && yrtTime[1] != "N/A" && yrtTime[1] != "NaN" && yrtTime[1] != "undefined") yrtmin = yrtTime[1].replace("\"", "").toString().substr(0,2); //console.log('\n valyMin>> '+yrtTime[1]); } //console.log('\nhr > '+yrthr); //console.log('\nmin > '+yrtmin); fs.appendFileSync(outputFile, arrFileData[i][0]+','+arrFileData[i][1]+','+yrtyear+''+yrtmonth+''+yrtday+','+yrthr+':'+yrtmin+':00'+','+arrFileData[i][2]+','+arrFileData[i][2]+','+arrFileData[i][2]+','+arrFileData[i][2]+','+arrFileData[i][9].toString().trim()+','+'0'+'\n','utf-8', function(err){ if(err) throw err; }); } }else { fs.writeFile(outputFile, header+'\n',function(){ for(var i=0; i<arrFileData.length; i++) { if(arrFileData[i].length != 10) { continue; } var arrYrtDate = ""; var yrtday = ""; var yrtmonth = ""; var yrtyear = ""; if(arrFileData[i][3] != "\"N/A\"" && arrFileData[i][3] != "" && arrFileData[i][3] != null && arrFileData[i][3] != "N/A" && arrFileData[i][3] != "NaN" && arrFileData[i][3] != "undefined" && typeof arrFileData[i][3] != "undefined") { arrYrtDate= arrFileData[0][3].replace("\"", "").trim().split('/'); yrtday = arrYrtDate[1]; if(parseInt(yrtday) < 10) yrtday = '0'+yrtday; yrtmonth = arrYrtDate[0]; if(parseInt(yrtmonth) < 10) yrtmonth = '0'+yrtmonth; //console.log('\n valyear >> '+arrYrtDate[2]); if(arrYrtDate[2] != "\"N/A\"" && arrYrtDate[2] != "" && arrYrtDate[2] != null && arrYrtDate[2] != "N/A" && arrYrtDate[2] != "NaN" && arrYrtDate[2] != "undefined") yrtyear = arrYrtDate[2].replace("\"", "").trim() } var yrtTime =""; var yrthr ="00"; var yrtmin ="00"; if(arrFileData[i][4] != "\"N/A\"" && arrFileData[i][4] != "" && arrFileData[i][4] != null && arrFileData[i][4] != "N/A" && arrFileData[i][4] != "NaN" && arrFileData[i][4] != "undefined") { yrtTime = arrFileData[i][4].toString().replace("\"", "").split(':'); yrthr = parseInt(yrtTime[0].toString().trim())+5; if(yrtTime[1] != "\"N/A\"" && yrtTime[1] != "" && yrtTime[1] != null && yrtTime[1] != "N/A" && yrtTime[1] != "NaN" && yrtTime[1] != "undefined") yrtmin = yrtTime[1].replace("\"", "").toString().substr(0,2); //console.log('\n valyMin>> '+yrtTime[1]); } //console.log('\nhr > '+yrthr); //console.log('\nmin > '+yrtmin); fs.appendFileSync(outputFile, arrFileData[i][0]+','+arrFileData[i][1]+','+yrtyear+''+yrtmonth+''+yrtday+','+yrthr+':'+yrtmin+':00'+','+arrFileData[i][2]+','+arrFileData[i][2]+','+arrFileData[i][2]+','+arrFileData[i][2]+','+arrFileData[i][9].toString().trim()+','+'0'+'\n','utf-8', function(err){ if(err) throw err; }); } }); } Ext.getCmp('pbar3').updateText('Processing yahoo_rt.csv completed'); document.getElementById("om").innerHTML+="<br/>Processing yahoo_rt.csv completed.."; //console.log('finished1'); sn++; callfun(count,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); } }); }