First tests. Gathering 2 requests takes longer than serial handling
This commit is contained in:
parent
74ac2415cc
commit
460cb51d86
16
test_tmdb.py
16
test_tmdb.py
|
@ -6,7 +6,7 @@ from tmdb.tmdb_api import Movie, TvShow, MoviePopular
|
|||
from tmdb.tmdb import TmdbBot, MessageConstructor
|
||||
from tmdb.database import Database
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
import time
|
||||
|
||||
def apiRequests(command):
|
||||
api_key = '51d75c00dc1502dc894b7773ec3e7a15'
|
||||
|
@ -17,13 +17,17 @@ def apiRequests(command):
|
|||
return requests.get(url, params=params).json()
|
||||
|
||||
|
||||
class TestTmdbMethods(unittest.TestCase):
|
||||
class TestTmdbMethods(unittest.IsolatedAsyncioTestCase):
|
||||
# TMDB API
|
||||
def test_search_item(self):
|
||||
async def test_search_item(self):
|
||||
start = time.time()
|
||||
movie = Movie()
|
||||
id = movie.search_title('Breakfast Club')
|
||||
await movie.load_parameters()
|
||||
id = await movie.search_title('Breakfast Club')
|
||||
self.assertEqual(id, 2108)
|
||||
|
||||
print(time.time() - start)
|
||||
await movie.close_session()
|
||||
'''
|
||||
def test_cast(self):
|
||||
movie = Movie()
|
||||
movie.search_title('Breakfast Club')
|
||||
|
@ -148,7 +152,7 @@ class TestTmdbMethods(unittest.TestCase):
|
|||
test_result = results['results'][-1]['title']
|
||||
tested = list.getListText()[(len(results['results'][-1]['title'])) * -1:]
|
||||
self.assertEqual(tested, test_result)
|
||||
|
||||
'''
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
@ -15,36 +15,43 @@ along with tmdb-bot. If not, see <https://www.gnu.org/licenses/>.
|
|||
'''
|
||||
import requests
|
||||
from html import escape
|
||||
import aiohttp, asyncio
|
||||
|
||||
|
||||
class TmdbApi():
|
||||
def __init__(self):
|
||||
self.load_parameters()
|
||||
self.session = aiohttp.ClientSession()
|
||||
self.language = 'en'
|
||||
self.valid = False
|
||||
|
||||
def load_parameters(self):
|
||||
async 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_images = result['images']['base_url']
|
||||
self.base_url_poster = self.base_url_images + result['images']['poster_sizes'][0]
|
||||
self.poster_sizes = result['images']['poster_sizes']
|
||||
async with self.session.get(self.base_url + 'configuration', params=self.get_apikey()) as resp:
|
||||
result = await resp.json()
|
||||
self.base_url_images = result['images']['base_url']
|
||||
self.base_url_poster = self.base_url_images + result['images']['poster_sizes'][0]
|
||||
self.poster_sizes = result['images']['poster_sizes']
|
||||
|
||||
def get_apikey(self):
|
||||
return {'api_key': self.api_key}
|
||||
|
||||
def request(self, request_uri, params: dict = {}):
|
||||
async def request(self, request_uri, params: dict = {}):
|
||||
url = self.base_url + request_uri.lstrip('/')
|
||||
params.update(self.get_apikey())
|
||||
params.update({'language': self.language})
|
||||
result = requests.get(url, params=params)
|
||||
self.valid = True
|
||||
return result.json()
|
||||
result = None
|
||||
async with self.session.get(url, params=params) as resp:
|
||||
result = await resp.json()
|
||||
self.valid = True
|
||||
return result
|
||||
|
||||
def set_language(self, language):
|
||||
self.language = language
|
||||
|
||||
async def close_session(self):
|
||||
await self.session.close()
|
||||
|
||||
|
||||
class TmdbApiSingle(TmdbApi):
|
||||
def __init__(self):
|
||||
|
@ -109,29 +116,35 @@ class Movie(TmdbApiSingle):
|
|||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def search_title(self, title: str, year: int = None) -> int:
|
||||
async def search_title(self, title: str, year: int = None) -> int:
|
||||
payload = {}
|
||||
payload['query'] = title
|
||||
if year:
|
||||
payload['year'] = year
|
||||
json = self.request('search/movie', params=payload)
|
||||
json = await self.request('search/movie', params=payload)
|
||||
if json['total_results'] > 0:
|
||||
movie_id = json['results'][0]['id']
|
||||
self.query_details(movie_id)
|
||||
#await asyncio.gather(
|
||||
await self.query_details(movie_id)
|
||||
await self.query_cast(movie_id)
|
||||
#)
|
||||
return movie_id
|
||||
|
||||
def query_details(self, id):
|
||||
data = self.request('movie/' + str(id))
|
||||
async def query_details(self, id):
|
||||
print('Get details')
|
||||
data = await self.request('movie/' + str(id))
|
||||
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()
|
||||
print('Got details')
|
||||
|
||||
def query_cast(self):
|
||||
data = self.request('movie/' + str(self.id) + '/credits')
|
||||
async def query_cast(self, id):
|
||||
print('Get Cast')
|
||||
data = await self.request('movie/' + str(id) + '/credits')
|
||||
print('Got Cast')
|
||||
self.cast = []
|
||||
for actor in data['cast']:
|
||||
self.cast.append(actor['name'])
|
||||
|
|
Loading…
Reference in a new issue