Question

I'm trying to parse an item from RSS but only if it contains certain string in it's title.

I have a Ember RSS parser app that works as log checker for one game. For example, here it shows a "adventure log" for a player called "Versusik":

http://alog.questscape.eu/#/logs/1/index

Here's the code for scraping information from URL that contains a RSS feed I'm working with.

"feed.js":

App.Feed = DS.Model.extend({
  name: DS.attr('string'),
  url: DS.attr('string'),

  feedItems: DS.hasMany('App.FeedItem'),

  refresh: function() {
    var url = this.get('url');
    var googleUrl = document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=http://services.runescape.com/m=adventurers-log/k=4/rssfeed?searchName=' + encodeURIComponent(url);
    $.ajax({
      url: googleUrl,
      dataType: 'json',
      context: this,
      success: function(data) {
        var feed = data.responseData.feed;
        var items = feed.entries.forEach(function(entry) {
          if(this.get('feedItems').findProperty('link', entry.link)) {
            return;
          }
          var feedItem = this.get('feedItems').createRecord({
            title: entry.title,
            author: entry.author,
            body: entry.content,
            bodySnippet: entry.contentSnippet,
            link: entry.link,
            publishedDate: entry.publishedDate
          });
        }, this);
        this.get('store').commit();
      }
    });
  }
});

App.Feed.FIXTURES = [
  {id: 1, name: 'Versusik', url: 'Versusik'},
  {id: 2, name: 'Questalion', url: 'Questalion'},
  {id: 3, name: 'Mokuin', url: 'Mokuin'},
  {id: 4, name: 'Aiimer', url: 'Aiimer'},
  {id: 5, name: 'Jakube33', url: 'Jakube33'},
  {id: 6, name: 'Mordor Prime', url: 'Mordor_Prime'},
  //...
  //...it goes on for every Player I've specified...
  //...
  {id: 53, name: 'TooShortName', url: 'TooShortName'},

];

And here is code for single items.

"feed_item.js":

App.FeedItem = DS.Model.extend({
  title: DS.attr('string'),
  author: DS.attr('string'),
  content: DS.attr('string'),
  contentSnippet: DS.attr('string'),
  link: DS.attr('string'),
  publishedDate: DS.attr('date'),

  feed: DS.belongsTo('App.Feed')
});

App.FeedItem.FIXTURES = []; 

It work as expected, parsing data from http://services.runescape.com/m=adventurers-log/rssfeed?searchName=("Name of player", you can try Versusik) and displays it inside my ember app.

But what if I only want to parse an item if it title contains string "dragon helm" (while full item title says "I found a dragon helm")?

Basically I want to make similar app but it will only parse those strings and put it inside a table like this.

Thank you in advance for any help provided.

Was it helpful?

Solution

But what if I only want to parse an item if it title contains string "dragon helm" (while full item title says "I found a dragon helm")?

Use named functions to separate the concerns:

function foo(data)
  {
  /* feed data */
  var feed = data.responseData.feed;
  /* feed entry transformation */
  var items = feed.entries.forEach(function(entry) {bar(entry, this)}); 

  this.get('store').commit();
  }

function bar(entry, that)
  {
  if(that.get('feedItems').findProperty('link', entry.link) == false) 
    {
    return;
    }
  else
    {
    that.get('feedItems').createRecord({
        title: entry.title,
        author: entry.author,
        body: entry.content,
        bodySnippet: entry.contentSnippet,
        link: entry.link,
        publishedDate: entry.publishedDate
      });
    }
  }

then add the conditional check to the success callback:

success: function(data) {
  /* Test for "dragon helm" in stringified JSON */
  if /dragon\shelm/.test(JSON.stringify(data))
    {
    foo(data);
    }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top