[youtube_transcript_tool]
function get_youtube_transcript($video_id) { global $wpdb; $table_name = $wpdb->prefix . 'youtube_transcripts'; // Cache check karein $cached = $wpdb->get_var($wpdb->prepare( "SELECT transcript FROM $table_name WHERE video_id = %s AND created_at > DATE_SUB(NOW(), INTERVAL 7 DAY)", $video_id )); if ($cached) return $cached; // Naya method - YouTube Data API use karein $api_keys = [ 'AIzaSyB6gEqWN2Xqno8xQ7aeAQpOrZ_6dS6ZQdI', 'AIzaSyA_9c24-5kHqayUv4fR3K81IVW7v3tJqFc', 'AIzaSyDqXaxXrF1wEvd2lAuI2uH0ZwmZ7bEWkzY' ]; shuffle($api_keys); $api_key = $api_keys[0]; $api_url = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$video_id&key=$api_key"; $response = wp_remote_get($api_url); if (is_wp_error($response)) return false; $data = json_decode(wp_remote_retrieve_body($response), true); // Captions available hai ya nahi check karein if (empty($data['items'][0]['snippet']['caption'])) { return false; } // Transcript fetch karne ka asli tarika $transcript_url = "https://video.google.com/timedtext?lang=en&v=$video_id"; $transcript_response = wp_remote_get($transcript_url); if (is_wp_error($transcript_response)) return false; $transcript_xml = wp_remote_retrieve_body($transcript_response); // XML ko process karein $xml = simplexml_load_string($transcript_xml); if (!$xml) return false; $transcript = ''; foreach ($xml->text as $text_line) { $transcript .= (string)$text_line . ' '; } // Extra spaces hatayein $transcript = preg_replace('/\s+/', ' ', trim($transcript)); // Cache mein save karein $wpdb->insert($table_name, [ 'video_id' => $video_id, 'transcript' => $transcript, 'created_at' => current_time('mysql') ]); return $transcript; }
[youtube_transcript_tool]