Hi, I'm planning on putting a store's inventory on a Drupal site and I'm wondering if it's possible to create a script (maybe in python/php?) to enter the data automatically to Drupal with CCK? Thanks in advance!
CCK or otherwise, it's just a wellformed POST query (presumably), so sure, go for it.
The fastest and easiest thing would be to do the stuff with a little Drupal module you make for the case, instead of having to send lots of posts to the server and spend resources on node loads and what not.
Anyways, what you need for this is quite similar to what mac answers here:
In this case you don't need all the special file_field stuff, but you still need to insert the values for the different cck fields you might have and the node body and title. After setting the value which you could get directly from your database, you can save your node.
If you connect to the db directly, you need to have the same type as the one you use for drupal, or do it outside the Drupal api. If you do use the drupal API for it, take a look at db_set_active()
There are a couple of Drupal modules dedicated to different scenarios of external (mass) imports - check this overview for a options/comparisons.
If you have very specific needs, you could write your own module, using the existing ones and the links/hints provided by googletorps (+1) for guidance on how to do the actual insertion while ignoring the generalizations.
Lots of good suggestions have been done by Henrik and Googletorp already.
A few additional elements to consider to design your strategy:
- Are you going to do a full-fledged e-store (presumably realised with ubercart) or are you simply setting up a view of nodes, just to present the inventory to site visitors?
- How many products are you going to import?
- How often are you going to reimport them?
Solutions that I would feel to exclude for sure:
- POST: as commented by googletorp, it would be overly complicated.
- External script: you can't really avoid (unless you like to live dangerously and/or have time to waste) using drupal API, whether they are the core ones or those of ubercart. Data is scattered across several tables, and there are plenty of hooks that are triggered when a node is inserted. The only exception is if you would do a PHP script that perform the bootstrap first (see the structure of index.php or xmlrpc.php to see how it works), but in that case I would rather go for a module altogether: much more elegant, portable, maintainable.
Solutions that I would support:
- Do your own module! As pointed out by googletorp, I gave some sample code on how to add CCK fields in this answer.
- Yes, that is... is the only one I believe in! ;)
However what I learned is equally important, is to pick a suitable source of data for the import. Here's my opinion:
- Read directly from DB: Good only if you have to import stuff once and for all and if the DB schema of the exporting application is simple enough to build sensible queries. Software changes and evolves, and DB schemas follow. If you find yourself in the need of re-importing in two months time and the schema of your other application has changed, you will have to change your code, change tests, etc, etc...
- Use XML files: If your original application can export in this format, with PHP' SimpleXML and Xpath + PHP typecasting it's really a breeze to get the data you want in the format you want in a matter of minutes. The only downside of this method is that relies on... files. So, if you need to have periodic unmanned and automatic imports, it's a bit of a pain to foresee all the problems that can happen (wrong permissions on filesystems, corrupted files, wrong encoding...) and to put in place counter-measures. Vice-versa, I like this method a lot for when I know somebody will be supervising the import process all the time and can intervene in case of troubles.
- Webservice: That is the one I like the most if I have to import in an automatic and periodic fashion. The biggest advantage is that the two applications "talk" to each other, and expose some of their business logic, so that you can actually have a session that looks like: "hey, I need all the products that have changed prices since last week" - "here you are, they should be 127, in three categories, do you copy that?" - "Oh yeah... Got all of them loud and clear: 127 items and 3 categories!". This simplify a lot trapping errors and exceptions. While Drupal works out the box as a webservice consumer and provider, you will have to implement webservice also on the other application, and this might or might not be difficult: it totally depends from the exporting application.
HTH!
If your source data is in MySQL I would be looking at the migrate module (http://drupal.org/project/migrate) to create content.