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