Skip to content

Instantly share code, notes, and snippets.

@mikekunze
Created February 17, 2012 21:37
Show Gist options
  • Save mikekunze/1855612 to your computer and use it in GitHub Desktop.
Save mikekunze/1855612 to your computer and use it in GitHub Desktop.
Get Blogger Posts with NodeJS
require 'iced-coffee-script'
mongoose = require 'mongoose'
httpAgent = require 'http-agent'
async = require 'async'
mongoConnect = 'mongodb://username:[email protected]:27367/db'
mongoose.connect mongoConnect
Schema = mongoose.Schema
ObjectId = Schema.ObjectId
mongoose.model 'blogpost', new Schema {
content: String
title: String
timestamp: Date
}
agent = httpAgent.create 'blogName.blogspot.com', ['feeds/posts/default?alt=json']
agent.addListener 'next', (e, agent) =>
response = JSON.parse agent.body
eachEntry = (item, cb) ->
BlogPost = mongoose.model 'blogpost'
BlogPost.findOne { title: item.title.$t }, (err, doc) ->
if !doc
blogPost = new BlogPost()
blogPost.content = item.content.$t
blogPost.title = item.title.$t
blogPost.timestamp = item.published.$t
blogPost.save (err) =>
console.log 'Done saving ' + item.title.$t
cb()
if doc
console.log 'cached ' + item.title.$t
cb()
async.forEach response.feed.entry, eachEntry, () ->
console.log '\n'
process.exit()
agent.next()
agent.addListener 'stop', (e, agent) ->
console.log 'Agent has completed visiting all urls\n\n'
agent.start()
@mikekunze
Copy link
Author

added async

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment