Initial source commit
This commit is contained in:
parent
76578f9c91
commit
a811653ebc
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -129,3 +129,4 @@ dmypy.json
|
|||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
lomion.tmdb.mbp
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
# tmdb-bot
|
||||
|
||||
A [maubot](https://github.com/maubot/maubot) to get information about movies from [TheMovieDB.org](https://www.themoviedb.org/).
|
||||
|
||||
## Usage
|
||||
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>.
|
7
maubot.yaml
Normal file
7
maubot.yaml
Normal file
|
@ -0,0 +1,7 @@
|
|||
maubot: 0.1.0
|
||||
id: lomion.tmdb
|
||||
version: 0.0.1
|
||||
license: AGPL 3.0
|
||||
modules:
|
||||
- tmdb
|
||||
main_class: TmdbBot
|
1
tmdb/__init__.py
Normal file
1
tmdb/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .tmdb import TmdbBot
|
58
tmdb/tmdb.py
Normal file
58
tmdb/tmdb.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
from html import escape
|
||||
|
||||
from mautrix.types import TextMessageEventContent, MediaMessageEventContent, MessageType, Format, RelatesTo, RelationType
|
||||
|
||||
from maubot import Plugin, MessageEvent
|
||||
from maubot.handlers import command
|
||||
|
||||
from tmdb.tmdb_api import Movie
|
||||
|
||||
class TmdbBot(Plugin):
|
||||
|
||||
async def send_movie_info(self, evt: MessageEvent, movie) -> None:
|
||||
mxc_uri = await self.client.upload_media(data=movie.get_image_binary())
|
||||
text_message = f'{movie.title}'
|
||||
if len(movie.overview) > 200:
|
||||
three_dotts = " [...]"
|
||||
else:
|
||||
three_dotts = ""
|
||||
cast = "Acting: "
|
||||
for actor in movie.cast[:3]:
|
||||
cast+= f'{actor}, '
|
||||
cast = cast[:-2]
|
||||
html_message = f"""<p><b>{escape(movie.title)}</b></p>
|
||||
<p>{escape(movie.overview)[:200]}{three_dotts}</p>
|
||||
<p>{cast}</p>
|
||||
<p>taken from www.themoviedb.org</p>"""
|
||||
content = TextMessageEventContent(
|
||||
msgtype=MessageType.TEXT, format=Format.HTML,
|
||||
body=f"{text_message}",
|
||||
formatted_body=f"{html_message}")
|
||||
await evt.respond(content)
|
||||
content = MediaMessageEventContent(
|
||||
msgtype=MessageType.IMAGE,
|
||||
body=f"Image {movie.title}",
|
||||
url=f"{mxc_uri}")
|
||||
await evt.respond(content)
|
||||
|
||||
|
||||
@command.new("movie-id", help="Movie lookup by id")
|
||||
@command.argument("message", pass_raw=True, required=True)
|
||||
async def movie_id(self, evt: MessageEvent, message: str = "") -> None:
|
||||
movie = Movie()
|
||||
movie.query_details(message)
|
||||
await self.send_movie_info(evt, movie)
|
||||
|
||||
|
||||
@command.new("movie-search", help="Movie lookup by Title")
|
||||
@command.argument("message", pass_raw=True, required=True)
|
||||
async def movie_search(self, evt: MessageEvent, message: str = "") -> None:
|
||||
movie = Movie()
|
||||
movie.search_title(message)
|
||||
if movie.valid:
|
||||
await self.send_movie_info(evt, movie)
|
||||
else:
|
||||
content = TextMessageEventContent(
|
||||
msgtype=MessageType.NOTICE, format=Format.HTML,
|
||||
body=f"No movie found!")
|
||||
await evt.respond(content)
|
72
tmdb/tmdb_api.py
Normal file
72
tmdb/tmdb_api.py
Normal file
|
@ -0,0 +1,72 @@
|
|||
import requests
|
||||
|
||||
class Connection():
|
||||
def __init__(self):
|
||||
self.load_parameters()
|
||||
|
||||
def load_parameters(self):
|
||||
self.api_key = '51d75c00dc1502dc894b7773ec3e7a15'
|
||||
|
||||
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.valid = False
|
||||
|
||||
def get_apikey(self):
|
||||
return { 'api_key' : self.api_key }
|
||||
|
||||
def request(self, request_uri):
|
||||
url = self.base_url + request_uri
|
||||
payload = self.get_apikey()
|
||||
payload['language'] = 'de'
|
||||
result = requests.get(url, params=payload)
|
||||
self.valid = True
|
||||
return result
|
||||
|
||||
def get_image_binary(self):
|
||||
return requests.get(self.poster_url).content
|
||||
|
||||
|
||||
class Movie(Connection):
|
||||
def __init__(self):
|
||||
self.load_parameters()
|
||||
pass
|
||||
|
||||
def search_title(self, title):
|
||||
url = self.base_url+ 'search/movie'
|
||||
payload = self.get_apikey()
|
||||
payload['language'] = 'de'
|
||||
payload['query'] = title
|
||||
result = requests.get(url, params=payload)
|
||||
json = result.json()
|
||||
if json['total_results'] > 0:
|
||||
movie_id = json['results'][0]['id']
|
||||
self.query_details(movie_id)
|
||||
return movie_id
|
||||
|
||||
def query_details(self, id):
|
||||
data = self.request('movie/' + str(id)).json()
|
||||
self.title = data['title']
|
||||
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/movie/' + str(self.id)
|
||||
self.vote_average = str(data['vote_average'])
|
||||
self.query_cast()
|
||||
|
||||
def query_cast(self):
|
||||
data = self.request('movie/'+str(self.id)+'/credits').json()
|
||||
self.cast = []
|
||||
for actor in data['cast']:
|
||||
self.cast.append(actor['name'])
|
||||
|
||||
def get_cast(self, amount):
|
||||
return self.cast[:amount]
|
||||
|
||||
movie = Movie()
|
||||
#movie.query_details('550')
|
||||
movie.search_title('Jack Reacher')
|
||||
print(movie.title)
|
||||
print(movie.overview[:150])
|
||||
print(movie.web_url)
|
||||
print(movie.poster_url)
|
Loading…
Reference in a new issue