public function run() { $this->output->writeln("Downloading video: " . $this->video->getId()); $this->logger->addInfo("START Downloading video: " . $this->video->getId()); if ($this->video->getDownloaded()) { $this->output->writeln("<info>Video already downloaded... Continue...</info>"); $this->logger->addInfo("END Skipping video: " . $this->video->getId() . " -> Already exists!"); return; } $builder = new ProcessBuilder(array('youtube-dl', '-x', '--audio-format', 'mp3', '--output', $this->webDir . "/media/" . "%(id)s.%(ext)s", '--cache-dir', $this->appDir . "/ytdl_cache/", 'https://youtube.com/watch?v=' . $this->video->getId())); $process = $builder->getProcess(); $process->setTimeout(0); $logger = $this->logger; $output = $this->output; $process->run(function ($type, $buffer) use($logger, $output) { if (Process::ERR === $type) { $output->writeln("<error>{$buffer}</error>"); $logger->addError("YOUTUBE-DL: " . $buffer); } else { $logger->addDebug("YOUTUBE-DL: " . $buffer); } }); $this->logger->addDebug("Video " . $this->video->getId() . ": Exitcode " . $process->getExitCode()); if ($process->getExitCode() === 0) { $this->video->setDownloaded(true); $output->writeln("<info>Video successfully downloaded!</info>"); $this->logger->addInfo("Video " . $this->video->getId() . ": Downloaded successfull"); } elseif ($process->getExitCode() === 127) { $this->video->setDownloaded(false); $output->writeln("<error>youtube-dl isn't installed!</error>"); $this->logger->addError("youtube-dl isn't available!"); } else { $this->video->setDownloaded(false); $output->writeln("<error>Video downloading error! See logs.</error>"); $this->logger->addError("Video " . $this->video->getId() . ": Unknown error!"); } $this->videoWrapper->saveVideo($this->video); }
protected function execute(InputInterface $input, OutputInterface $output) { // Direct access to the Container. /** @var Kengrabber $kg */ $kg = $this->getApplication()->getContainer(); $output->writeln("Starting with fetching the videos from channel: " . $kg['config']['youtube_channel_username']); $yt = new Youtube(array('key' => $kg['config']['youtube_api_key'])); $channel = $yt->getChannelByName($kg['config']['youtube_channel_username']); // Setting channel data... $kg['option']->setOption('channel.title', $channel->snippet->title); $kg['option']->setOption('channel.description', $channel->snippet->description); $params = array('type' => 'video', 'channelId' => $channel->id, 'maxResults' => 15, 'order' => 'date', 'safeSearch' => 'none', 'part' => 'id, snippet'); foreach ($kg['config']['youtube_queries'] as $query) { $params['q'] = $query; do { $search = $yt->searchAdvanced($params, true); if (is_array($search['results'])) { foreach ($search['results'] as $v) { $vid = $yt->getVideoInfo($v->id->videoId); /** @var Video $video */ $video = $kg['video']->getVideoById($vid->id); if ($video === null) { $video = new Video(); $video->setId($vid->id); } $video->setTitle($vid->snippet->title); $video->setDescription($vid->snippet->description); $video->setPublished(new \DateTime($vid->snippet->publishedAt)); $kg['video']->saveVideo($video); } } if (isset($search['info']['nextPageToken'])) { $params['pageToken'] = $search['info']['nextPageToken']; $output->writeln("Go to next page..."); } else { break; } } while (true); } $output->writeln("Finished crawling..."); }
protected function resultToVideo($res) { $video = new Video(); $video->setId($res['id']); $video->setTitle($res['title']); $video->setDescription($res['description']); $video->setPublished(new \DateTime($res['published'])); $video->setDownloaded((bool) $res['downloaded']); return $video; }