From 4441ab873521bea198cc8a2036bca6dea5611373 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Thu, 23 Apr 2015 16:51:28 +0200 Subject: [PATCH] aggiunta un sacco di roba --- .gitignore | 2 ++ bin/hubot | 2 +- package.json | 6 +++- scripts/agenda.coffee | 51 ++++++++++++++++++++++++++ scripts/circolari.coffee | 77 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 scripts/agenda.coffee create mode 100644 scripts/circolari.coffee diff --git a/.gitignore b/.gitignore index e9049bb..030048b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules .DS_Store* .hubot_history +circolari/ +run.sh diff --git a/bin/hubot b/bin/hubot index 8502584..5ba73d1 100755 --- a/bin/hubot +++ b/bin/hubot @@ -5,4 +5,4 @@ set -e npm install export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH" -exec node_modules/.bin/hubot --name "asjon" "$@" +DEBUG=nightmare PYTHON=python2 PORT=9000 BIND_ADDRESS=localhost node_modules/.bin/hubot --name "asjon" "$@" diff --git a/package.json b/package.json index e86c2ae..111b956 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "author": "Enrico Fasoli ", "description": "Il miglior amico della 5IA", "dependencies": { + "cheerio": "^0.19.0", "hubot": "^2.12.0", "hubot-bitcoin": "^1.0.3", "hubot-diagnostics": "0.0.1", @@ -18,7 +19,10 @@ "hubot-rules": "^0.1.0", "hubot-scripts": "^2.5.16", "hubot-shipit": "^0.2.0", - "hubot-youtube": "^0.1.2" + "hubot-youtube": "^0.1.2", + "moment": "^2.10.2", + "nightmare": "^1.8.0", + "weak": "^0.4.0" }, "engines": { "node": "0.10.x" diff --git a/scripts/agenda.coffee b/scripts/agenda.coffee new file mode 100644 index 0000000..7cba25c --- /dev/null +++ b/scripts/agenda.coffee @@ -0,0 +1,51 @@ +Nightmare = require 'nightmare' +cheerio = require 'cheerio' +moment = require 'moment' + +downloadAgenda = (day, cb) -> + cbCalled = no + htmlData = "" + loadHtml = -> document.body.innerHTML + saveHtml = (data) -> htmlData = data + dayHasEvents = (b) -> + unless b + cbCalled = yes + cb [] + console.log Nightmare + ### + .exists('a[href="?d='+day+'"]',dayHasEvents) + .click('a[href="/agenda/"]').wait() + .click('a[href="?d='+day+'"]').wait() + ### + n = new Nightmare() + .goto('https://galilei-cr-sito.registroelettronico.com/login/') + .type('#username',process.env.REGISTRO_USERNAME) + .type('#password',process.env.REGISTRO_PASSWORD) + .click('#btnLogin').wait() + if process.env.REGISTRO_ID_STUDENTE + n.goto('https://galilei-cr-sito.registroelettronico.com/select-student/'+process.env.REGISTRO_ID_STUDENTE+'/') + n.goto('https://galilei-cr-sito.registroelettronico.com/agenda/?d='+day) + .evaluate(loadHtml, saveHtml) + n.run (err,nightmare) -> + if err then console.log err + if !cbCalled and htmlData.length > 0 + rowExtractor = -> + if $('td',this).get(1)? + $($('td',this).get(1)).text().trim() + else "" + $ = cheerio.load htmlData + tab = $('.result_table tr').map(rowExtractor).get() + tab.splice 0, 2 + cb tab + +cosaCePerIl = (day,res) -> + res.send 'aspetta che guardo l\'agenda per il '+day + downloadAgenda day, (data) -> + if data.length is 0 + res.send "non c'è niente per doma :)" + else + res.send "ecco cosa c'è per doma: "+data.join('; ') + +module.exports = (robot) -> + robot.hear "cosa c'è per domani?", (res) -> + cosaCePerIl moment().add(1, 'days').format('YYYY-MM-DD'), res diff --git a/scripts/circolari.coffee b/scripts/circolari.coffee new file mode 100644 index 0000000..f06c794 --- /dev/null +++ b/scripts/circolari.coffee @@ -0,0 +1,77 @@ +cheerio = require('cheerio') +fs = require('fs') + +parseHtml = (htmlData,done) -> + $ = cheerio.load htmlData + tab = $('tr').map (i) -> + # console.log($('td',this).html()) + # console.log($(this,'td').length) + link = ""; destinatario = "" + l = $('td',this).map (j) -> + # console.log($(this).html()) + if $('a',this).get(0)? + #console.log($('a',this).get(0)) + if $('a',this).get(0).attribs?.href? + link = 'http://galileicrema.it' + $('a',this).get(0).attribs.href + item = $(this).text().trim() + # console.log(i,j,item) + # console.log(item.length) + if(j == 5) + destinatario = item.split('\n\n\t\t\t\t\t') + # if(destinatario[0] === "Tutti") destinatario = ["ATA","Docenti","Studenti"] + return item + l = l.get() + obj = + protocollo: l[0], + mittente: l[1], + titolo: l[2], + oggetto: l[3], + data: l[4], + destinatario: destinatario, + link: link + return obj + tab = tab.get() + tab.splice 0, 1 + # fs.writeFileSync('result.json',JSON.stringify(tab)) + # console.log("saved file") + done null, tab + +downloadCircolari = (robot, callback) -> + robot.http("http://galileicrema.it/Intraitis/comunicazioni/ComVis.asp?PerChi=Tutti") + .get() (err, res, body) -> + callback err, body + +diffCircolari = (oldObj,newObj) -> + diff = newObj.length - oldObj.length + newObj.slice(0,diff) + +circolari = [] + +parseCircolari = (err,data,callback) -> + if err + console.log(err) + else + parseHtml data, (err,res) -> + #console.log("Done!") + circolari = res + fs.writeFile 'circolari/circolari.json', JSON.stringify circolari + callback circolari + +module.exports = (robot) -> + robot.respond /circolari/i, (res) -> + res.send 'controllo circolari...' + fs.exists 'circolari/circolari.json', (jsonExists) -> + if jsonExists + circolari = JSON.parse fs.readFileSync('circolari/circolari.json').toString() + res.send JSON.stringify circolari.slice 0,5 + else + fs.exists 'circolari.html', (htmlExists) -> + if htmlExists + fs.readFile 'circolari/circolari.html', (a,b) -> parseCircolari a, b, (x) -> res.send JSON.stringify x.slice 0, 5 + else + res.send "download circolari..." + downloadCircolari robot, (a,b) -> + res.send 'finito download...' + parseCircolari a, b, (x) -> + res.send 'invio...' + res.send JSON.stringify x.slice 0, 5