Skip to content

Instantly share code, notes, and snippets.

@russellbeattie
Created December 9, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russellbeattie/8cc64c1ad8bb8c230cce to your computer and use it in GitHub Desktop.
Save russellbeattie/8cc64c1ad8bb8c230cce to your computer and use it in GitHub Desktop.
Just a quick library-free script to grab a feed and parse it
var https = require('https');
var http = require('http');
var url = require('url');
var feedUrl = 'http://www.techmeme.com/feed.xml';
main();
function main(){
getFeed(feedUrl, function(feedStr){
// console.log(feedStr);
parseFeed(feedStr, function(items){
for(var i=0; i < items.length; i++){
console.log(items[i].title, items[i].guid);
}
});
});
}
function getFeed(feedUrl, fn) {
var parts = url.parse(feedUrl);
var options = {
host: parts.host,
path: parts.path,
method: 'GET'
};
var callback = function(res) {
var str = '';
var success = true;
if (res.statusCode >= 300 && res.statusCode < 400) {
getFeed(res.headers.location, fn);
success = false;
}
res.on('data', function(body) {
str += body;
});
res.on('end', function() {
if(success){
fn(str);
}
});
};
var req;
if(parts.protocol == 'https'){
req = https.request(options, callback);
} else {
req = http.request(options, callback);
}
req.on('error', function(e) {
console.error(e);
});
req.end();
}
function parseFeed(str, fn){
//This will break, hard. Don't do this.
var regex = /<item>([\s\S]*?)<\/item>/g;
var matches = [];
var result;
while ((result = regex.exec(str)) !== null) {
var itemStr = result[1];
var item = {};
item.title = /<title>(.*?)<\/title>/.exec(itemStr)[1];
item.guid = /<guid>(.+?)<\/guid>/.exec(itemStr)[1];
item.pubDate = /<pubDate>(.+?)<\/pubDate>/.exec(itemStr)[1];
//atom
// item.description = /<description><!\[CDATA\[(.*?)\]\]><\/description>/.exec(itemStr)[1];
// item.link = /<link>(.+?)<\/link>/.exec(itemStr)[1];
// item.guid = /<guid isPermaLink="false">(.+?)<\/guid>/.exec(itemStr)[1];
matches.push(item);
}
fn(matches);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment