diff options
Diffstat (limited to 'modules/quotesgrab')
-rw-r--r-- | modules/quotesgrab/quotesgrab.js | 65 |
1 files changed, 65 insertions, 0 deletions
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("<tbody>",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<keylist.length;i++){ + idx=html.indexOf("<td",idx); if(idx==-1)break; idx+=3; + idx=html.indexOf(">",idx); if(idx==-1)break; idx+=1; + idx2=html.indexOf("</td>",idx); if(idx2==-1)break; + obj[keylist[i]]= + html.slice(idx,idx2) + .replace(/<\/?div[^>]*>/g,"") + .replace(/<br>/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)); + }); + }); +}; |