Skip to content

Commit bbde762

Browse files
authored
Merge pull request #53 from inpercima/copilot/enhance-extract-method-with-cast-info
Enhance list items with cast, original title, and release date
2 parents 4a6b4c7 + f3d18df commit bbde762

4 files changed

Lines changed: 54 additions & 9 deletions

File tree

api/rest/list.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
$language = $coreService->getParam('language');
1111

1212
$listService = new ListService();
13-
echo $listService->listAll($listId, $language);
13+
echo $listService->getListDetails($listId, $language);
1414
?>

api/service/list.service.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
?>

frontend/src/app/features/dashboard/dashboard.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
<mat-list-item lines="70">
5353
<mat-icon aria-hidden="false" matListItemIcon>movie</mat-icon>
5454
<span matListItemTitle>{{ item.title }}</span>
55+
<span aria-label="Original title">{{ item.original_title }}</span>
56+
<span aria-label="Release date">{{ item.release_date }}</span>
57+
<span aria-label="Cast">{{ item.cast.join(', ') }}</span>
5558
<span [innerHTML]="item.comment"></span>
5659
</mat-list-item>
5760
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export interface Item {
22
title: string;
3+
original_title: string;
4+
release_date: string;
5+
cast: string[];
36
comment: string;
47
}

0 commit comments

Comments
 (0)