Handling for Tv Shows (tmdb_api part)

This commit is contained in:
lomion 2020-06-24 10:45:18 +02:00
parent 4d2f386b4f
commit 10955cc96f
2 changed files with 69 additions and 13 deletions

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import unittest import unittest
from tmdb.tmdb_api import Movie from tmdb.tmdb_api import Movie, TvShow
from tmdb.tmdb import TmdbBot from tmdb.tmdb import TmdbBot
from tmdb.database import Database from tmdb.database import Database
from sqlalchemy import create_engine from sqlalchemy import create_engine
@ -54,5 +54,33 @@ class TestTmdbMethods(unittest.TestCase):
db.set_language('@testuser:example.com', 'en') db.set_language('@testuser:example.com', 'en')
self.assertEqual(str(db.get_language('@testuser:example.com')), 'en') self.assertEqual(str(db.get_language('@testuser:example.com')), 'en')
def test_id_lookup(self):
movie = Movie()
movie.query_details('2108')
self.assertEqual('The Breakfast Club', movie.title)
def test_search_fails(self):
movie = Movie()
id = movie.search_title('Breakfast Club 2019')
self.assertEqual(id, None)
self.assertEqual(None, movie.title)
# TV Shows
def test_search_tvshow(self):
movie = TvShow()
id = movie.search_title('The Flash')
self.assertEqual(id, 60735)
def test_tv_title(self):
movie = TvShow()
movie.search_title('The Flash')
self.assertEqual('The Flash', movie.title)
def test_cast(self):
movie = TvShow()
movie.search_title('The Flash')
self.assertEqual('Grant Gustin', movie.cast[0])
self.assertEqual('Carlos Valdes', movie.cast[2])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -38,12 +38,11 @@ class TmdbApi():
def get_apikey(self): def get_apikey(self):
return { 'api_key' : self.api_key } return { 'api_key' : self.api_key }
def request(self, request_uri, payload : dict = {}): def request(self, request_uri, params : dict = {}):
url = self.base_url + request_uri url = self.base_url + request_uri.lstrip('/')
_payload = self.get_apikey() params.update(self.get_apikey())
_payload['language'] = self.language params.update({ 'language' : self.language })
_payload.update(payload) result = requests.get(url, params=params)
result = requests.get(url, params=_payload)
self.valid = True self.valid = True
return result return result
@ -60,15 +59,11 @@ class TmdbApi():
class Movie(TmdbApi): class Movie(TmdbApi):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.load_parameters()
pass
def search_title(self, title): def search_title(self, title):
url = self.base_url+ 'search/movie' payload = {}
payload = self.get_apikey()
payload['language'] = self.language
payload['query'] = title payload['query'] = title
result = requests.get(url, params=payload) result = self.request('search/movie', params=payload)
json = result.json() json = result.json()
if json['total_results'] > 0: if json['total_results'] > 0:
movie_id = json['results'][0]['id'] movie_id = json['results'][0]['id']
@ -94,3 +89,36 @@ class Movie(TmdbApi):
def get_cast(self, amount): def get_cast(self, amount):
return self.cast[:amount] return self.cast[:amount]
class TvShow(TmdbApi):
def __init__(self):
super().__init__()
def search_title(self, title):
payload = {}
payload['query'] = title
result = self.request('/search/tv', 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('tv/' + str(id)).json()
self.title = data['name']
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/tv/' + str(self.id)
self.vote_average = str(data['vote_average'])
self.query_cast()
def query_cast(self):
data = self.request('tv/'+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]