I have a custom module and I would like it to programmatically make one of my Feeds importers run.

I searched and used Google but can't seem to find anything on this subject.

1- Is it posisble? (I'm sure it must be :)
2- Could someone give me an example or point me to a resource that explains how to do this?

Thanks!

Comments

alex_b’s picture

Status: Active » Fixed

I just added more documentation on "Programmatic operations" to http://drupal.org/node/622700 - this should answer your questions.

Anonymous’s picture

Thank you!

In my case I have added some file uploaded functionality to the user's profile page. Users will upload a CSV file of places they have visited, and once uploaded I want to trigger my CSV Feeds importer to run, using the uploaded file.

From what you wrote, I can guess that the important line for me is the one below (I don't especially need a progress bar)

while (FEEDS_BATCH_COMPLETE != feeds_source('my_importer_id', $node->nid)->import());

A few questions:

1- my_importer_id is the machine name of the importer, correct?
2- Why the need for a $node->nid?

Thanks!

Anonymous’s picture

Category: support » feature

I've played around with the code and have gotten the following to work:

  $myFeed = feeds_source('geocache_importer');
  $config = array('FeedsFileFetcher'=>array('source'=>'sites/default/files/' . $file));
  $myFeed->addConfig($config);
  while (FEEDS_BATCH_COMPLETE != $myFeed->import());

I've tried it with a test file that contains just one CSV line entry and so far so good. Thanks for pointing me in the right direction!

Closing this issue and I'm raising a new issue at http://drupal.org/node/753580 for a feature request to add a method to change the feed source's Fetcher's source:

feeds_source->setFetcherSource()

Status: Fixed » Closed (fixed)

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

Sinan Erdem’s picture

Status: Closed (fixed) » Active

I am using this code in an action of a rule:

  $myFeed = feeds_source('my_importer');
  $config = array('FeedsFileFetcher'=>array('source'=>'sites/default/files/myfile.xml'));
  $myFeed->addConfig($config);
  while (FEEDS_BATCH_COMPLETE != $myFeed->import());

When the rule is triggered it gives the following error:

warning: file_get_contents(): Filename cannot be empty in /var/www/html/otty.fundeon.nl/sites/all/modules/feeds/plugins/FeedsFileFetcher.inc on line 28.

Feeds XPath parser: The XML document is empty.

I also tried 'FeedsHTTPFetcher' instead of 'FeedsFileFetcher' but it doesnt change anything.

Thanks in advance....

automaticerik’s picture

I've followed everything in here, but it looks like all of this is in reference to a feed that isn't attached to a node. If I have a feed that is attached to a node, how should this be accomplished?

EDIT: Nevermind, found that feeds_source takes a node id as a second argument. Now it works great!

wuinfo - bill wu’s picture

If I develop an stand alone php page to trigger the importer, what are the complete set of required php or inc pages needed?

rbennata’s picture

I'm trying to run the import from an external shell so my code looks like:

        define("DRUPAL_ROOT", "/opt/bitnami/apps/drupal/htdocs/");
        $_SERVER['REMOTE_ADDR']="127.0.0.1";
        require_once '../../../includes/bootstrap.inc';
        drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

        error_reporting(E_ALL);

        $feed = $_SERVER['argv'][1];
        $folder = $_SERVER['argv'][2];
        $files = glob($folder . "*.csv");

        foreach($files as $filename) {
                print (date("m/d/Y h:i:s a", time()) . ": Starting import for $filename\n");
                $feedSource = feeds_source($feed);
                $config = $feedSource->getConfig();
                $config['FeedsFileFetcher']['source'] = $filename;
                $feedSource->setConfig($config);
                $feedSource->save();
                while (FEEDS_BATCH_COMPLETE != $feedSource->import());
                print (date("m/d/Y h:i:s a", time()) . ": Ended import for $filename\n");
        }

It runs, and it even shows on the feed log with "node created" or "node updated". But the nodes are not really created or updated to the database.

Is there some "commit" I also need to do because it is a separate process?
Or - what is wrong with this code?
(this is D7)

sisko’s picture

Version: 6.x-1.0-alpha12 » 7.x-2.0-alpha8

@Sinan Erdem: I tried your code (#5) in a Rule action on a D7 site and it worked fine

Thank you!

peter panes’s picture

Issue summary: View changes

Is there an easy way to pass the feed itself in from a variable?
Otherwise im just creating extra work and load by saving the contents of the variable to a file just so the feed importer can can load it from the file.

timellemeet’s picture

@peter panes

I tweaked feeds callback fetcher. Instead of passing on a function name a pass on a variable and have a default function simply return that function.

thomaswalther’s picture

bluegeek9’s picture

Status: Active » Closed (outdated)
//www.flaticon.com/free-icons/thank-you Thank you for your contribution!

Unfortunately, Drupal 7 is End of Life and no longer supported. We strongly encourage you to upgrade to a supported version of Drupal.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.