From f00ba92ed2cc1a9c24ad783e83525d1b5a85b857 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 13 Sep 2016 11:43:06 +0200 Subject: Initial --- modules/quotesgrab/quotesgrab.js | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 modules/quotesgrab/quotesgrab.js (limited to 'modules/quotesgrab/quotesgrab.js') diff --git a/modules/quotesgrab/quotesgrab.js b/modules/quotesgrab/quotesgrab.js new file mode 100644 index 0000000..6b3b068 --- /dev/null +++ b/modules/quotesgrab/quotesgrab.js @@ -0,0 +1,65 @@ +var cmn=require("../$common.js"), + https=require("https"); + +function parsequoteshtml(html){ + var idx=html.indexOf("row-header-wrapper"); if(idx==-1)return []; idx+=18; + var endidx=html.indexOf("",idx); + var idx2; + var list=[],obj; + var keylist=["timestamp","name","quote"],i; + while(true){ + idx=html.indexOf("row-header-wrapper",idx); if(idx==-1)break; idx+=18; + if(idx>endidx)break; + obj={}; + for(i=0;i",idx); if(idx==-1)break; idx+=1; + idx2=html.indexOf("",idx); if(idx2==-1)break; + obj[keylist[i]]= + html.slice(idx,idx2) + .replace(/<\/?div[^>]*>/g,"") + .replace(/
/g,"\n") + .replace(/"/g,'"') + .replace(/&/g,"&") + .replace(/&#x([0-9a-f][0-9a-f]);/gi,function(match,p1){ + return String.fromCharCode(parseInt(p1,16)); + }) + .replace(/&#([0-9]{1,3});/g,function(match,p1){ + return String.fromCharCode(parseInt(p1,10)); + }); + idx=idx2+5; + } + if(obj.name=="x"||obj.name=="X")continue; + obj.timestamp=new Date(obj.timestamp); + list.push(obj); + } + return list; +} + +function getquoteslist(cb){ + https.get("https://docs.google.com/spreadsheets/d/1ywrThdscubPOC-gHh_qnFGfuPrtYxTap6UsJBDnt88c/htmlview",function(res){ + var body=""; + res.on("data",function(data){ + body+=data.toString(); + }); + res.on("end",function(){ + cb(parsequoteshtml(body)); + }); + res.on("error",function(err){ + console.log("Error in quotes res:",err); + cb(""); + }); + }).on("error",function(err){ + console.log("Error in quotes https:",err); + cb(""); + }); +} + +module.exports=function(app,io,moddir){ + app.get("/quotes",function(req,res){ + res.set("Content-Type", "text/json"); + getquoteslist(function(obj){ + res.end(JSON.stringify(obj)); + }); + }); +}; -- cgit v1.2.3-54-g00ecf