2020-06-22 15:22:28 +01:00
|
|
|
'''
|
|
|
|
This file is part of tmdb-bot.
|
|
|
|
|
|
|
|
tmdb-bot is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License version 3 as published by
|
|
|
|
the Free Software Foundation.
|
|
|
|
|
|
|
|
tmdb-bot is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
'''
|
2020-06-22 12:15:28 +01:00
|
|
|
import requests
|
2021-03-16 20:45:27 +00:00
|
|
|
from html import escape
|
2020-06-22 12:15:28 +01:00
|
|
|
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-23 16:47:37 +01:00
|
|
|
class TmdbApi():
|
2020-06-22 12:15:28 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.load_parameters()
|
2020-08-18 14:33:19 +01:00
|
|
|
self.language = 'en'
|
2020-06-23 16:47:37 +01:00
|
|
|
self.valid = False
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-22 12:15:28 +01:00
|
|
|
def load_parameters(self):
|
|
|
|
self.api_key = '51d75c00dc1502dc894b7773ec3e7a15'
|
|
|
|
self.base_url = "https://api.themoviedb.org/3/"
|
2021-03-16 15:35:02 +00:00
|
|
|
result = requests.get(self.base_url + 'configuration', params=self.get_apikey()).json()
|
2020-09-11 10:15:04 +01:00
|
|
|
self.base_url_images = result['images']['base_url']
|
|
|
|
self.base_url_poster = self.base_url_images + result['images']['poster_sizes'][0]
|
2020-09-09 14:18:22 +01:00
|
|
|
self.poster_sizes = result['images']['poster_sizes']
|
2020-06-22 12:15:28 +01:00
|
|
|
|
|
|
|
def get_apikey(self):
|
2021-03-16 15:35:02 +00:00
|
|
|
return {'api_key': self.api_key}
|
2020-06-22 12:15:28 +01:00
|
|
|
|
2021-03-16 15:35:02 +00:00
|
|
|
def request(self, request_uri, params: dict = {}):
|
2020-06-24 09:45:18 +01:00
|
|
|
url = self.base_url + request_uri.lstrip('/')
|
|
|
|
params.update(self.get_apikey())
|
2021-03-16 15:35:02 +00:00
|
|
|
params.update({'language': self.language})
|
2020-06-24 09:45:18 +01:00
|
|
|
result = requests.get(url, params=params)
|
2020-06-22 12:15:28 +01:00
|
|
|
self.valid = True
|
2021-03-16 16:56:02 +00:00
|
|
|
return result.json()
|
2020-06-22 12:15:28 +01:00
|
|
|
|
2020-06-22 13:41:31 +01:00
|
|
|
def set_language(self, language):
|
|
|
|
self.language = language
|
|
|
|
|
2021-03-16 16:56:02 +00:00
|
|
|
|
|
|
|
class TmdbApiSingle(TmdbApi):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.title = None
|
|
|
|
self.id = None
|
|
|
|
self.poster_url = None
|
|
|
|
self.overview = None
|
|
|
|
self.web_url = None
|
|
|
|
self.vote_average = None
|
|
|
|
|
2020-06-22 12:15:28 +01:00
|
|
|
def get_image_binary(self):
|
2020-06-23 16:47:37 +01:00
|
|
|
if self.poster_url:
|
|
|
|
return requests.get(self.poster_url).content
|
|
|
|
else:
|
|
|
|
return None
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-09-09 14:18:22 +01:00
|
|
|
def set_poster_size(self, size):
|
|
|
|
for x in self.poster_sizes:
|
|
|
|
if x == size:
|
2020-09-11 10:15:04 +01:00
|
|
|
self.base_url_poster = self.base_url_images + x
|
2020-09-09 14:18:22 +01:00
|
|
|
return x
|
|
|
|
return None
|
2020-06-22 12:15:28 +01:00
|
|
|
|
2021-03-16 20:45:27 +00:00
|
|
|
|
2021-03-16 16:56:02 +00:00
|
|
|
class MoviePopular(TmdbApi):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.list = []
|
|
|
|
self.length = 0
|
|
|
|
|
|
|
|
def query(self) -> int:
|
|
|
|
result = self.request('/movie/popular')
|
|
|
|
self.length = result['total_results']
|
|
|
|
self.list = result['results']
|
|
|
|
return self.length
|
|
|
|
|
2021-03-16 20:45:27 +00:00
|
|
|
def getListHtml(self, length: int = None) -> str:
|
|
|
|
self.query()
|
|
|
|
html = ""
|
|
|
|
if length:
|
|
|
|
loop = length
|
|
|
|
else:
|
|
|
|
loop = self.length
|
|
|
|
for element in self.list[:loop]:
|
|
|
|
html += f"""<p><a href="https://www.themoviedb.org/movie/{str(element['id'])}">{escape(element['title'])}</a></p>"""
|
|
|
|
return html
|
|
|
|
|
|
|
|
def getListText(self, length: int = None) -> str:
|
|
|
|
self.query()
|
|
|
|
text = ""
|
|
|
|
if length:
|
|
|
|
loop = length
|
|
|
|
else:
|
|
|
|
loop = self.length
|
|
|
|
for element in self.list[:loop]:
|
|
|
|
text += element['title']
|
|
|
|
return text
|
|
|
|
|
2020-06-22 12:15:28 +01:00
|
|
|
|
2021-03-16 16:56:02 +00:00
|
|
|
class Movie(TmdbApiSingle):
|
2020-06-22 12:15:28 +01:00
|
|
|
def __init__(self):
|
2020-06-23 16:47:37 +01:00
|
|
|
super().__init__()
|
2021-03-16 15:35:02 +00:00
|
|
|
|
|
|
|
def search_title(self, title: str, year: int = None) -> int:
|
2020-06-24 09:45:18 +01:00
|
|
|
payload = {}
|
2020-06-22 12:15:28 +01:00
|
|
|
payload['query'] = title
|
2020-06-26 15:41:44 +01:00
|
|
|
if year:
|
|
|
|
payload['year'] = year
|
2021-03-16 16:56:02 +00:00
|
|
|
json = self.request('search/movie', params=payload)
|
2020-06-22 12:15:28 +01:00
|
|
|
if json['total_results'] > 0:
|
|
|
|
movie_id = json['results'][0]['id']
|
|
|
|
self.query_details(movie_id)
|
|
|
|
return movie_id
|
|
|
|
|
|
|
|
def query_details(self, id):
|
2021-03-16 16:56:02 +00:00
|
|
|
data = self.request('movie/' + str(id))
|
2020-06-22 12:15:28 +01:00
|
|
|
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()
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-22 12:15:28 +01:00
|
|
|
def query_cast(self):
|
2021-03-16 16:56:02 +00:00
|
|
|
data = self.request('movie/' + str(self.id) + '/credits')
|
2020-06-22 12:15:28 +01:00
|
|
|
self.cast = []
|
|
|
|
for actor in data['cast']:
|
|
|
|
self.cast.append(actor['name'])
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-22 12:15:28 +01:00
|
|
|
def get_cast(self, amount):
|
|
|
|
return self.cast[:amount]
|
2020-06-24 09:45:18 +01:00
|
|
|
|
|
|
|
|
2021-03-16 16:56:02 +00:00
|
|
|
class TvShow(TmdbApiSingle):
|
2020-06-24 09:45:18 +01:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-24 09:45:18 +01:00
|
|
|
def search_title(self, title):
|
|
|
|
payload = {}
|
|
|
|
payload['query'] = title
|
2021-03-16 16:56:02 +00:00
|
|
|
json = self.request('/search/tv', params=payload)
|
2020-06-24 09:45:18 +01:00
|
|
|
if json['total_results'] > 0:
|
|
|
|
movie_id = json['results'][0]['id']
|
|
|
|
self.query_details(movie_id)
|
|
|
|
return movie_id
|
|
|
|
|
|
|
|
def query_details(self, id):
|
2021-03-16 16:56:02 +00:00
|
|
|
data = self.request('tv/' + str(id))
|
2020-06-24 09:45:18 +01:00
|
|
|
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()
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-24 09:45:18 +01:00
|
|
|
def query_cast(self):
|
2021-03-16 16:56:02 +00:00
|
|
|
data = self.request('tv/' + str(self.id) + '/credits')
|
2020-06-24 09:45:18 +01:00
|
|
|
self.cast = []
|
|
|
|
for actor in data['cast']:
|
|
|
|
self.cast.append(actor['name'])
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-24 09:45:18 +01:00
|
|
|
def get_cast(self, amount):
|
2021-03-16 15:35:02 +00:00
|
|
|
return self.cast[:amount]
|