When performing a CCK content_migrate, the valid_id() test of MediaInternetYouTubeHandler object returns false when it encounters an invalid YouTube id. This behavior was likely designed for the UI but during a batch update it stops the migration with a fatal error.

This is what I'm using to get around it:

public function valid_id($id) {
    $url = 'http://gdata.youtube.com/feeds/api/videos/'. $id;
    $response = drupal_http_request($url, array('method' => 'HEAD'));
    if ($response->code != 200) {
      watchdog('video field', "The following YouYube video is invalid: " . $id);
      return TRUE;
//      throw new MediaInternetValidationException(t('The YouTube video ID is invalid or the video was deleted.'));
    }
CommentFileSizeAuthor
#1 issue-1567770.patch706 byteslsolesen

Comments

lsolesen’s picture

Status: Active » Needs work
StatusFileSize
new706 bytes

Expressed as a patch that would be the following. However, it is highly unlikely that media will change behavior, so emfield should probably catch the exception instead. So setting to needs work.

andyanderso’s picture

I think this is the same issue I am having. I am pretty new to "patching" drupal code. Maybe you could help with out with more details about where to apply this patch? In the Emfield module?, the cck migrate module? Sorry for the probably silly question, but thanks for the help :)

vm’s picture

the patch seems to be for the media_youtube module

andyanderso’s picture

I figured out where to apply the patch. I ended up having to tweak the above code just a bit and also comment out another line that caused the migration to stop whenever it encountered an error.

Here are the changes I made to my MediaInterenetYouTubeHandler.inc file.

First at line 24:

<?php
static public function validId($id) {
    $url = 'http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3D'. $id;
    $response = drupal_http_request($url, array('method' => 'HEAD'));
    if ($response->code != 200) {
//new lines below
      watchdog('video field', "The following YouYube video is invalid: " . $id);
      return TRUE;
// original code that caused the migration to stop when it encountered an error. --     throw new MediaInternetValidationException("The YouTube video ID is invalid or the video was deleted.");
        }
    return TRUE;
  }
?>

Then at line line 109:

<?php
  public function getOEmbed() {
    $uri = $this->parse($this->embedCode);
    $external_url = file_create_url($uri);
    $oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
    $response = drupal_http_request($oembed_url);
    if (!isset($response->error)) {
      return drupal_json_decode($response->data);
    }
    else {
      // commented out because caused migrate to stop when encountered an error --  throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
      return ;
    }
?>

It seems like this problem occurs when the migration tries to process these fields when they hae no data in them. Folks would not encounter this error when every node with a embedded vid field has a video stored in the field. It only seems to happen when some of the nodes with this field do not have vids in them. Since the field value is null the migration treats that as an invalid link.

Anyway with those lines replaced in the code the migration went smoothly.

Thanks for the help.

rob_johnston’s picture

The response code returned could be 200 but I'm also getting 304 (Not Modified), which is equally valid and should not be throwing an error. How about something more like the following:

    switch ($response->code) {
      case 200:
      case 304:
        return TRUE;
        break;
      default:
        throw new MediaInternetValidationException(t('The YouTube video ID is invalid or the video was deleted.'));
    }
janis_lv’s picture

Hi,
I don't know if this is the right place, I think it is, so here it goes.

I'm trying to upgrade fields from "admin/structure/content_migrate" few 1st are updated from over 2k fields.

I've got so far that the update process stops on every field where the youtube video is no longer available, as the nodes are 3-4 years old, that could happen :(
(I looked up the old fields in db - 1st that was broken, removed it from db, rerun the migrate - few more got upgraded but it keeps repeating).
the problem is that the error is breaking batch update and I can't upgrade all the fields.
I could filter them by hand, but 2k is stupidly lost time.

I've tried the code above but without any luck.
could someone suggest something?

###

turned out wasn't so hard, I also commented out the embed check to return true :D
thank you!
Sorry I'm a newb.

aangel’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)