From 53d14f153c37613a14ac8af3cb1f09de74915aa5 Mon Sep 17 00:00:00 2001 From: lomion Date: Tue, 16 Mar 2021 17:56:02 +0100 Subject: [PATCH] Add "Most Popular" - Part 1 --- test_tmdb.py | 15 ++++++++++++++- tmdb/tmdb_api.py | 50 +++++++++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/test_tmdb.py b/test_tmdb.py index 0111ede..a72fb89 100644 --- a/test_tmdb.py +++ b/test_tmdb.py @@ -1,10 +1,19 @@ #!/usr/bin/env python3 import unittest -from tmdb.tmdb_api import Movie, TvShow +import requests +from tmdb.tmdb_api import Movie, TvShow, MoviePopular from tmdb.tmdb import TmdbBot, MessageConstructor from tmdb.database import Database from sqlalchemy import create_engine +def apiRequests(command): + api_key = '51d75c00dc1502dc894b7773ec3e7a15' + base_url = "https://api.themoviedb.org/3/" + url = base_url + command.lstrip('/') + params={'api_key': api_key} + params.update({'language': 'en'}) + return requests.get(url, params=params).json() + class TestTmdbMethods(unittest.TestCase): # TMDB API @@ -111,6 +120,10 @@ class TestTmdbMethods(unittest.TestCase): movie.search_title('Dune') self.assertEqual(movie.poster_url, "http://image.tmdb.org/t/p/w92/9HNZTw2D3cM1yA08FF5SeWEO9eX.jpg") + def test_movie_popular_length(self): + results = apiRequests('/movie/popular') + list = MoviePopular() + self.assertEqual(list.query(), results['total_results']) if __name__ == '__main__': unittest.main() diff --git a/tmdb/tmdb_api.py b/tmdb/tmdb_api.py index cbe1c83..6197ac5 100644 --- a/tmdb/tmdb_api.py +++ b/tmdb/tmdb_api.py @@ -19,14 +19,7 @@ import requests class TmdbApi(): def __init__(self): self.load_parameters() - self.title = None - self.id = None - self.poster_url = None - self.overview = None - self.web_url = None - self.vote_average = None self.language = 'en' - self.valid = False def load_parameters(self): @@ -46,11 +39,22 @@ class TmdbApi(): params.update({'language': self.language}) result = requests.get(url, params=params) self.valid = True - return result + return result.json() def set_language(self, language): self.language = language + +class TmdbApiSingle(TmdbApi): + def __init__(self): + super().__init__() + self.title = None + self.id = None + self.poster_url = None + self.overview = None + self.web_url = None + self.vote_average = None + def get_image_binary(self): if self.poster_url: return requests.get(self.poster_url).content @@ -64,8 +68,20 @@ class TmdbApi(): return x return None +class MoviePopular(TmdbApi): + def __init__(self): + super().__init__() + self.list = [] + self.length = 0 -class Movie(TmdbApi): + def query(self) -> int: + result = self.request('/movie/popular') + self.length = result['total_results'] + self.list = result['results'] + return self.length + + +class Movie(TmdbApiSingle): def __init__(self): super().__init__() @@ -74,15 +90,14 @@ class Movie(TmdbApi): payload['query'] = title if year: payload['year'] = year - result = self.request('search/movie', params=payload) - json = result.json() + json = self.request('search/movie', params=payload) 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() + data = self.request('movie/' + str(id)) self.title = data['title'] self.id = data['id'] self.poster_url = self.base_url_poster + data['poster_path'] @@ -92,7 +107,7 @@ class Movie(TmdbApi): self.query_cast() def query_cast(self): - data = self.request('movie/' + str(self.id) + '/credits').json() + data = self.request('movie/' + str(self.id) + '/credits') self.cast = [] for actor in data['cast']: self.cast.append(actor['name']) @@ -101,22 +116,21 @@ class Movie(TmdbApi): return self.cast[:amount] -class TvShow(TmdbApi): +class TvShow(TmdbApiSingle): def __init__(self): super().__init__() def search_title(self, title): payload = {} payload['query'] = title - result = self.request('/search/tv', params=payload) - json = result.json() + json = self.request('/search/tv', params=payload) 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('tv/' + str(id)).json() + data = self.request('tv/' + str(id)) self.title = data['name'] self.id = data['id'] self.poster_url = self.base_url_poster + data['poster_path'] @@ -126,7 +140,7 @@ class TvShow(TmdbApi): self.query_cast() def query_cast(self): - data = self.request('tv/' + str(self.id) + '/credits').json() + data = self.request('tv/' + str(self.id) + '/credits') self.cast = [] for actor in data['cast']: self.cast.append(actor['name'])