1 | <?php |
||
2 | |||
3 | namespace Jackal\Downloader\Ext\Youtube\Downloader; |
||
4 | |||
5 | use Jackal\Downloader\Downloader\AbstractDownloader; |
||
6 | use Jackal\Downloader\Ext\Youtube\Filter\VideoResultFilter; |
||
7 | use Jackal\Downloader\Ext\Youtube\Validator\CUrlValidator; |
||
8 | |||
9 | class YoutubeDownloader extends AbstractDownloader |
||
10 | { |
||
11 | /** @var array $options */ |
||
12 | protected $options; |
||
13 | |||
14 | /** @var string $youtubeVideoURL */ |
||
15 | protected $youtubeVideoURL; |
||
16 | |||
17 | /** @var VideoResultFilter $videoFilter */ |
||
18 | protected $videoFilter; |
||
19 | |||
20 | protected $downloadLinks = []; |
||
21 | |||
22 | public function __construct($id, array $config = []) |
||
23 | { |
||
24 | if(!array_key_exists('allow-no-audio', $config)){ |
||
25 | $config['allow-no-audio'] = false; |
||
26 | } |
||
27 | |||
28 | parent::__construct($id, $config); |
||
29 | |||
30 | $this->videoFilter = new VideoResultFilter($config['allow-no-audio']); |
||
31 | $this->videoFilter->setValidator(new CUrlValidator()); |
||
32 | |||
33 | $this->youtubeVideoURL = 'https://www.youtube.com/watch?v=' . $this->getVideoId(); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @return array |
||
38 | * @throws \Jackal\Downloader\Ext\Youtube\Exception\YoutubeDownloaderException |
||
39 | */ |
||
40 | protected function getDownloadLinks() : array{ |
||
41 | if($this->downloadLinks == []) { |
||
42 | $yt = new \YouTube\YouTubeDownloader(); |
||
43 | $this->downloadLinks = $yt->getDownloadLinks($this->youtubeVideoURL); |
||
44 | $this->downloadLinks = $this->videoFilter->filter($this->downloadLinks, []); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
45 | } |
||
46 | |||
47 | return $this->downloadLinks; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | * @throws \Jackal\Downloader\Exception\DownloadException |
||
53 | * @throws \Jackal\Downloader\Ext\Youtube\Exception\YoutubeDownloaderException |
||
54 | */ |
||
55 | public function getURL() : string |
||
56 | { |
||
57 | $formatVideos = $this->filterByFormats($this->getDownloadLinks()); |
||
58 | |||
59 | return array_values($formatVideos)[0]; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return array |
||
64 | * @throws \Jackal\Downloader\Exception\DownloadException |
||
65 | * @throws \Jackal\Downloader\Ext\Youtube\Exception\YoutubeDownloaderException |
||
66 | */ |
||
67 | public function getFormatsAvailable(): array |
||
68 | { |
||
69 | $formatVideos = $this->filterByFormats($this->getDownloadLinks()); |
||
70 | |||
71 | return array_keys($formatVideos); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | public static function getPublicUrlRegex(): string |
||
78 | { |
||
79 | return '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/i'; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | public static function getType(): string |
||
86 | { |
||
87 | return 'youtube'; |
||
88 | } |
||
89 | } |
||
90 |