Enhanced movie search by release year

This commit is contained in:
lomion 2020-06-26 16:41:44 +02:00
parent 05fd4b8f0b
commit 0eafc0fc3c
4 changed files with 32 additions and 4 deletions

View file

@ -4,7 +4,7 @@ A [maubot](https://github.com/maubot/maubot) to get information about movies fro
## Usage ## Usage
Use `!movie-id {tmdb id}` to get movie detail for tmdb-id. Use `!movie-id {tmdb id}` to get movie detail for tmdb-id.
Use `!movie-search {title}` to get movie detail based on the given title. Use `!movie-search {title} [y:{release year}]` to get movie detail based on the given title.
Use `!movie-language {language}` to set your prefered language. Use `!movie-language {language}` to set your prefered language.

View file

@ -65,6 +65,22 @@ class TestTmdbMethods(unittest.TestCase):
self.assertEqual(id, None) self.assertEqual(id, None)
self.assertEqual(None, movie.title) self.assertEqual(None, movie.title)
def test_search_year(self):
movie = Movie()
id = movie.search_title('Dune')
self.assertEqual(id, 841)
id = movie.search_title('Dune', 2020)
self.assertEqual(id, 438631)
def test_split_year(self):
tmdb = TmdbBot("","" ,"" ,"" ,"" ,"" ,"" ,"" ,"" )
title, year = tmdb.split_title_year('Dune')
self.assertEqual('Dune', title)
self.assertEqual(None, year)
title, year = tmdb.split_title_year('Dune y:2020 ')
self.assertEqual('Dune', title)
self.assertEqual(2020, year)
# TV Shows # TV Shows
def test_search_tvshow(self): def test_search_tvshow(self):
movie = TvShow() movie = TvShow()

View file

@ -14,6 +14,7 @@ You should have received a copy of the GNU Affero General Public License
along with tmdb-bot. If not, see <https://www.gnu.org/licenses/>. along with tmdb-bot. If not, see <https://www.gnu.org/licenses/>.
''' '''
from html import escape from html import escape
import re
from mautrix.types import TextMessageEventContent, MediaMessageEventContent, MessageType, Format, RelatesTo, RelationType from mautrix.types import TextMessageEventContent, MediaMessageEventContent, MessageType, Format, RelatesTo, RelationType
@ -62,6 +63,14 @@ class TmdbBot(Plugin):
<p>Taken from www.themoviedb.org</p>""" <p>Taken from www.themoviedb.org</p>"""
return html_message return html_message
def split_title_year(self, message : str) -> (str, int):
m = re.search(r'^(.*) (y:\d\d\d\d)', message)
if m:
title = m.group(1)
year = int(m.group(2)[2:])
return (title, year)
return (message, None)
async def send_movie_info(self, evt: MessageEvent, movie) -> None: async def send_movie_info(self, evt: MessageEvent, movie) -> None:
html_message = self.construct_html_message(movie) html_message = self.construct_html_message(movie)
await self.send_html_message(evt, f'{movie.title}', html_message) await self.send_html_message(evt, f'{movie.title}', html_message)
@ -86,7 +95,8 @@ class TmdbBot(Plugin):
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) title, year = self.split_title_year(message)
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:

View file

@ -60,9 +60,11 @@ class Movie(TmdbApi):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
def search_title(self, title): def search_title(self, title : str, year: int = None) -> int:
payload = {} payload = {}
payload['query'] = title payload['query'] = title
if year:
payload['year'] = year
result = self.request('search/movie', params=payload) result = self.request('search/movie', params=payload)
json = result.json() json = result.json()
if json['total_results'] > 0: if json['total_results'] > 0: