Add "Most Popular" - Part 1

This commit is contained in:
lomion 2021-03-16 17:56:02 +01:00
parent bb8b6f63f1
commit 53d14f153c
2 changed files with 46 additions and 19 deletions

View file

@ -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()

View file

@ -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'])