2020-06-22 12:15:28 +01:00
|
|
|
import requests
|
|
|
|
|
|
|
|
class Connection():
|
|
|
|
def __init__(self):
|
|
|
|
self.load_parameters()
|
|
|
|
|
|
|
|
def load_parameters(self):
|
|
|
|
self.api_key = '51d75c00dc1502dc894b7773ec3e7a15'
|
|
|
|
|
|
|
|
self.base_url = "https://api.themoviedb.org/3/"
|
|
|
|
result = requests.get(self.base_url + 'configuration', params = self.get_apikey()).json()
|
|
|
|
self.base_url_poster = result['images']['base_url'] + result['images']['poster_sizes'][0]
|
|
|
|
self.valid = False
|
2020-06-22 13:41:31 +01:00
|
|
|
|
|
|
|
self.language = 'de'
|
2020-06-22 12:15:28 +01:00
|
|
|
|
|
|
|
def get_apikey(self):
|
|
|
|
return { 'api_key' : self.api_key }
|
|
|
|
|
|
|
|
def request(self, request_uri):
|
|
|
|
url = self.base_url + request_uri
|
|
|
|
payload = self.get_apikey()
|
2020-06-22 13:41:31 +01:00
|
|
|
payload['language'] = self.language
|
2020-06-22 12:15:28 +01:00
|
|
|
result = requests.get(url, params=payload)
|
|
|
|
self.valid = True
|
|
|
|
return result
|
|
|
|
|
2020-06-22 13:41:31 +01:00
|
|
|
def set_language(self, language):
|
|
|
|
self.language = language
|
|
|
|
|
2020-06-22 12:15:28 +01:00
|
|
|
def get_image_binary(self):
|
|
|
|
return requests.get(self.poster_url).content
|
|
|
|
|
|
|
|
|
|
|
|
class Movie(Connection):
|
|
|
|
def __init__(self):
|
|
|
|
self.load_parameters()
|
|
|
|
pass
|
|
|
|
|
|
|
|
def search_title(self, title):
|
|
|
|
url = self.base_url+ 'search/movie'
|
|
|
|
payload = self.get_apikey()
|
2020-06-22 13:41:31 +01:00
|
|
|
payload['language'] = self.language
|
2020-06-22 12:15:28 +01:00
|
|
|
payload['query'] = title
|
|
|
|
result = requests.get(url, params=payload)
|
|
|
|
json = result.json()
|
|
|
|
if json['total_results'] > 0:
|
|
|
|
movie_id = json['results'][0]['id']
|
|
|
|
self.query_details(movie_id)
|
|
|
|
return movie_id
|
|
|
|
|
|
|
|
def query_details(self, id):
|
|
|
|
data = self.request('movie/' + str(id)).json()
|
|
|
|
self.title = data['title']
|
|
|
|
self.id = data['id']
|
|
|
|
self.poster_url = self.base_url_poster + data['poster_path']
|
|
|
|
self.overview = data['overview']
|
|
|
|
self.web_url = 'https://www.themoviedb.org/movie/' + str(self.id)
|
|
|
|
self.vote_average = str(data['vote_average'])
|
|
|
|
self.query_cast()
|
|
|
|
|
|
|
|
def query_cast(self):
|
|
|
|
data = self.request('movie/'+str(self.id)+'/credits').json()
|
|
|
|
self.cast = []
|
|
|
|
for actor in data['cast']:
|
|
|
|
self.cast.append(actor['name'])
|
|
|
|
|
|
|
|
def get_cast(self, amount):
|
|
|
|
return self.cast[:amount]
|
|
|
|
|
|
|
|
movie = Movie()
|
|
|
|
#movie.query_details('550')
|
|
|
|
movie.search_title('Jack Reacher')
|
|
|
|
print(movie.title)
|
|
|
|
print(movie.overview[:150])
|
|
|
|
print(movie.web_url)
|
|
|
|
print(movie.poster_url)
|