-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·45 lines (40 loc) · 1.56 KB
/
index.php
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
<?php
require_once __DIR__ . "/bootstrap.php";
if(ALLOW_CORS){
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
http_response_code(200);
exit();
}
}
$CaptionHandle = new \App\CaptionsHandle();
$video_url = $_GET['yt_url'] ?? $_POST['yt_url'] ?? '';
$data = new stdClass();
if(empty($video_url)){
$data->error = "no url or video ID was received";
}else{
$cookie_file = __DIR__."/my_yt_cookie_file.txt";
// $CaptionHandle->loginWithCookieFile($cookie_file);
// Uncomment the line above if you need to log in
try {
$obj = $CaptionHandle->getSubtitleURL($video_url);
if ($obj->caption_url) {
$captions_content = $CaptionHandle->httpRequest($obj->caption_url);
$captions_content = html_entity_decode($captions_content);
// Remove XML/HTML from subtitles
// replacing "<" with " <" is to prevent some words from sticking to another
$captions_content = str_replace("<", " <", $captions_content);
$captions_content = strip_tags($captions_content);
$data->caption = $captions_content;
$data->video_id = $obj->video_id;
$data->title = $obj->video_title;
$data->caption_url = $obj->caption_url;
} else {
$data->error = "Couldn't get the subtitle";
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
echo json_encode($data);