Advertisement
Guest User

Untitled

a guest
Dec 13th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. /**
  2. * BuzzfeedController
  3. *
  4. * @description :: Server-side logic for managing buzzfeeds
  5. * @help :: See http://links.sailsjs.org/docs/controllers
  6. */
  7.  
  8. module.exports = {
  9.  
  10. buzzy: function (req, res) {
  11. var FeedParser = require('feedparser'),
  12. request = require('request');
  13.  
  14. var req = request('http://www.buzzfeed.com/index.xml'),
  15. feedparser = new FeedParser();
  16.  
  17. req.on('error', function (error) {
  18. // handle any request errors
  19. });
  20. req.on('response', function (res) {
  21. var stream = this;
  22.  
  23. if (res.statusCode != 200) return this.emit('error', new Error('Bad status code'));
  24.  
  25. stream.pipe(feedparser);
  26. });
  27.  
  28.  
  29. feedparser.on('error', function (error) {
  30. // always handle errors
  31. });
  32. feedparser.on('readable', function () {
  33. // This is where the action is!
  34. var stream = this,
  35. meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance
  36. ,
  37. item;
  38.  
  39. while (item = stream.read()) {
  40.  
  41. Buzzfeed.find({
  42. 'title': {
  43. 'contains': item.title
  44. }
  45. }).exec(function (err, data) {
  46. console.log(data);
  47. });
  48.  
  49. }
  50. });
  51.  
  52. }
  53. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement