Improved Movie and TvShow search

This commit is contained in:
lomion 2021-09-23 13:39:40 +02:00
parent 67e21f1f1e
commit 28c2a9e474
4 changed files with 42 additions and 27 deletions

View file

@ -8,7 +8,7 @@ async def test():
movie = Movie()
await movie.load_parameters()
id = await movie.search_title('Breakfast Club')
print("ID of Breakfast Club " + id)
print("ID of Breakfast Club " + str(id))
print(time.time() - start)
await movie.close_session()

View file

@ -5,7 +5,7 @@ pipeline:
- pip install -r requirements.txt
- pip install flake8
- python3 test_tmdb.py
- flake8 --ignore=E501 --exclude=__init__.py --exclude=test_tmdb.py
- flake8 --ignore=E501 --exclude=__init__.py,test_tmdb.py
build:
image: alpine
@ -16,4 +16,4 @@ pipeline:
upload:
image: uploader
project: tmdb-bot
artifact: lomion.tmdb.${DRONE_COMMIT_SHA:0:8}.mbp
artifact: lomion.tmdb.${DRONE_COMMIT_SHA:0:8}.mbp

View file

@ -132,15 +132,26 @@ class TmdbBot(Plugin):
constructor = MessageConstructor(movie)
html_message = constructor.construct_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:
movie = Movie()
movie = await self.init_movie()
self.poster_size(evt, movie)
language = self.db.get_language(evt.sender)
if language:
movie.set_language(language)
movie.query_details(message)
await movie.query_details(message)
await self.send_movie_info(evt, movie)
@command.new("movie-id", help="Movie lookup by id")
@ -169,7 +180,7 @@ class TmdbBot(Plugin):
await evt.respond(content)
async def set_poster_size(self, evt: MessageEvent, message: str = None) -> None:
movie = Movie()
movie = await self.init_movie()
poster_sizes = ""
for x in movie.poster_sizes:
poster_sizes += x + " "
@ -197,12 +208,12 @@ class TmdbBot(Plugin):
await self.send_help(evt)
async def tvshow_search(self, evt: MessageEvent, message: str = "") -> None:
movie = TvShow()
movie = await self.init_tvshow()
self.poster_size(evt, movie)
language = self.db.get_language(evt.sender)
if language:
movie.set_language(language)
movie.search_title(message)
await movie.search_title(message)
if movie.valid:
await self.send_movie_info(evt, movie)
else:
@ -214,13 +225,13 @@ class TmdbBot(Plugin):
await self.tvshow_search(evt, message)
async def movie_search(self, evt: MessageEvent, message: str = "") -> None:
movie = Movie()
movie = await self.init_movie()
self.poster_size(evt, movie)
language = self.db.get_language(evt.sender)
if language:
movie.set_language(language)
title, year = self.split_title_year(message)
movie.search_title(title, year)
await movie.search_title(title, year)
if movie.valid:
await self.send_movie_info(evt, movie)
else:

View file

@ -59,15 +59,20 @@ class TmdbApiSingle(TmdbApi):
self.title = None
self.id = None
self.poster_url = None
self.poster_binary = None
self.overview = None
self.web_url = None
self.vote_average = None
def get_image_binary(self):
return self.poster_binary
async def query_image_binary(self):
if self.poster_url:
async with self.session.get(self.poster_url) as resp:
return await resp.json()
return None
self.poster_binary = await resp.read()
else:
self.poster_binary = None
def set_poster_size(self, size):
for x in self.poster_sizes:
@ -124,13 +129,13 @@ class Movie(TmdbApiSingle):
json = await self.request('search/movie', params=payload)
if json['total_results'] > 0:
movie_id = json['results'][0]['id']
await self.query_details(movie_id)
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
async def query_details(self, id):
print('Get details')
data = await self.request('movie/' + str(id))
self.title = data['title']
self.id = data['id']
@ -138,12 +143,9 @@ class Movie(TmdbApiSingle):
self.overview = data['overview']
self.web_url = 'https://www.themoviedb.org/movie/' + str(self.id)
self.vote_average = str(data['vote_average'])
print('Got details')
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'])
@ -156,27 +158,29 @@ class TvShow(TmdbApiSingle):
def __init__(self):
super().__init__()
def search_title(self, title):
async def search_title(self, title):
payload = {}
payload['query'] = title
json = self.request('/search/tv', params=payload)
json = await self.request('/search/tv', params=payload)
if json['total_results'] > 0:
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
def query_details(self, id):
data = self.request('tv/' + str(id))
async def query_details(self, id):
data = await self.request('tv/' + str(id))
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')
async def query_cast(self):
data = await self.request('tv/' + str(self.id) + '/credits')
self.cast = []
for actor in data['cast']:
self.cast.append(actor['name'])