1

I'm looking for a way to import an RSS feeds into nodes.

This is an example of RSS feeds that I'm trying to import:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
  <title>List of changes (3244034544-3243867643)</title>
  <link>http://matt.dev.openstreetmap.org/owl_viewer</link>
  <description>Changes for range 3244034544-3243867643</description>
    <item>
    <title>Changeset 7989922 by Drupol</title>
    <description>
      <![CDATA[<p>By <a href="http://www.openstreetmap.org/user/Drupol">Drupol</a> covering 6 tiles, with comment "Colfontaine et alentours", using "JOSM/1.5 (4021 en)". View changeset on <a href="http://www.openstreetmap.org/browse/changeset/7989922">main OSM site</a>.</p>]]>
    </description>
    <pubDate>Wed, 27 Apr 2011 23:19:19 +0100</pubDate>
    <link>http://matt.dev.openstreetmap.org/owl_viewer/tiles/7989922</link>
    <guid>http://matt.dev.openstreetmap.org/owl_viewer/tiles/7989922</guid>
  </item>
  <item>
    <title>Changeset 7989942 by Drupol</title>
    <description>
      <![CDATA[<p>By <a href="http://www.openstreetmap.org/user/Drupol">Drupol</a> covering 1 tiles, with comment "Colfontaine et alentours", using "JOSM/1.5 (4021 en)". View changeset on <a href="http://www.openstreetmap.org/browse/changeset/7989942">main OSM site</a>.</p>]]>
    </description>
    <pubDate>Wed, 27 Apr 2011 23:19:19 +0100</pubDate>
    <link>http://matt.dev.openstreetmap.org/owl_viewer/tiles/7989942</link>
    <guid>http://matt.dev.openstreetmap.org/owl_viewer/tiles/7989942</guid>
  </item>
  .
  .
  .

I use the module Feeds and a simple importer to import the data.

What I would like to do is importing the Changeset ID, Username and Comments into CCK fields.
I've created my node 'OSMLOG' and the 3 custom fields.

Back in Feeds, I map the title to the $node->"OSM Chanseget Field", again title to $node->"OSM Username", and software to $node->"OSM Software".
Then I create my own module to work on data before assigning them:

/**
 * Implementation of hook_feeds_node_processor_targets_alter().
 */
function mymodule_feeds_node_processor_targets_alter(&$targets, $content_type) {

  dpm($content_type);

  if ($content_type == 'osmlog') {
    $targets['field_osmlog_software'] = array(
      'name' => t('OSM Log Software'),
      'description' => t('OSM Log Software'),
      'callback' => 'mymodule_set_target'
    );
    $targets['field_osmlog_changeset'] = array(
      'name' => t('OSM Log Changeset ID'),
      'description' => t('OSM Log Changeset ID'),
      'callback' => 'mymodule_set_target'
    );
    $targets['field_osmlog_user'] = array(
      'name' => t('OSM Log User'),
      'description' => t('OSM Log User'),
      'callback' => 'mymodule_set_target'
    );
  }
}


/**
 * Mapping callback.
 */
function mymodule_set_target($node, $target, $value) {
  $node->$target = "Testing";
}

If I do that, nothing is working, it seems that is never goes into the callback. I'm pretty sure that it goes into the hook_feeds_node_processor_targets_alter() but not in the callback. Is there a good documentation on how to setup such things with Feeds ?

6
  • You're not returning anything in your mymodule_set_target() function (it's still commented out).
    – 30equals
    May 2, 2011 at 13:52
  • Is it the right way to do things ? If you look at drupal.org/node/622700#mappingapi , it doesn't return something.
    – user334
    May 2, 2011 at 13:59
  • my bad, it doesn't have to return anything, but a value needs to set (//$node->$target = $value) - and yours is commented out ..? sorry about the mixup. have you looked at other feeds mappings ?
    – 30equals
    May 2, 2011 at 14:15
  • My code is commented out because I don't know if it's the right way to do. I will do something at home tonight and see if it works as it should. I'll let you know if I succeeded ;-)
    – user334
    May 2, 2011 at 14:49
  • I'm interested in Drupal 6.
    – user334
    May 2, 2011 at 22:11

1 Answer 1

1

Since you're trying to parse XML, you can avoid having to create your own Feeds parser. Instead, use Feeds XPath Parser.

What it'll do is expose a couple of new parsers including XPath XML Parser. You can then map arbitrary XPath queries to fields.

So you'd map xpathparser:0, xpathparser:1, xpathparser:2 to your CCK fields for Changeset ID, Username, and Comments, respectively.

Then, in the parser settings, you'd use the following XPath queries:

  • Context: //item
  • xpathparser:0: substring-after(substring-before(title, ' by'), 'Changeset ')
  • xpathparser:1: substring-after(title, ' by ')
  • xpathparser:2: description:text()

(Note: I couldn't figure out what you were referring to when you said "comments", so I guessed it was the <description> element. Modify to your needs if it's another element.)

If you need to provide this importer programmatically, Feeds is Features aware, and the importer can be easily exported and provided as a custom Features module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.