Exemplo n.º 1
0
 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...");
 }