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.

402 lines (326 loc) 16.9 kB
/*********************** Company Name:XNet Inc. Last Modified Date:12-Jan-2013 Author: Prashant Shilimkar Description :This page will download mcx bhavcopy csv file from site http://www.mcxindia.com/SitePages/BhavCopyDateWise.aspx given date. *************************/ /******************************** <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 mcx_bhavcopy(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn) { try { var mon=count[n].split('-')[0]; var m1=count[n].split('-')[1]; var d=count[n].split('-')[2]; var yy=count[n].split('-')[3]; var y=yy.substr(2,2); var intMonth = getMonth(mon); var currentDateForDownloadMCX = m1+'/'+d+'/'+yy; //console.log('\n n = '+n+' date : '+currentDateForDownloadMCX); //document.getElementById("om").innerHTML+="<br/>mcxbhavcopy"+mon+d+yy+" is downloading.."; var currdate1=new Date(); var year1=currdate1.getFullYear(); if(year1==yy){ try { document.getElementById("om").innerHTML+="<br/>mcxbhavcopy"+mon+d+yy+".csv is downloading.."; fs = require('fs'); var csv = require('ya-csv'); var async = require('async'); var htmlparser = require("htmlparser"); var DomHandler = require("DomHandler"); var DomElementType = require('DomElementType'); var http = require('http'), fs = require('fs'), request = require('request'); var urlStockinfo = "http://www.mcxindia.com/SitePages/BhavCopyDateWise.aspx"; // url to which request have to post var r = request.get(urlStockinfo); // This request is for to get __VIEWSTATE and __EVENTVALIDATION values //var fileNameCsv = "mcxbhavcopy.csv"; var resData = ""; var resData1 = ""; //r.end(); r.on('response', function(response) { response.on('data', function(chunk) { resData += chunk; }); }); r.on('end', function () { var viewState_value = ""; var eventValidation_value = ""; var mImgBtnGo_X = ""; try{ // Parsing html for getting value of __VIEWSTATE var parser = new htmlparser.Parser( { onopentag: function(name, attribs){ if(name === "input" && attribs["id"] === "__VIEWSTATE"){ viewState_value = attribs["value"]; } }, ontext:function(text){ }, onclosetag: function(tagname){ } }); parser.write(resData); parser.done(); // Parsing html for getting value of __EVENTVALIDATION var parser1 = new htmlparser.Parser( { onopentag: function(name, attribs){ if(name === "input" && attribs["id"] === "__EVENTVALIDATION"){ eventValidation_value = attribs["value"]; } }, ontext:function(text){ }, onclosetag: function(tagname){ } }); parser1.write(resData); parser1.done(); new htmlparser.FeedHandler(function(error, feed){ }); }catch(err) { if(err){ document.getElementById("om").innerHTML+="<br/>"+err; } } var r1 = request // This request to load data of given date and set x and y co-ordinates to mImgBtnGo using __VIEWSTATE and __EVENTVALIDATION values ( { 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://www.mcxindia.com", "Accept-Encoding": "utf-8", "Accept": "text/plain,application/csv,*/*;q=0.3", "Cookie": "cookie" }, form:{ '__VIEWSTATE':viewState_value.trim(), '__EVENTVALIDATION':eventValidation_value.trim(), 'mTbdate':currentDateForDownloadMCX, 'mImgBtnGo.x':'13', 'mImgBtnGo.y':'6' } //,'__EVENTTARGET':'btnLink_Excel'}//,'__EVENTARGUMENT':'Page$2'} } ); r1.on('response', function(response) { response.on('data', function(chunk) { resData1 += chunk; }); }); var viewState_value1 = ""; var eventValidation_value1 = ""; r1.on('end', function() { try{ // Parsing html for getting value of __VIEWSTATE var parser2 = new htmlparser.Parser( { onopentag: function(name, attribs){ if(name === "input" && attribs["id"] === "__VIEWSTATE"){ viewState_value1 = attribs["value"]; } }, ontext:function(text){ }, onclosetag: function(tagname){ } }); parser2.write(resData1); parser2.done(); // Parsing html for getting value of __EVENTVALIDATION var parser3 = new htmlparser.Parser( { onopentag: function(name, attribs) { if(name === "input" && attribs["id"] === "__EVENTVALIDATION"){ eventValidation_value1 = attribs["value"]; } }, ontext:function(text){ }, onclosetag: function(tagname){ } }); parser3.write(resData1); parser3.done(); new htmlparser.FeedHandler(function(error, feed){ }); }catch(err) { if(err){ document.getElementById("om").innerHTML+="<br/>"+err; } } // This request is for to download loaded data (ie csv) by above request using __VIEWSTATE and __EVENTVALIDATION values /********cheking for status code here****/ 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": "utf-8", "Accept": "text/plain,application/csv,*/*;q=0.3", "Cookie": "cookie" }, form:{ '__VIEWSTATE':viewState_value1.trim(), '__EVENTVALIDATION':eventValidation_value1.trim(), 'mTbdate':currentDateForDownloadMCX, '__EVENTTARGET':'btnLink_Excel' } }, function(error, response, body) { if(response != null) { if(response.statusCode != null || response.statusCode != "") { if (!error && response.statusCode == 200) { downloadmcxcsv(response.statusCode); } else { downloadmcxcsv(404); } }else { downloadmcxcsv(404); } }else { downloadmcxcsv(404); } } ); /**************************end of checking status code****/ function downloadmcxcsv(code){ if(code==200){ document.getElementById("om").innerHTML+="<br/>mcxbhavcopy"+mon+d+yy+".csv is downloading.."; var outCsv = fs.createWriteStream(downloadpath+"mcxbhavcopy_"+mon+d+yy+".csv"); var r2 = 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": "utf-8", "Accept": "text/plain,application/csv,*/*;q=0.3", "Cookie": "cookie" }, form:{ '__VIEWSTATE':viewState_value1.trim(), '__EVENTVALIDATION':eventValidation_value1.trim(), 'mTbdate':currentDateForDownloadMCX, '__EVENTTARGET':'btnLink_Excel' } } ); r2.pipe(outCsv); r2.on("end", function(){ document.getElementById("om").innerHTML+="<br/>mcxbhavcopy"+mon+d+yy+".csv is downloaded"; n++; if(count.length > n) { loadprogressbar(); setTimeout(function(){ mcx_bhavcopy(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); },1000); //mcx_bhavcopy(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); }else { loadprogressbar(); setTimeout(function(){ process_mcxeodc(count,0,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); },2000); } }); r2.on("error", function(){ document.getElementById("om").innerHTML+="<br/>Problem in downloading mcxbhavcopy"+mon+d+yy+".csv"; mcx_bhavcopy(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); }); } else { document.getElementById("om").innerHTML+="<br/><font color=red>mcxbhavcopy"+mon+d+yy+".csv file not found on server</font>"; n++; if(count.length > n) { loadprogressbar(); setTimeout(function(){ mcx_bhavcopy(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); },1000); }else { loadprogressbar(); setTimeout(function(){ process_mcxeodc(count,0,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); },2000); } } } }); }); }catch(err) { if(err){ document.getElementById("om").innerHTML+="<br/>"+err; } } } else { n++; if(count.length > n) { loadprogressbar(); setTimeout(function(){ mcx_bhavcopy(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); },1000); }else { loadprogressbar(); setTimeout(function(){ process_mcxeodc(count,0,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); },2000); } } }catch(err) { console.log(err); n++; if(count.length > n) { loadprogressbar(); setTimeout(function(){ mcx_bhavcopy(count,n,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); },1000); }else { loadprogressbar(); setTimeout(function(){ process_mcxeodc(count,0,baseurl,downloadpath,unzippath,outputpath,logpath,fromd,tod,sn); },2000); } } }