diff --git a/CONFIGURATION.txt b/CONFIGURATION.txt old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/youtube_import.admin.inc b/youtube_import.admin.inc old mode 100644 new mode 100755 diff --git a/youtube_import.info b/youtube_import.info old mode 100644 new mode 100755 diff --git a/youtube_import.install b/youtube_import.install old mode 100644 new mode 100755 diff --git a/youtube_import.module b/youtube_import.module old mode 100644 new mode 100755 index ec1bbe3..54c9c84 --- a/youtube_import.module +++ b/youtube_import.module @@ -1,4 +1,5 @@ items as $video) { - $video_ids[] = $video->contentDetails->videoId; + if (!empty($data->items)) { + foreach ($data->items as $video) { + $video_ids[] = $video->contentDetails->videoId; + } } - return $video_ids; } @@ -273,9 +275,9 @@ function youtube_import_find($youtube_id) { // Search the database for the 1st existing node. $results = db_select("field_data_{$id_field['field_name']}", 't') - ->fields('t', array('entity_id')) - ->condition($condition, $youtube_id, "LIKE") - ->execute(); + ->fields('t', array('entity_id')) + ->condition($condition, $youtube_id, "LIKE") + ->execute(); // If I found it, load it. if ($results->rowCount()) { @@ -322,121 +324,123 @@ function youtube_import_videos() { $fieldinfo[] = 'created'; // Loop through the data to create our nodes. - foreach ($data->items as $video) { - // Find the node or create a new one. - $wrapper = youtube_import_find($video->id); - $wrapper->status = 1; - $wrapper->language(LANGUAGE_NONE); - - // Loop through the field mapping. - foreach ($mapping as $fieldname => $property) { - // If the field isn't mapped, moved on. - if (empty($property)) { - continue; - } - - // If the field isn't part of the content type, move on. - if (!in_array($fieldname, $fieldinfo)) { - continue; - } - - // Set the default back to nothing. - $value = ''; - - // Check the JSON object loctations for the key and get the value. - $value = isset($video->$property) ? $video->$property : $value; - $value = isset($video->snippet->$property) ? $video->snippet->$property : $value; - $value = isset($video->contentDetails->$property) ? $video->contentDetails->$property : $value; - $value = isset($video->statistics->$property) ? $video->statistics->$property : $value; - $value = $property == 'url' ? 'https://www.youtube.com/v/' . $video->id : $value; - - /* - * Thumbnails are really special and we want to get - * the best image to create the thumbnail - * so we go through the possible thumbnails and find the - * best one that actually exists. - */ - if ($property == 'thumbnails') { - - // Get the thumbnails object. - $thumbnails = $video->snippet->$property; - - // Loop through possible values. - foreach (array('maxres', 'standard', 'high', 'medium', 'default') as $size) { - // Determine if it was found. - if (isset($thumbnails->$size)) { - // Make its url the value. - $value = $thumbnails->$size->url; - // Break away from the for loop. - break; - } + if (!empty($data->items)) { + foreach ($data->items as $video) { + // Find the node or create a new one. + $wrapper = youtube_import_find($video->id); + $wrapper->status = 1; + $wrapper->language(LANGUAGE_NONE); + + // Loop through the field mapping. + foreach ($mapping as $fieldname => $property) { + // If the field isn't mapped, moved on. + if (empty($property)) { + continue; } - } + // If the field isn't part of the content type, move on. + if (!in_array($fieldname, $fieldinfo)) { + continue; + } - // If a value isn't found, move on. - if (empty($value)) { - continue; - } + // Set the default back to nothing. + $value = ''; - try { + // Check the JSON object loctations for the key and get the value. + $value = isset($video->$property) ? $video->$property : $value; + $value = isset($video->snippet->$property) ? $video->snippet->$property : $value; + $value = isset($video->contentDetails->$property) ? $video->contentDetails->$property : $value; + $value = isset($video->statistics->$property) ? $video->statistics->$property : $value; + $value = $property == 'url' ? 'https://www.youtube.com/v/' . $video->id : $value; - // If this field is the thumbnail. + /* + * Thumbnails are really special and we want to get + * the best image to create the thumbnail + * so we go through the possible thumbnails and find the + * best one that actually exists. + */ if ($property == 'thumbnails') { - // Create the file location. - if (function_exists('transliteration_clean_filename')) { - $filename = "public://" . transliteration_clean_filename($video->id . '.jpg'); - } - else { - $filename = "public://" . $video->id . '.jpg'; - } - // Make sure we haven't already done this. - if ($filepath = file_save_data(file_get_contents($value), $filename, FILE_EXISTS_REPLACE)) { - $wrapper->$fieldname->file->set($filepath); + // Get the thumbnails object. + $thumbnails = $video->snippet->$property; + + // Loop through possible values. + foreach (array('maxres', 'standard', 'high', 'medium', 'default') as $size) { + // Determine if it was found. + if (isset($thumbnails->$size)) { + // Make its url the value. + $value = $thumbnails->$size->url; + // Break away from the for loop. + break; + } } } - elseif ($fieldname == 'created') { - // Created date can be set directly. - $wrapper->$fieldname->set(strtotime($value)); - } - elseif ($wrapper->$fieldname->type() == 'text' || $wrapper->$fieldname->type() == 'integer') { - // If this is a text or number. - $wrapper->$fieldname = $value; + + // If a value isn't found, move on. + if (empty($value)) { + continue; } - elseif ($wrapper->$fieldname->type() == 'youtube') { - // Special functionality needed for youtube fields. - $wrapper->$fieldname = array( - 'input' => 'https://www.youtube.com/v/' . $video->id, - 'video_id' => $video->id, - ); + + try { + + // If this field is the thumbnail. + if ($property == 'thumbnails') { + // Create the file location. + if (function_exists('transliteration_clean_filename')) { + $filename = "public://" . transliteration_clean_filename($video->id . '.jpg'); + } + else { + $filename = "public://" . $video->id . '.jpg'; + } + + // Make sure we haven't already done this. + if ($filepath = file_save_data(file_get_contents($value), $filename, FILE_EXISTS_REPLACE)) { + $wrapper->$fieldname->file->set($filepath); + } + } + elseif ($fieldname == 'created') { + // Created date can be set directly. + $wrapper->$fieldname->set(strtotime($value)); + } + elseif ($wrapper->$fieldname->type() == 'text' || $wrapper->$fieldname->type() == 'integer') { + // If this is a text or number. + $wrapper->$fieldname = $value; + } + elseif ($wrapper->$fieldname->type() == 'youtube') { + // Special functionality needed for youtube fields. + $wrapper->$fieldname = array( + 'input' => 'https://www.youtube.com/v/' . $video->id, + 'video_id' => $video->id, + ); + } + else { + $wrapper->$fieldname = array('value' => $value); + } } - else { - $wrapper->$fieldname = array('value' => $value); + catch (Exception $ex) { + /* + * I can't guess at every possible field type and drupal + * doesn't seem to have an easy way to set values. Real + * pain in the ass. So, every thing else throws a pretty + * error and makes it the user's problem. + */ + drupal_set_message(t("This module is unable to set the value for a field with of @type type. Please remove @field from the import.", array('@type' => $wrapper->$fieldname->type(), '@field' => $fieldname)), 'error'); } } - catch (Exception $ex) { - /* - * I can't guess at every possible field type and drupal - * doesn't seem to have an easy way to set values. Real - * pain in the ass. So, every thing else throws a pretty - * error and makes it the user's problem. - */ - drupal_set_message(t("This module is unable to set the value for a field with of @type type. Please remove @field from the import.", array('@type' => $wrapper->$fieldname->type(), '@field' => $fieldname)), 'error'); + + // Set the author. + if (!empty($drupal_user)) { + $wrapper->author = $drupal_user; } - } - // Set the author. - if (!empty($drupal_user)) { - $wrapper->author = $drupal_user; + // Save the node. + $wrapper->save(); } - // Save the node. - $wrapper->save(); + // Tell how many we found. + drupal_set_message(count($data->items) . ' videos found.'); } - // Tell how many we found. - drupal_set_message(count($data->items) . ' videos found.'); // Update the timestamp. youtube_import_set('lastrun', time()); @@ -482,7 +486,6 @@ function youtube_import_set($key, $value = NULL, $mapping = 0) { youtube_import_set($key, $value, 0); } } - } else { /* @@ -490,18 +493,18 @@ function youtube_import_set($key, $value = NULL, $mapping = 0) { * set the actual value in the database. */ db_merge('youtube_import_settings') - ->key( - array( - 'name' => $key, + ->key( + array( + 'name' => $key, + ) ) - ) - ->fields( - array( - 'value' => $value, - 'ismap' => $mapping, + ->fields( + array( + 'value' => $value, + 'ismap' => $mapping, + ) ) - ) - ->execute(); + ->execute(); } }