Hi folks,
I've been hacking on the FeedAPI for my own purposes, and figured I'd share to see if I could have the code I've written included in some form or another. I'm not sure this is a feature that should be enabled by default but what I've done is added a few bits of code to the feedapi_item.module function feedapi_item_feedapi_item_save():
/**
* Create a node from the feed item
* Store the relationship between the node and the feed item
* Implementation of hook_feedapi_item_save().
* @todo User shouldn't be the user of the time when item is saved.
* User should be the owner of the feed.
*/
function feedapi_item_feedapi_item_save($feed_item, $feed_nid, $settings = array()) {
// Construct the node object
$node = new stdClass();
$node->type = !empty($settings['content_type']) ? $settings['content_type'] : variable_get('feedapi_item_type', 'story');
// Get the default options from the cont
$options = variable_get('node_options_'. $node->type, FALSE);
if (is_array($options)) {
$node->status = in_array('status', $options) ? 1 : 0;
$node->promote = in_array('promote', $options) ? 1 : 0;
$node->sticky = in_array('sticky', $options) ? 1 : 0;
}
else {
$node->status = 1;
}
$node->title = $feed_item->title;
global $user;
$node->uid = isset($settings['uid']) ? $settings['uid'] : $user->uid;
$node->created = (isset($settings['node_date']) && $settings['node_date'] == 'feed') ? $feed_item->options->timestamp : time();
$node->body = $feed_item->description;
if (isset($feed_item->options->teaser)) {
$node->teaser = $feed_item->options->teaser;
}
/*********************************************************************/
/* This is an attempt to format the tags so that taxonomy_node_save
can parse them (DD -08.21.07) */
$tmp_terms = array();
$terms_array = array();
foreach ($feed_item->options->tags as $this_tag_id => $this_tag) {
$tmp_terms[1] .= $this_tag . ", ";
}
$terms_array['tags'][1] = preg_replace('/, $/', '', $tmp_terms[1]);
/*********************************************************************/
if (!isset($feed_item->nid)) {
node_save($node);
// Added to see if we could save categories from feeds: (DD -08.21.07)
taxonomy_node_save($node->nid, $terms_array);
$feed_item->nid = $node->nid;
db_query("INSERT INTO {feedapi_node_item} (feed_nid, nid, url, timestamp, arrived, guid) VALUES (%d, %d, '%s', %d, %d, '%s')", $feed_nid, $fee\
d_item->nid, $feed_item->options->original_url, $feed_item->options->timestamp, time(), $feed_item->options->guid);
}
else {
$node->nid = $feed_item->nid;
node_save($node);
// Added to see if we could save categories from feeds: (DD -08.21.07)
taxonomy_node_save($node->nid, $terms_array);
db_query("UPDATE {feedapi_node_item} SET url = '%s', timestamp = %d, guid = '%s' WHERE fiid = %d", $feed_item->options->original_url, $feed_it\
em->options->timestamp, $feed_item->options->guid, $feed_item->fiid);
}
return $feed_item;
}
The parts I've added have been commented, I hope it is clear.
Here are the caveats, from my perspective:
- This addition adds tags from RSS feeds automatically, and they then become part of your free tag vocabulary. As far as I know it works smoothly, but this is the first serious hacking on Drupal code I've done, so who knows what mistakes I've made.
- This should probably be set up as a non-default option, so there should probably be some more code added to make this something users can turn on if they want it.
- I believe I've done this in a 'Drupal-ly' fashion, but I really am not sure. I probably could stand to read some more developer docs.
- I have not tested this with the parser_common_syndication parser, only parser_simplepie.
- I'm probably forgetting something else.
Anyways, if this seems like something you'd want included (I know I do) but the coding I've done doesn't follow good form for any reason, let me know and I'd be glad to try and re-hack it to conform. Let me know what I can do to get this feature included in the module, in any case--it seems that it should be included since tags/categories are being collected already (at least by the simplepie parser).
Comments
Comment #1
aron novakIt looks awesome! A new feature that really makes sense! :)
I don't know that you have time or not to make this patch more perfect, but i'll commit into the repository after it is an optional thing.
I mean that the user should be able to decide if (s)he wants the terms extracted or not automatically. (for example because of the Yahoo Terms module)
You can create this new setting option by the following (this option should be per-feed):
- add your new setting to feedapi_item_settings_form() - feedapi will take care of save/load/etc
- get your new setting in feedapi_item_save() via the settings array
Please let me know if you have time to implement this. I can't wait for the new version of your patch ;)
(if not it's likely that i'll do)
Comment #2
dubitable commentedGreat--I'm slammed right now with my workload (it's the beginning of our semester, I work for a university) but I will add the optional-ity ASAP and resubmit. Thanks for the encouragement!
Comment #3
momper commentedyes - this would be a nice optinal feature ...
greetings momper
Comment #4
Draggan commentedthis looks good. just for the record: it's feedapi_node/feedapi_node.module now
del.icio.us feeds were not correctly imported, all tags were used as one tag.
as a quick hack i split them up first:
foreach ($feed_item->options->tags as $this_tag_id => $this_tag) {
// case: tags are seperated by spaces (del.icio.us)
$this_tag = preg_replace('/ /', ', ', $this_tag);
$tmp_terms[1] .= $this_tag . ", ";
}
Comment #5
alex_b commentedFYI, this functionality is covered by the feed element mapper, available in my sandbox: http://cvs.drupal.org/viewvc.py/drupal/contributions/sandbox/alex_b/feed...
delicious tags might need some extra love there, too. - the delicious tags issue should actually be reported to simple pie, though.
I will roll out feed element mapper as soon as i ve got some time here.
In its current version you can map any feed item element to any taxonomy. You can configure its behaviour on a per node type and on a per node basis. One hot application is to map author tags to a taxonomy, which allows you to aggregate authors of items.
Here is the issue for the Feed Element Mapper: http://drupal.org/node/165388
Comment #6
yellek commentedHave a look at http://simplepie.org/wiki/addons/del.icio.us. If there was some way to include this in the simplepie support provided by feedapi we might be on the road to a solution.
Can anyone hazard a suggestion as to what modifications might be required?