Skip to content

Commit

Permalink
Catching errors on requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sadasant committed Oct 15, 2012
1 parent c3a458d commit f8cc71d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
3 changes: 2 additions & 1 deletion example_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ exports.irc = {
}

exports.github = {
private_atom : 'http://atom_http_url.com/'
host : '' // Your host, without https (myhost.com)
, path : '' // Your path (/mypage.php?lol=1)
}
32 changes: 28 additions & 4 deletions github.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
var http = require('http')
, xml = require('xml2json')
, bot
, options = {
host : ''
, path : ''
, method : 'GET'
}
, time = 1000 * 60 * 1 // 1 Minute

exports.init = function(_bot) {
bot = _bot

setInterval(function() {
http.get(bot.config.github.private_atom, gotResults)
}, 1000 * 60 * 1) // 1 Minute
options.host = bot.config.github.host
options.path = bot.config.github.path

makeRequests()
}

function makeRequests() {
var req = http.request(options, gotResults)
req.end()
req.on('error', gotError)
}

function gotResults(res) {
Expand All @@ -16,6 +29,17 @@ function gotResults(res) {
data += chunk
})
res.on('end', function() {
bot.controller.githubFeed(JSON.parse(xml.toJson(data)))
try {
data = JSON.parse(xml.toJson(data))
} catch(e) {
return gotError(e)
}
bot.controller.githubFeed(data)
setTimeout(makeRequests, time)
})
}

function gotError(error) {
console.log(error)
setTimeout(makeRequests, time)
}
28 changes: 24 additions & 4 deletions twitter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
var http = require('http')
, bot
, options = {
host : 'search.twitter.com'
, path : '/search.json?q=OpenVE&rpp=5&result_type=recent'
, method : 'GET'
}
, time = 1000 * 30 // 30 Seconds

exports.init = function(_bot) {
bot = _bot
makeRequests()
}

setInterval(function() {
http.get('http://search.twitter.com/search.json?q=OpenVE&rpp=5&result_type=recent', gotResults)
}, 1000 * 60 * 0.5) // 30 Seconds
function makeRequests() {
var req = http.request(options, gotResults)
req.end()
req.on('error', gotError)
}

function gotResults(res) {
Expand All @@ -15,6 +24,17 @@ function gotResults(res) {
data += chunk
})
res.on('end', function() {
bot.controller.twitterSearch(JSON.parse(data))
try {
data = JSON.parse(data)
} catch(e) {
return gotError(e)
}
bot.controller.twitterSearch(data)
setTimeout(makeRequests, time)
})
}

function gotError(error) {
console.log(error)
setTimeout(makeRequests, time)
}

0 comments on commit f8cc71d

Please sign in to comment.