Ejemplo n.º 1
0
 /**
  * @Route ("/apiway", name="apiway")
  */
 public function apiAction()
 {
     $em = $this->getDoctrine()->getManager();
     //clear table first
     $sql = "DELETE FROM video";
     $stmt = $this->getDoctrine()->getManager()->getConnection()->prepare($sql);
     $stmt->execute();
     #get artists list from yaml file as Symfony good form
     $artists = Yaml::parse(file_get_contents("artists.yml"));
     #used of Google Client is unnecessary because not used OAuth2, just only query with key
     $developer_key = $this->container->getParameter("google_api_key");
     $client = new Google_Client();
     $client->setDeveloperKey($developer_key);
     $youtube = new Google_Service_YouTube($client);
     foreach ($artists as $artist) {
         $search_response = $youtube->search->listSearch("id", ['q' => $artist, 'maxResults' => 5, 'type' => 'video']);
         #merge video IDs for getting statistic data by artists video
         $videoResults = [];
         foreach ($search_response['items'] as $searchResult) {
             array_push($videoResults, $searchResult['id']['videoId']);
         }
         $videoIds = join(',', $videoResults);
         $videosResponse = $youtube->videos->listVideos('snippet, statistics', array('id' => $videoIds));
         foreach ($videosResponse['items'] as $videoResult) {
             $video = new Video();
             $video->setAuthor($artist);
             $video->setLink($videoResult['id']);
             $video->setName($videoResult['snippet']['title']);
             $video->setViews($videoResult['statistics']['viewCount']);
             $em->persist($video);
         }
         $em->flush();
     }
     return $this->redirect($this->generateUrl("homepage"));
 }