Improved Movie and TvShow search
This commit is contained in:
parent
67e21f1f1e
commit
28c2a9e474
|
@ -8,7 +8,7 @@ async def test():
|
||||||
movie = Movie()
|
movie = Movie()
|
||||||
await movie.load_parameters()
|
await movie.load_parameters()
|
||||||
id = await movie.search_title('Breakfast Club')
|
id = await movie.search_title('Breakfast Club')
|
||||||
print("ID of Breakfast Club " + id)
|
print("ID of Breakfast Club " + str(id))
|
||||||
print(time.time() - start)
|
print(time.time() - start)
|
||||||
await movie.close_session()
|
await movie.close_session()
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ pipeline:
|
||||||
- pip install -r requirements.txt
|
- pip install -r requirements.txt
|
||||||
- pip install flake8
|
- pip install flake8
|
||||||
- python3 test_tmdb.py
|
- python3 test_tmdb.py
|
||||||
- flake8 --ignore=E501 --exclude=__init__.py --exclude=test_tmdb.py
|
- flake8 --ignore=E501 --exclude=__init__.py,test_tmdb.py
|
||||||
|
|
||||||
build:
|
build:
|
||||||
image: alpine
|
image: alpine
|
||||||
|
|
27
tmdb/tmdb.py
27
tmdb/tmdb.py
|
@ -132,15 +132,26 @@ class TmdbBot(Plugin):
|
||||||
constructor = MessageConstructor(movie)
|
constructor = MessageConstructor(movie)
|
||||||
html_message = constructor.construct_html_message()
|
html_message = constructor.construct_html_message()
|
||||||
await self.send_html_message(evt, f'{movie.title}', html_message)
|
await self.send_html_message(evt, f'{movie.title}', html_message)
|
||||||
await self.send_image(evt, movie.title, movie.get_image_binary())
|
if movie.get_image_binary():
|
||||||
|
await self.send_image(evt, movie.title, movie.get_image_binary())
|
||||||
|
|
||||||
|
async def init_movie(self):
|
||||||
|
movie = Movie()
|
||||||
|
await movie.load_parameters()
|
||||||
|
return movie
|
||||||
|
|
||||||
|
async def init_tvshow(self):
|
||||||
|
show = TvShow()
|
||||||
|
await show.load_parameters()
|
||||||
|
return show
|
||||||
|
|
||||||
async def movie_id(self, evt: MessageEvent, message: str = "") -> None:
|
async def movie_id(self, evt: MessageEvent, message: str = "") -> None:
|
||||||
movie = Movie()
|
movie = await self.init_movie()
|
||||||
self.poster_size(evt, movie)
|
self.poster_size(evt, movie)
|
||||||
language = self.db.get_language(evt.sender)
|
language = self.db.get_language(evt.sender)
|
||||||
if language:
|
if language:
|
||||||
movie.set_language(language)
|
movie.set_language(language)
|
||||||
movie.query_details(message)
|
await movie.query_details(message)
|
||||||
await self.send_movie_info(evt, movie)
|
await self.send_movie_info(evt, movie)
|
||||||
|
|
||||||
@command.new("movie-id", help="Movie lookup by id")
|
@command.new("movie-id", help="Movie lookup by id")
|
||||||
|
@ -169,7 +180,7 @@ class TmdbBot(Plugin):
|
||||||
await evt.respond(content)
|
await evt.respond(content)
|
||||||
|
|
||||||
async def set_poster_size(self, evt: MessageEvent, message: str = None) -> None:
|
async def set_poster_size(self, evt: MessageEvent, message: str = None) -> None:
|
||||||
movie = Movie()
|
movie = await self.init_movie()
|
||||||
poster_sizes = ""
|
poster_sizes = ""
|
||||||
for x in movie.poster_sizes:
|
for x in movie.poster_sizes:
|
||||||
poster_sizes += x + " "
|
poster_sizes += x + " "
|
||||||
|
@ -197,12 +208,12 @@ class TmdbBot(Plugin):
|
||||||
await self.send_help(evt)
|
await self.send_help(evt)
|
||||||
|
|
||||||
async def tvshow_search(self, evt: MessageEvent, message: str = "") -> None:
|
async def tvshow_search(self, evt: MessageEvent, message: str = "") -> None:
|
||||||
movie = TvShow()
|
movie = await self.init_tvshow()
|
||||||
self.poster_size(evt, movie)
|
self.poster_size(evt, movie)
|
||||||
language = self.db.get_language(evt.sender)
|
language = self.db.get_language(evt.sender)
|
||||||
if language:
|
if language:
|
||||||
movie.set_language(language)
|
movie.set_language(language)
|
||||||
movie.search_title(message)
|
await movie.search_title(message)
|
||||||
if movie.valid:
|
if movie.valid:
|
||||||
await self.send_movie_info(evt, movie)
|
await self.send_movie_info(evt, movie)
|
||||||
else:
|
else:
|
||||||
|
@ -214,13 +225,13 @@ class TmdbBot(Plugin):
|
||||||
await self.tvshow_search(evt, message)
|
await self.tvshow_search(evt, message)
|
||||||
|
|
||||||
async def movie_search(self, evt: MessageEvent, message: str = "") -> None:
|
async def movie_search(self, evt: MessageEvent, message: str = "") -> None:
|
||||||
movie = Movie()
|
movie = await self.init_movie()
|
||||||
self.poster_size(evt, movie)
|
self.poster_size(evt, movie)
|
||||||
language = self.db.get_language(evt.sender)
|
language = self.db.get_language(evt.sender)
|
||||||
if language:
|
if language:
|
||||||
movie.set_language(language)
|
movie.set_language(language)
|
||||||
title, year = self.split_title_year(message)
|
title, year = self.split_title_year(message)
|
||||||
movie.search_title(title, year)
|
await movie.search_title(title, year)
|
||||||
if movie.valid:
|
if movie.valid:
|
||||||
await self.send_movie_info(evt, movie)
|
await self.send_movie_info(evt, movie)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -59,15 +59,20 @@ class TmdbApiSingle(TmdbApi):
|
||||||
self.title = None
|
self.title = None
|
||||||
self.id = None
|
self.id = None
|
||||||
self.poster_url = None
|
self.poster_url = None
|
||||||
|
self.poster_binary = None
|
||||||
self.overview = None
|
self.overview = None
|
||||||
self.web_url = None
|
self.web_url = None
|
||||||
self.vote_average = None
|
self.vote_average = None
|
||||||
|
|
||||||
def get_image_binary(self):
|
def get_image_binary(self):
|
||||||
|
return self.poster_binary
|
||||||
|
|
||||||
|
async def query_image_binary(self):
|
||||||
if self.poster_url:
|
if self.poster_url:
|
||||||
async with self.session.get(self.poster_url) as resp:
|
async with self.session.get(self.poster_url) as resp:
|
||||||
return await resp.json()
|
self.poster_binary = await resp.read()
|
||||||
return None
|
else:
|
||||||
|
self.poster_binary = None
|
||||||
|
|
||||||
def set_poster_size(self, size):
|
def set_poster_size(self, size):
|
||||||
for x in self.poster_sizes:
|
for x in self.poster_sizes:
|
||||||
|
@ -124,13 +129,13 @@ class Movie(TmdbApiSingle):
|
||||||
json = await 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']
|
||||||
|
await self.query_details(movie_id)
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
self.query_details(movie_id),
|
self.query_cast(movie_id),
|
||||||
self.query_cast(movie_id))
|
self.query_image_binary())
|
||||||
return movie_id
|
return movie_id
|
||||||
|
|
||||||
async def query_details(self, id):
|
async def query_details(self, id):
|
||||||
print('Get details')
|
|
||||||
data = await self.request('movie/' + str(id))
|
data = await self.request('movie/' + str(id))
|
||||||
self.title = data['title']
|
self.title = data['title']
|
||||||
self.id = data['id']
|
self.id = data['id']
|
||||||
|
@ -138,12 +143,9 @@ class Movie(TmdbApiSingle):
|
||||||
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'])
|
||||||
print('Got details')
|
|
||||||
|
|
||||||
async def query_cast(self, id):
|
async def query_cast(self, id):
|
||||||
print('Get Cast')
|
|
||||||
data = await self.request('movie/' + str(id) + '/credits')
|
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'])
|
||||||
|
@ -156,27 +158,29 @@ class TvShow(TmdbApiSingle):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def search_title(self, title):
|
async def search_title(self, title):
|
||||||
payload = {}
|
payload = {}
|
||||||
payload['query'] = title
|
payload['query'] = title
|
||||||
json = self.request('/search/tv', params=payload)
|
json = await self.request('/search/tv', 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 self.query_details(movie_id)
|
||||||
|
await asyncio.gather(
|
||||||
|
self.query_cast(),
|
||||||
|
self.query_image_binary())
|
||||||
return movie_id
|
return movie_id
|
||||||
|
|
||||||
def query_details(self, id):
|
async def query_details(self, id):
|
||||||
data = self.request('tv/' + str(id))
|
data = await self.request('tv/' + str(id))
|
||||||
self.title = data['name']
|
self.title = data['name']
|
||||||
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/tv/' + str(self.id)
|
self.web_url = 'https://www.themoviedb.org/tv/' + str(self.id)
|
||||||
self.vote_average = str(data['vote_average'])
|
self.vote_average = str(data['vote_average'])
|
||||||
self.query_cast()
|
|
||||||
|
|
||||||
def query_cast(self):
|
async def query_cast(self):
|
||||||
data = self.request('tv/' + str(self.id) + '/credits')
|
data = await self.request('tv/' + str(self.id) + '/credits')
|
||||||
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