aggiunta un sacco di roba

This commit is contained in:
Enrico Fasoli 2015-04-23 16:51:28 +02:00
parent 03295319ea
commit 4441ab8735
5 changed files with 136 additions and 2 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
node_modules
.DS_Store*
.hubot_history
circolari/
run.sh

View File

@ -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" "$@"

View File

@ -5,6 +5,7 @@
"author": "Enrico Fasoli <fazius2009@gmail.com>",
"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"

51
scripts/agenda.coffee Normal file
View File

@ -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

77
scripts/circolari.coffee Normal file
View File

@ -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