@@ -8,25 +8,34 @@ class ListService {
88 public function __construct () {}
99
1010 /**
11- * List all title from a public list in themoviedb.
11+ * List all items from a public list from themoviedb with their details .
1212 */
13- public function listAll ($ listId , $ language ) {
13+ public function getListDetails ($ listId , $ language ) {
1414 $ page = 1 ;
15- $ data = $ this ->request ($ listId , $ language , $ page );
15+ $ data = $ this ->getList ($ listId , $ language , $ page );
1616 $ items = $ this ->extract ($ data );
1717 $ totalPages = $ data ->total_pages ;
1818 for ($ i = $ page + 1 ; $ i <= $ totalPages ; $ i ++) {
19- $ items = array_merge ($ items , $ this ->extract ($ this ->request ($ listId , $ language , $ i )));
19+ $ items = array_merge ($ items , $ this ->extract ($ this ->getList ($ listId , $ language , $ i )));
2020 }
2121 return json_encode (array ("name " => $ data ->name , "description " => $ data ->description , "items " => $ items ));
2222 }
2323
2424 /**
25- * Executes one or multiple requests to get all items from a public list in themoviedb .
25+ * Fetches a public list from themoviedb with the given list ID, language and page number .
2626 */
27- private function request ($ listId , $ language , $ page ) {
27+ private function getList ($ listId , $ language , $ page ) {
2828 $ apiKey = Config::API_KEY ;
29- return json_decode (file_get_contents ("https://api.themoviedb.org/4/list/ $ listId?api_key= $ apiKey&language= $ language&page= $ page " ));
29+ $ url = "https://api.themoviedb.org/3/list/ $ listId?api_key= $ apiKey&language= $ language&page= $ page " ;
30+ $ response = @file_get_contents ($ url );
31+ if ($ response === false ) {
32+ return (object ) [];
33+ }
34+ $ data = json_decode ($ response );
35+ if ($ data === null || !isset ($ data ->items )) {
36+ return (object ) [];
37+ }
38+ return $ data ;
3039 }
3140
3241 /**
@@ -37,12 +46,42 @@ private function extract($data) {
3746 foreach ($ data ->results as $ key => $ value ) {
3847 $ mediaType = $ value ->media_type ;
3948 $ title = $ mediaType == 'tv ' ? $ value ->name : $ value ->title ;
49+ $ originalTitle = $ mediaType == 'tv ' ? $ value ->original_name : $ value ->original_title ;
50+ $ releaseDate = $ mediaType == 'tv ' ? $ value ->first_air_date : $ value ->release_date ;
4051 $ commentId = "$ mediaType: $ value ->id " ;
4152 $ comment = $ data ->comments ->$ commentId ;
4253 $ comment = $ comment == null ? '' : $ comment ;
43- array_push ($ items , (object ) [ 'title ' => $ title , 'comment ' => nl2br ($ comment ) ]);
54+ $ cast = $ this ->getCast ($ value ->id , $ mediaType );
55+ array_push ($ items , (object ) [
56+ 'title ' => $ title ,
57+ 'original_title ' => $ originalTitle ,
58+ 'release_date ' => $ releaseDate ,
59+ 'cast ' => $ cast ,
60+ 'comment ' => nl2br ($ comment )
61+ ]);
4462 }
4563 return $ items ;
4664 }
65+
66+ /**
67+ * Fetches the top 5 main actors for a movie or TV show from the TMDB v3 credits endpoint.
68+ */
69+ private function getCast ($ movieId , $ mediaType ) {
70+ $ apiKey = Config::API_KEY ;
71+ $ endpoint = $ mediaType == 'tv ' ? 'tv ' : 'movie ' ;
72+ $ url = "https://api.themoviedb.org/3/ $ endpoint/ $ movieId/credits?api_key= $ apiKey " ;
73+ $ response = @file_get_contents ($ url );
74+ if ($ response === false ) {
75+ return [];
76+ }
77+ $ credits = json_decode ($ response );
78+ if ($ credits === null || !isset ($ credits ->cast )) {
79+ return [];
80+ }
81+ $ mainActors = array_slice ($ credits ->cast , 0 , 5 );
82+ return array_map (function ($ actor ) {
83+ return $ actor ->name ;
84+ }, $ mainActors );
85+ }
4786}
4887?>
0 commit comments