Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

classTMDBAPIUtils: # Do not modify def __init__(self, api_key:str): self.api_key=api_key def get_movie_cast( self, movie_id:str , limit:int=None, exclude_ids:list=None ) -> list: Get the movie cast for

classTMDBAPIUtils:

# Do not modify

def __init__(self, api_key:str):

self.api_key=api_key

def get_movie_cast( self, movie_id:str , limit:int=None, exclude_ids:list=None ) -> list:

"""

Get the movie cast for a given movie id, with optional parameters to exclude an cast member

from being returned and/or to limit the number of returned cast members

documentation url: https://developers.themoviedb.org/3/movies/get-movie-credits

:param integer movie_id: a movie_id

:param integer limit: number of returned cast members by their 'order' attribute

e.g., limit=5 will attempt to return the 5 cast members having 'order' attribute values between 0-4

If there are fewer cast members than the specified limit or the limit not specified, return all cast members

:param list exclude_ids: a list of ints containing ids (not cast_ids) of cast membersthat should be excluded from the returned result

e.g., if exclude_ids are [353, 455] then exclude these from any result.

:rtype: list

return a list of dicts, one dict per cast member with the following structure:

[{'cast_id': '97909' # the id of the cast member

'character': 'John Doe' # the name of the character played

'credit_id': '52fe4249c3a36847f8012927' # id of the credit}, ... ]

Important: the exclude_ids processing should occur prior to limiting output.

"""

conn = http.client.HTTPSConnection("api.themoviedb.org")

conn.request("GET", "/3/movie/100/credits?api_key=a6d70e67f1fe7e409edc31e7a7636c37".format(movie_id, self.api_key))

r1 = conn.getresponse()

#print(r1.status, r1.reason)

if r1.status == 200:

movie_data = r1.read()

movie_data_list = json.loads(movie_data.decode('utf-8'))

movie_Cast= movie_data_list['cast']

if exclude_ids and limit :

#exlude ids first

exclude_ids_ = exclude_ids

mc_ID_filter = [d for d in movie_Cast if d['id'] not inexclude_ids_ ]

#select only the records where order is smaller than the limit

mc_order_filter = [d for d in mc_ID_filter if d['order'] < limit ]

#get only certain string from the filtered dict in the list

for dictatin mc_order_filter:

movie_Cast_shrinked = {key_i: dictat[key_i] for key_i in dictat.keys()

& {'cast_id', 'character','credit_id'}}

movie_Cast_list.append(movie_Cast_shrinked)

return movie_Cast_list

elif limit is None and exclude_ids is None:

for dictatin movie_Cast:

movie_Cast_shrinked = {key_i: dictat[key_i] for key_i in dictat.keys()

& {'cast_id', 'character','credit_id'}}

movie_Cast_list.append(movie_Cast_shrinked)

return movie_Cast_list

elif limit is None :

#exlude ids first

exclude_ids_ = exclude_ids

mc_ID_filter = [d for d in movie_Cast if d['id'] not inexclude_ids_ ]

for dictatin mc_ID_filter:

movie_Cast_shrinked = {key_i: dictat[key_i] for key_i in dictat.keys()

& {'cast_id', 'character','credit_id'}}

movie_Cast_list.append(movie_Cast_shrinked)

return movie_Cast_list

elif exclude_ids is None :

#select only the records where order is smaller than the limit

mc_order_filter = [d for d in movie_Cast if d['order'] < limit ]

for dictatin mc_order_filter:

movie_Cast_shrinked = {key_i: dictat[key_i] for key_i in dictat.keys()

& {'cast_id', 'character','credit_id'}}

movie_Cast_list.append(movie_Cast_shrinked)

return movie_Cast_list

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

this question as soon as possible thanks

Answered: 1 week ago