I'm wirting a set of custom import scripts to import my nodes from site A to B, and I have several hundred nodes with video fields...

I have tried both node_save & drupal_execute ways of creating my nodes, with assigning the relative cck fields for video... but it just doesn't save to the database.

Is there a simple example of how you can use emfield programmatically?

Comments

joejoejoew’s picture

Try this:

function _populate_youtube_emfield(&$node, $vid, $field_name='field_video') {
  $url = 'http://gdata.youtube.com/feeds/api/videos/' . $vid . '?v=2';
  $request = emfield_request_xml('youtube', $url, array(), TRUE, FALSE, FALSE, FALSE, FALSE);

  $node->{$field_name}[0] = array(
    'embed' => 'http://www.youtube.com/watch?v=' . $vid,
    'value' => $vid,
    'provider' => 'youtube',
    'data' => array(
      //'emvideo_data_version' => 0,
      //  'emvideo_youtube_version' => 0,
      'duration' => $request['MEDIA:GROUP']['YT:DURATION'][1]['SECONDS'],
      'playlist' => 0,
      'thumbnail' => array(
        'url' => 'http://img.youtube.com/vi/' . $vid . '/0.jpg',
      ),
      'flash' => array(
        'url' => 'http://youtube.com/v/' . $vid,
        //'size' => 1082,
        'mime' => 'application/x-shockwave-flash',
      ),
    ),
    'status' => 1,
    // 'version' => 0,
    'title' => '',
    'description' => '',
    'duration' => $request['MEDIA:GROUP']['YT:DURATION'][1]['SECONDS'],
  );
}
the_g_bomb’s picture

Status: Active » Needs review

As an alternative to the above, have you thought about using the http://drupal.org/project/feeds module?

I have a custom importer setup using it, create a view on site A that outputs XML, csv, or any other format pick your preference as long as feeds supports it. Then create an import profile on site B, map the relevant fields and import away. It may just save you having to hand code eveything.

jibran’s picture

This code works fine for me.

    $node=new stdClass();
    $node->type='video';
    $node->title='test';
    $node->field_video[0]['embed']='http://www.youtube.com/watch?v=XxxXX_XxXXx';// or embed code
    $node=node_submit($node);
    if($node->validated) {
      node_save($node);
    }
Llewellyn’s picture

Hi there jibran
Does the embed code have to be a specific format?

jibran’s picture

You can add any code as long as you have enabled respective extended module(s). i.e. media_youtube, media_vimeo etc.
I hope this helps.

Llewellyn’s picture

Thanks will give this a try.

Steven Brown’s picture

I'm needing this same thing but in D7. Anyone know how I should do it?

atin81’s picture

Try with this code:

$value='DGdcTbicYzo'; //Youtube video ID

$fid = db_query('SELECT fid FROM {file_managed} WHERE uri = :uri', array(':uri' => 'youtube://v/' . $value))->fetchField();
 if (!empty($fid)) {
    $file = file_load($fid);
  } 
  else {
    $file = new stdClass();
    $file->uid = 1;
    $file->filename = $value;
    $file->uri = 'youtube://v/' . $value;
    $file->filemime = file_get_mimetype(drupal_realpath());
    $file->type = 'video';
    $file->status = 1;
    $file = file_save($file);
}

$node->field_video['und'][0] = (array) $file;
aaron’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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

rakesh.gectcr’s picture

<?php
$node = new StdClass();
$node->type = 'card';
$node->language = LANGUAGE_NONE;
$node->title = "Creating file node youtube";
$node->field_card_type['und'][0]['value'] = 'video';
$video_path = "http://www.youtube.com/watch?v=FmsgX1LkeRE";
module_load_include('inc', 'media_youtube', 'includes/MediaInternetYouTubeHandler.inc');
$obj = new MediaInternetYouTubeHandler($video_path);
$file = $obj->getFileObject();
$file->display = 1;
file_save($file);
$node->field_card_upload_video['und'][0] = (array) $file;
node_save($node);
?>