Initial creation of poster size implementation

This commit is contained in:
lomion 2020-09-09 15:18:22 +02:00
parent 14d9ff8654
commit 0127ad1f66
4 changed files with 47 additions and 5 deletions

View file

@ -26,7 +26,7 @@ class TestTmdbMethods(unittest.TestCase):
def test_overview(self):
movie = Movie()
movie.search_title('Breakfast Club')
description = 'Samstag morgen in einer amerikanischen High-School'
description = 'Five high school students from different walks of'
self.assertEqual(description, movie.overview[:len(description)])
def test_change_language(self):
@ -42,7 +42,7 @@ class TestTmdbMethods(unittest.TestCase):
movie.query_details('550')
message = tmdb.construct_html_message(movie, overview_length = 10)
self.assertEqual(message, """<p><a href="https://www.themoviedb.org/movie/550"><b>Fight Club</b></a></p>
<p>Ein Yuppie [...]</p>
<p>A ticking- [...]</p>
<p>Acting: Edward Norton, Brad Pitt, Helena Bonham Carter</p>
<p>Taken from www.themoviedb.org</p>""")
@ -81,6 +81,13 @@ class TestTmdbMethods(unittest.TestCase):
self.assertEqual('Dune', title)
self.assertEqual(2020, year)
def test_set_poster_size(self):
movie = Movie()
size = movie.set_poster_size("w500")
self.assertEqual(size, "w500")
size = movie.set_poster_size("w666")
self.assertEqual(size, None)
# TV Shows
def test_search_tvshow(self):
movie = TvShow()
@ -95,7 +102,7 @@ class TestTmdbMethods(unittest.TestCase):
def test_cast(self):
movie = TvShow()
movie.search_title('The Flash')
self.assertEqual('Grant Gustin', movie.cast[0])
self.assertEqual('Tom Cavanagh', movie.cast[0])
self.assertEqual('Carlos Valdes', movie.cast[2])
if __name__ == '__main__':

View file

@ -31,6 +31,10 @@ class Database:
Column("id", Integer, primary_key=True, autoincrement=True),
Column("user_id", String(255), nullable=False),
Column("language", String(255), nullable=False),)
self.tmdb_poster_size = Table("tmdb_poster_size", meta,
Column("id", Integer, primary_key=True, autoincrement=True),
Column("user_id", String(255), nullable=False),
Column("size", String(255), nullable=False),)
meta.create_all(db)
def set_language(self, user_id, language):
@ -42,3 +46,13 @@ class Database:
rows = self.db.execute(select([self.language.c.language])
.where(self.language.c.user_id == user_id))
return rows.fetchone()['language']
def set_poster_size(self, user_id, size):
with self.db.begin() as tx:
tx.execute(self.tmdb_poster_size.delete().where(self.tmdb_poster_size.c.user_id == user_id))
tx.execute(self.tmdb_poster_size.insert().values(user_id=user_id, size=size))
def get_poster_size(self, user_id):
rows = self.db.execute(select([self.tmdb_poster_size.c.size])
.where(self.tmdb_poster_size.c.user_id == user_id))
return rows.fetchone()['size']

View file

@ -149,6 +149,17 @@ class TmdbBot(Plugin):
body=f"Language set to {message}!")
await evt.respond(content)
async def poster_size(self, evt: MessageEvent, message: str = None) -> None:
movie = Movie()
if message:
size = movie.set_poster_size(message)
else:
self.db.set_language(evt.sender, message)
content = TextMessageEventContent(
msgtype=MessageType.NOTICE, format=Format.HTML,
body=f"Language set to {message}!")
await evt.respond(content)
@command.new("movie-language", help="Set language for lookup")
@command.argument("message", pass_raw=True, required=True)
async def command_movie_language(self, evt: MessageEvent, message: str = "") -> None:
@ -191,6 +202,8 @@ class TmdbBot(Plugin):
await self.movie_search(evt, parameters)
elif command.lower() == 'language':
await self.movie_language(evt, parameters)
elif command.lower() == 'poster_size':
await self.poster_size(evt, parameters)
elif command.lower() == 'tvshow':
await self.tvshow_search(evt, parameters)
elif command.lower() == 'id':

View file

@ -34,6 +34,7 @@ class TmdbApi():
self.base_url = "https://api.themoviedb.org/3/"
result = requests.get(self.base_url + 'configuration', params = self.get_apikey()).json()
self.base_url_poster = result['images']['base_url'] + result['images']['poster_sizes'][0]
self.poster_sizes = result['images']['poster_sizes']
def get_apikey(self):
return { 'api_key' : self.api_key }
@ -55,6 +56,13 @@ class TmdbApi():
else:
return None
def set_poster_size(self, size):
for x in self.poster_sizes:
if x == size:
self.base_url_poster = result['images']['base_url'] + x
return x
return None
class Movie(TmdbApi):
def __init__(self):