/** * Get YouTube Transcript * * Generate re-encoded YouTube transcript * * @since 2.0 * * @uses vye_extract_id Extract the ID * @uses vye_get_file Get a file * @uses vye_set_general_defaults Set default options * @uses vye_validate_id Validate the ID * * @param string $id Video ID * @param string $language Language * @return string $output Transcript output */ function vye_generate_transcript($id, $language = 'en') { // Extract the ID if a full URL has been specified $id = vye_extract_id($id); // Check what type of video it is and whether it's valid $embed_type = vye_validate_id($id); if ($embed_type != 'v') { if (strlen($embed_type) > 1) { return vye_error($embed_type); } else { return vye_error(sprintf(__('The YouTube ID of %s is invalid.', 'youtube-embed'), $id)); } } // Get general options $general = vye_set_general_defaults(); // Check to see if cache is available if ($general['transcript_cache'] != 0) { $cache_key = 'vye_transcript_' . $id; $output = get_transient($cache_key); if ($output !== false) { return $output; } } // Get transcript file $return = vye_get_file('http://video.google.com/timedtext?lang=' . strtolower($language) . '&v=' . $id); $xml = $return['file']; $output = ''; // If transcript file exists, strip and output if ($return['rc'] == 0) { $output = "<!-- YouTube Embed v" . youtube_embed_version . " -->\n"; $pos = 0; $eof = false; while (!$eof) { $text_start = strpos($xml, '<text ', $pos); if ($text_start !== false) { // Extract the start time $start_start = strpos($xml, 'start="', $text_start) + 7; $start_end = strpos($xml, '"', $start_start) - 1; $start = substr($xml, $start_start, $start_end - $start_start + 1); // Convert time format $start = str_pad(floor($start), 3, '0', STR_PAD_LEFT); $start = substr($start, 0, -2) . ':' . substr($start, -2, 2); // Now extract the text $text_start = strpos($xml, '>', $text_start) + 1; $text_end = strpos($xml, '</text>', $text_start) - 1; $text = substr($xml, $text_start, $text_end - $text_start + 1); // Now return the output $output .= "<div class=\"Transcript\"><span class=\"TranscriptTime\">" . $start . "</span> <span class=\"TranscriptText\">" . htmlspecialchars_decode($text) . "</span></div>\n"; $pos = $text_end + 7; } else { $eof = true; } } $output .= "<!-- End of YouTube Embed code -->\n"; } // Save the cache if ($general['transcript_cache'] != 0) { set_transient($cache_key, $output, 3600 * $general['transcript_cache']); } return $output; }
/** * Get XML transcript * * Return XML formatted YouTube transcript * * @since 2.0 * * @uses vye_error Output an error * @uses vye_extract_id Extract a video ID * @uses vye_get_file Get a file * @uses vye_validate_id Check the video ID is valid * * @param string $id YouTube video ID * @param string $id Language * @return string Transcript file in XML format */ function get_youtube_transcript_xml($id, $language = 'en') { // Extract the ID if a full URL has been specified $id = vye_extract_id($id); // Check what type of video it is and whether it's valid $embed_type = vye_validate_id($id); if ($embed_type != 'v') { if (strlen($embed_type) > 1) { echo vye_error($embed_type); } else { echo vye_error(sprintf(__('The YouTube ID of %s is invalid.', 'youtube-embed'), $id)); } return; } // Get transcript file $filename = 'http://video.google.com/timedtext?lang=' . strtolower($language) . '&v=' . $id; $xml = vye_get_file($filename); // Check success and return appropriate output if ($xml['rc'] > 0) { echo vye_error(sprintf(__('Could not fetch the transcript file %s.', 'youtube-embed'), $id)); return; } else { return $xml; } }
/** * Get YouTube comments * * Generate output of video comments * * @since 3.2 * * @uses vye_extract_id Extract the ID * @uses vye_get_file Get a file * @uses vye_set_general_defaults Set default options * @uses vye_validate_id Validate the ID * * @param string $id Video ID * @param string $language Language * @return string $output Transcript output */ function vye_generate_comments($paras) { extract($paras); if ($id == '') { return vye_error(__('No YouTube ID was specified.', 'youtube-embed')); } // Extract the ID if a full URL has been specified $id = vye_extract_id($id); // Check what type of video it is and whether it's valid $embed_type = vye_validate_id($id); if ($embed_type != 'v') { if (strlen($embed_type) > 1) { return vye_error($embed_type); } else { return vye_error(sprintf(__('The YouTube ID of %s is invalid.', 'youtube-embed'), $id)); } } // Get general options $general = vye_set_general_defaults(); // Get cache time if ($cache == '') { $cache = $general['comments_cache']; } // Check to see if cache is available if ($cache != 0) { $cache_key = 'vye_comments_' . $id; $output = get_transient($cache_key); $output = false; if ($output !== false) { return $output; } } // Get transcript file $return = vye_get_file('https://gdata.youtube.com/feeds/api/videos/' . $id . '/comments?orderby=published'); $xml = $return['file']; $output = ''; // If comments file exists process the XML and build the output if ($return['rc'] == 0) { // Set default values $output = "<!-- YouTube Embed v" . youtube_embed_version . " -->\n" . '<div id="comments">' . "\n"; $pos = 0; $eof = false; $number = 1; if (!is_numeric($avatar)) { $avatar = 32; } if (!is_numeric($limit)) { $limit = 10; } $tab = "\t"; while (!$eof) { $entry = vye_extract_xml($xml, 'entry'); if ($entry != '') { // Extract field data from XML $published = human_time_diff(strtotime(vye_extract_xml($entry, 'published'))); $comment = vye_extract_xml($entry, 'content'); $user = vye_extract_xml($entry, 'name'); $profile_url = 'http://www.youtube.com/user/' . $user; $user_xml_url = vye_extract_xml($entry, 'uri'); // Get user image URL from a seperate XML file $image_url = ''; $user_xml = wp_remote_get($user_xml_url); $start_pos = strpos($user_xml['body'], '<media:thumbnail url='); if ($start_pos !== false) { $start_pos = $start_pos + 22; $end_pos = strpos($user_xml['body'], '/>', $start_pos); if ($end_pos !== false) { $end_pos = $end_pos - 2; $image_url = substr($user_xml['body'], $start_pos, $end_pos - $start_pos + 1); } } // Using the information fetched above built the comment output $output .= '<div id="comment-' . $number . '">' . "\n"; $output .= $tag . '<div class="comment-author">' . "\n"; if (is_numeric($avatar)) { $output .= $tab . $tab . '<img alt="" src="' . $image_url . '" class="avatar avatar-' . $avatar . '" height="' . $avatar . 'px" width="' . $avatar . 'px" />' . "\n"; } $output .= $tab . $tab . '<cite class="fn"><a href="' . $profile_url . '" rel="external nofollow" class="url">' . $user . '</a></cite>' . "\n"; $output .= $tab . '</div>' . "\n"; $output .= $tab . '<div class="comment-meta commentmetadata">' . $published . ' ' . __('ago', 'youtube-embed') . '</div>' . "\n"; $output .= $tab . '<div class="comment-body"><p>' . $comment . '</p></div>' . "\n"; $output .= '</div>' . "\n"; $number++; if ($number > $limit) { $eof = true; } } else { $eof = true; } // Remove current record from the XML file $next_record = strpos($xml, '</entry>'); if ($next_record !== false) { $xml = substr($xml, $next_record + 8); } } $output .= "</div>\n<!-- End of YouTube Embed code -->\n"; } // Save the cache if ($cache != 0) { set_transient($cache_key, $output, 60 * $cache); } return $output; }