Last active
August 29, 2015 14:23
-
-
Save wgbartley/b9f1f016b0e223699da0 to your computer and use it in GitHub Desktop.
Discourse Forum User Post Alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This monitors a Discourse forum for postings by a particular user. | |
* I stopped just shy of figuring out how to publish an event to a | |
* Particle device PubSub stream. | |
**/ | |
var https = require('https'), | |
cp = require('child_process'), | |
xmlParseString = require('xml2js').parseString; | |
getLatest(); | |
function getLatest() { | |
https.get('https://community.particle.io/latest.rss', function(res) { | |
var body = ''; | |
res.on('data', function(d) { | |
body += d.toString(); | |
}); | |
res.on('end', function() { | |
xmlParseString(body, function(err, result) { | |
var items = result.rss.channel[0].item; | |
for(var i in items) { | |
getTopic(items[i].source[0].$.url); | |
} | |
}); | |
}); | |
}).on('error', function(e) { | |
console.log('ERROR: ', e); | |
setTimeout(getLatest, 60000); | |
}); | |
} | |
function getTopic(url) { | |
url = url.replace('spark.io', 'particle.io'); | |
https.get(url, function(res) { | |
var body = ''; | |
res.on('data', function(d) { | |
body += d.toString(); | |
}); | |
res.on('end', function() { | |
xmlParseString(body, function(err, result) { | |
var items = result.rss.channel[0].item; | |
for(var i in items) { | |
var author = /\(\@(\w)+/.exec(items[i].author)[0].substr(1); | |
if(author.toUpperCase()=='@JACK') { | |
console.log(items[i].pubDate); | |
cp.execFileSync('particle', ['publish', 'jackattack', '"'+items[i].pubDate+'"']); | |
} | |
} | |
}); | |
setTimeout(getLatest, 60000); | |
}); | |
}).on('error', function(e) { | |
console.log('ERROR: ', e); | |
setTimeout(getLatest, 60000); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment