Skip to content

Commit c5d634c

Browse files
committed
Fix MPA Rating
1 parent e73d43c commit c5d634c

File tree

4 files changed

+152
-19
lines changed

4 files changed

+152
-19
lines changed

mu-plugins/10up-plugin/requirements.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Create a WP CLI script that can take a list of IMDB IDs and import movie and per
3939
## API Endpoints
4040
- **Movies**: `https://api.imdbapi.dev/titles/{imdb_id}`
4141
- **People**: `https://api.imdbapi.dev/names/{imdb_id}`
42+
- **Movie Certificates**: `https://api.imdbapi.dev/titles/{imdb_id}/certificates`
4243

4344
## Movie Import Requirements
4445

@@ -59,6 +60,7 @@ Based on existing post meta fields in the 10up-plugin, map the following API res
5960
| `plot` | `tenup_movie_plot` | Movie plot | string |
6061
| `rating.aggregateRating` | `tenup_movie_viewer_rating` | Viewer rating | string |
6162
| `rating.voteCount` | `tenup_movie_viewer_rating_count` | Number of votes | string |
63+
| US Certificate Rating | `tenup_movie_mpa_rating` | MPA rating from US certificate | string |
6264

6365
### Runtime Conversion
6466
Convert `runtimeSeconds` to the expected object format:
@@ -69,6 +71,41 @@ Convert `runtimeSeconds` to the expected object format:
6971
]
7072
```
7173

74+
### MPA Rating Retrieval
75+
For each movie import, make an additional API call to retrieve MPA rating from certificates:
76+
77+
**API Endpoint**: `https://api.imdbapi.dev/titles/{imdb_id}/certificates`
78+
79+
**Process**:
80+
1. Make API call to certificates endpoint using the movie's IMDB ID
81+
2. Parse the `certificates` array in the response
82+
3. Find the certificate where:
83+
- `country.code` equals `"US"`
84+
- `attributes` array contains a string that includes `"certificate #"` or `"certificate#"`
85+
4. Extract the `rating` value from the matching certificate
86+
5. Store the rating in the `tenup_movie_mpa_rating` meta field
87+
88+
**Example**:
89+
Based on the API response from [https://api.imdbapi.dev/titles/tt2380307/certificates](https://api.imdbapi.dev/titles/tt2380307/certificates), the script should find:
90+
```json
91+
{
92+
"rating": "PG",
93+
"country": {
94+
"code": "US",
95+
"name": "United States"
96+
},
97+
"attributes": [
98+
"certificate #51192"
99+
]
100+
}
101+
```
102+
And store `"PG"` as the MPA rating.
103+
104+
**Error Handling**:
105+
- If no US certificate with "certificate #" or "certificate#" attribute is found, set `tenup_movie_mpa_rating` to "Not Rated"
106+
- If the certificates API call fails, log the error but continue with the movie import
107+
- Handle cases where the certificates array is empty or malformed
108+
72109
### Genre Taxonomy
73110
- **Taxonomy**: `tenup-genre`
74111
- **Source**: `genres` array from API response
@@ -228,8 +265,9 @@ wp imdb-import both --file=imdb_ids.txt
228265
When importing a movie, the script should:
229266

230267
1. **Fetch Movie Data**: Get movie data from IMDB API
231-
2. **Create Movie Post**: Create the movie post with all meta fields
232-
3. **Process Stars Array**: For each star in the `stars` array:
268+
2. **Fetch MPA Rating**: Get MPA rating from certificates API
269+
3. **Create Movie Post**: Create the movie post with all meta fields including MPA rating
270+
4. **Process Stars Array**: For each star in the `stars` array:
233271
- Check if person already exists (by `tenup_person_imdb_id`)
234272
- If exists:
235273
- Update person post with latest data from People API
@@ -251,15 +289,16 @@ When importing a movie, the script should:
251289
- Handle duplicate filenames by appending numbers
252290

253291
### API Rate Limiting
254-
- Respect rate limits between movie and people API calls
292+
- Respect rate limits between movie, certificates, and people API calls
255293
- Cache people API responses to avoid duplicate calls
294+
- Cache certificates API responses to avoid duplicate calls
256295
- Batch process stars to minimize API requests
296+
- Implement delays between API calls to respect rate limits
257297

258298
## Additional Notes
259299

260300
### Fields Not Available in API
261301
The following existing post meta fields do not have equivalents in the IMDB API responses and will not be populated:
262-
- `tenup_movie_mpa_rating` (MPA rating not available)
263302
- `tenup_movie_summary` (summary not available)
264303
- `tenup_movie_synopsis` (synopsis not available)
265304
- `tenup_movie_tagline` (tagline not available)

mu-plugins/10up-plugin/src/CLI/Importers/MovieImporter.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ private function create_movie( $data, $options ) {
189189
// Set meta fields.
190190
$this->set_movie_meta( $post_id, $data );
191191

192+
// Fetch and set MPA rating from certificates API.
193+
$this->set_mpa_rating( $post_id, $data['id'] );
194+
192195
// Set featured image.
193196
$this->set_featured_image( $post_id, $data );
194197

@@ -232,6 +235,9 @@ private function update_movie( $post_id, $data, $options ) {
232235
// Update meta fields.
233236
$this->set_movie_meta( $post_id, $data );
234237

238+
// Fetch and update MPA rating from certificates API.
239+
$this->set_mpa_rating( $post_id, $data['id'] );
240+
235241
// Update featured image.
236242
$this->set_featured_image( $post_id, $data );
237243

@@ -289,6 +295,33 @@ private function set_movie_meta( $post_id, $data ) {
289295
}
290296
}
291297

298+
/**
299+
* Set MPA rating from certificates API.
300+
*
301+
* @param int $post_id Post ID.
302+
* @param string $imdb_id IMDB movie ID.
303+
*/
304+
private function set_mpa_rating( $post_id, $imdb_id ) {
305+
// Fetch certificates data.
306+
$certificates_data = $this->api_client->get_movie_certificates( $imdb_id );
307+
308+
if ( is_wp_error( $certificates_data ) ) {
309+
WP_CLI::warning( sprintf( 'Failed to fetch certificates for %s: %s', $imdb_id, $certificates_data->get_error_message() ) );
310+
return;
311+
}
312+
313+
// Extract US MPA rating.
314+
$mpa_rating = $this->api_client->extract_us_mpa_rating( $certificates_data );
315+
316+
if ( $mpa_rating ) {
317+
update_post_meta( $post_id, 'tenup_movie_mpa_rating', $mpa_rating );
318+
WP_CLI::log( sprintf( 'Set MPA rating for %s: %s', $imdb_id, $mpa_rating ) );
319+
} else {
320+
update_post_meta( $post_id, 'tenup_movie_mpa_rating', 'Not Rated' );
321+
WP_CLI::log( sprintf( 'No US MPA rating found for %s, set to: Not Rated', $imdb_id ) );
322+
}
323+
}
324+
292325
/**
293326
* Set featured image from API data.
294327
*

mu-plugins/10up-plugin/src/CLI/Utils/IMDBApiClient.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class IMDBApiClient {
2828
*/
2929
private const PERSON_API_URL = 'https://api.imdbapi.dev/names/';
3030

31+
/**
32+
* Base API URL for movie certificates.
33+
*
34+
* @var string
35+
*/
36+
private const CERTIFICATES_API_URL = 'https://api.imdbapi.dev/titles/';
37+
3138
/**
3239
* Rate limiting delay between requests (in seconds).
3340
*
@@ -115,6 +122,66 @@ public function get_person_data( $imdb_id ) {
115122
return $data;
116123
}
117124

125+
/**
126+
* Get movie certificates data from IMDB API.
127+
*
128+
* @param string $imdb_id IMDB movie ID.
129+
* @return array|WP_Error Certificates data or error.
130+
*/
131+
public function get_movie_certificates( $imdb_id ) {
132+
$cache_key = 'imdb_certificates_' . $imdb_id;
133+
$cached_data = get_transient( $cache_key );
134+
135+
if ( false !== $cached_data ) {
136+
return $cached_data;
137+
}
138+
139+
$url = self::CERTIFICATES_API_URL . $imdb_id . '/certificates';
140+
$response = $this->make_request( $url );
141+
142+
if ( is_wp_error( $response ) ) {
143+
return $response;
144+
}
145+
146+
$data = json_decode( $response, true );
147+
148+
if ( ! $data || isset( $data['error'] ) ) {
149+
return new WP_Error( 'api_error', 'Failed to fetch certificates data: ' . ( $data['error'] ?? 'Unknown error' ) );
150+
}
151+
152+
// Cache the successful response.
153+
set_transient( $cache_key, $data, self::CACHE_DURATION );
154+
155+
return $data;
156+
}
157+
158+
/**
159+
* Extract US MPA rating from certificates data.
160+
*
161+
* @param array $certificates_data Certificates API response data.
162+
* @return string|null MPA rating or null if not found.
163+
*/
164+
public function extract_us_mpa_rating( $certificates_data ) {
165+
if ( ! isset( $certificates_data['certificates'] ) || ! is_array( $certificates_data['certificates'] ) ) {
166+
return null;
167+
}
168+
169+
foreach ( $certificates_data['certificates'] as $certificate ) {
170+
// Check if this is a US certificate with "certificate #" or "certificate#" attribute.
171+
if ( isset( $certificate['country']['code'] ) && 'US' === $certificate['country']['code'] ) {
172+
if ( isset( $certificate['attributes'] ) && is_array( $certificate['attributes'] ) ) {
173+
foreach ( $certificate['attributes'] as $attribute ) {
174+
if ( is_string( $attribute ) && ( strpos( $attribute, 'certificate #' ) !== false || strpos( $attribute, 'certificate#' ) !== false ) ) {
175+
return $certificate['rating'] ?? null;
176+
}
177+
}
178+
}
179+
}
180+
}
181+
182+
return null;
183+
}
184+
118185
/**
119186
* Make HTTP request with rate limiting and retry logic.
120187
*

mu-plugins/10up-plugin/src/PostMeta/MovieMPARating.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,6 @@ public function get_description(): string {
4444
*/
4545
protected $has_key_value_options = true;
4646

47-
/**
48-
* Get allowed values for field schema.
49-
*/
50-
public function allowed_values(): array {
51-
return [
52-
'Unrated' => 'Unrated',
53-
'G' => 'G',
54-
'PG' => 'PG',
55-
'PG-13' => 'PG-13',
56-
'R' => 'R',
57-
'NC-17' => 'NC-17',
58-
];
59-
}
60-
6147
/**
6248
* Get the post types.
6349
*
@@ -89,7 +75,15 @@ public function add_localized_script() {
8975
'tenup_plugin_admin',
9076
'TenupMovieMPARating',
9177
array(
92-
'options' => $this->allowed_values(),
78+
'options' => [
79+
'Not Rated' => 'Not Rated',
80+
'Approved' => 'Approved',
81+
'G' => 'G',
82+
'PG' => 'PG',
83+
'PG-13' => 'PG-13',
84+
'R' => 'R',
85+
'NC-17' => 'NC-17',
86+
],
9387
)
9488
);
9589
}

0 commit comments

Comments
 (0)