File size: 2,944 Bytes
5f32fbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | <?php
namespace App\Http\Controllers;
use App\Services\Youtube\YoutubeTranscriptService;
use Illuminate\Support\Facades\File;
class TestController extends Controller
{
public function test()
{
// Example usage:
$videoUrl = 'https://www.youtube.com/watch?v=M7FIvfx5J10';
dd((new youtubeTranscriptService)->getTranscript($videoUrl));
}
public function getYoutubeTranscript($videoUrl)
{
// Initialize cURL session to get the HTML content of the YouTube video page
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $videoUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Search for "captionTracks" in the HTML content
$matches = [];
preg_match('/"captionTracks":(\[.*?\])/', $response, $matches);
dd($matches);
if (isset($matches[1])) {
// Decode the JSON structure
$captionTracks = json_decode($matches[1], true);
if (isset($captionTracks[0]['baseUrl'])) {
// Get the base URL for the captions
$baseUrl = $captionTracks[0]['baseUrl'];
// Decode the Unicode \u0026 into &
$baseUrl = html_entity_decode($baseUrl, ENT_QUOTES, 'UTF-8');
// Fetch the captions
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$captions = curl_exec($ch);
curl_close($ch);
return $captions;
} else {
throw new Exception('No caption tracks found.');
}
} else {
throw new Exception('No captionTracks found in the response.');
}
}
public function collectMissingStrings()
{
// Get all translatable strings in the app
$strings = collect();
// Replace 'resources' with the actual directory containing your views and files
$files = File::allFiles(resource_path());
foreach ($files as $file) {
$content = file_get_contents($file);
preg_match_all('/__\((\'|")(.*?)(\'|")\)/', $content, $matches);
foreach ($matches[2] as $match) {
$strings->push($match);
}
}
// Load existing translations
$existingTranslations = json_decode(file_get_contents(base_path('lang/en.json')), true);
// Add new strings to the translations if the keys do not exist
foreach ($strings->unique() as $string) {
if (! isset($existingTranslations[$string])) {
$existingTranslations[$string] = $string;
}
}
// Write updated translations to en.json
file_put_contents(base_path('lang/en.json'), json_encode($existingTranslations, JSON_PRETTY_PRINT));
}
}
|