Can I restrict the number of feed items imported, for example:

  • By number, eg. the latest 20
  • By date, eg. Not older than 20 days

Comments

luckysmack’s picture

Subscribing, i'd like to know the same thing as well. I see no options to choose how many items are pulled when the feed is aggregated. An option for this would be great.

tfranz’s picture

Just a hint: I had quite a similar problem and solved it this way:

Added some code in "FeedsNodeProcessor.inc", rows 23ff:

  /**
   * Implementation of FeedsProcessor::process().
   */
  public function process(FeedsImportBatch $batch, FeedsSource $source) {

    // Keep track of processed items in this pass, set total number of items.
    $processed = 0;
    if (!$batch->getTotal(FEEDS_PROCESSING)) {
      $batch->setTotal(FEEDS_PROCESSING, count($batch->items));
    }
// START ADDED CODE
    $nodefeedcounter = 0;   //reset the counter of existing nodes
	$nodefeedlimiter = 10;  // how many items do you accept?
	$sql = "SELECT COUNT(*) FROM dwb_node WHERE type='feednode'";
	$nodefeedcounter = db_result(db_query($sql)); // result: counts all existing nodes of type 'feednode' in the database
	array_splice($batch->items, $nodefeedlimiter-$nodefeedcounter); //  THATS the main part: just changing the length of the $batch-Array
// END ADDED CODE

    while ($item = $batch->shiftItem()) {  
    // ... and so on
    }

Hope it helps somebody ... at least it works for me ... :-)

mlmoseley’s picture

Issue summary: View changes

After much reading through the code , I solved this problem by changing one line. In feeds-6.x-1.0-beta13, in:

feeds/plugins/FeedsNodeProcessor.inc

On or about line 25, there's a function called:

public function process(FeedsImportBatch $batch, FeedsSource $source)

That function uses the variable '$batch_size', which is the number of nodes to import, as set in settings.php with this statement:

$conf['feeds_node_batch_size'] = 10;

At this point in the code, the function is looping through an array of fifty feed items, and creating nodes from them. Note: the array of feed items already exist. At the bottom of the loop the code compares $batch_size to $processed (the number of nodes processed) and provides an IF statement saying what to do if the $batch_size is equal to or greater than the number processes. In other words, when the loop has hit the limit. Here's the IF statement.

      if ($processed >= $batch_size) {
        $total = $batch->getTotal(FEEDS_PROCESSING);
        $batch->setProgress(FEEDS_PROCESSING, $total - count($batch->items));
        return;
      }

In this context 'return' is supposed to end the execution of the function, but it doesn't. It seems to just end the current iteration of the 'while' loop in which it resides. I replaced 'return' with 'break', and suddenly the node limit in settings.php worked.

This solved my problem because I needed fewer than fifty items. I don't know what would happen if you set the limit to more than fifty.

I would REALLY like to not hack a module. Really. But I just couldn't find any other solution to this.

--Marshall

MegaChriz’s picture

Status: Active » Fixed

Limiting the number of items to import can be done easily by implementing hook_feeds_after_parse(). Via that hook all items to import are passed, so you can simply loop through them and unset the items you don't want to import.

Example for Feeds 6.x-1.x:

/**
 * Implementation of hook_feeds_after_parse().
 */
function mymodule_feeds_after_parse($importer, $source) {
  $count = 0;
  foreach ($source->batch->items as $item_key => $item) {
    $count++;
    if ($count > 10) {
      unset($source->batch->items[$item_key]);
    }
  }
}

Example for Feeds 7.x-2.x:

/**
 * Implements hook_feeds_after_parse().
 */
function mymodule_feeds_after_parse($source, $result) {
 $count = 0;
 foreach ($result->items as $item_key => $item) {
   $count++;
   if ($count > 10) {
     unset($result->items[$item_key]);
   }
 }
}

For skipping items that contain a certain phrase, you can also use the Keyword filter plugin from Feeds Tamper.

For skipping items that are older than a certain date, see #2453845: Limit updating items to x days.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

sbydrupal’s picture

Can you please comment on #10 at https://www.drupal.org/node/1909974 whether it is possible to
solve ? I don't want to replicate same comment in this thread. Thanks

arne.olafson’s picture

I created a project for this here: https://www.drupal.org/project/single_feeds