From b3f8d56139a404e64c6fad75119e9f6ba9b125b3 Mon Sep 17 00:00:00 2001 From: starry69 Date: Fri, 16 Oct 2020 21:49:36 +0530 Subject: [PATCH] V1.8: Add reviews method & improved code formatting Signed-off-by: starry69 --- README.md | 10 +++- pymoviedb/__init__.py | 2 +- pymoviedb/excs/__init__.py | 2 +- pymoviedb/pytmdb.py | 116 +++++++++++++++++++++++++++---------- setup.py | 5 +- 5 files changed, 100 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index b902228..0537f46 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,19 @@ Exceptions can be imported from `pymoviedb.excs` for easy error handling. Example: ```python -from pymoviedb.excs import ZeroResultsFound +from pymoviedb.excs import ZeroResultsFound, TmdbApiError tv = TvShows(APIKEY) try: print(tv.search("xsxsxsx")) except ZeroResultsFound: - print("Nothing found!") + print("Nothing found!") + +try: + print(tv.searchid("xsxsxsxs")) +except TmdbApiError as e: + print(e.message) + ``` diff --git a/pymoviedb/__init__.py b/pymoviedb/__init__.py index b3fcbed..f9e5e14 100644 --- a/pymoviedb/__init__.py +++ b/pymoviedb/__init__.py @@ -15,7 +15,7 @@ from .excs import * __author__ = ["Stɑrry Shivɑm"] -__version__ = 1.7 +__version__ = 1.8 if __name__ == "__main__": from pprint import pprint diff --git a/pymoviedb/excs/__init__.py b/pymoviedb/excs/__init__.py index 5bc07d0..dd0e1aa 100644 --- a/pymoviedb/excs/__init__.py +++ b/pymoviedb/excs/__init__.py @@ -4,7 +4,7 @@ class TmdbApiError(Exception): """ def __init__(self, message): - super().__init__(message) + super().__init__(message["status_message"]) class ZeroResultsFound(Exception): diff --git a/pymoviedb/pytmdb.py b/pymoviedb/pytmdb.py index 8f486f5..c6043d1 100644 --- a/pymoviedb/pytmdb.py +++ b/pymoviedb/pytmdb.py @@ -80,10 +80,10 @@ def search( check_res = resp.json()["results"] if len(check_res) <= 0: - raise ZeroResultsFound(resp.text) + raise ZeroResultsFound(resp.json()) return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def searchid( self, @@ -105,7 +105,7 @@ def searchid( if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def recommendations( self, movie_id: int, language: str = "en", page: Optional[int] = 1 @@ -116,11 +116,14 @@ def recommendations( payload = {"api_key": self.api_key, "language": language, "page": page} - resp = get(self.base_url + f"/movie/{movie_id}/recommendations", params=payload) + resp = get( + self.base_url + f"/movie/{movie_id}/recommendations", + params=payload, + ) if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def trending(self, time_win: str = "week") -> Dict[str, Any]: """ @@ -128,11 +131,13 @@ def trending(self, time_win: str = "week") -> Dict[str, Any]: """ payload = {"api_key": self.api_key} - resp = get(self.base_url + f"/trending/movie/{time_win}", params=payload) + resp = get( + self.base_url + f"/trending/movie/{time_win}", params=payload + ) if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def certification(self) -> Dict[str, Any]: """ @@ -146,7 +151,7 @@ def certification(self) -> Dict[str, Any]: if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def genre(self, language: str = "en") -> Dict[str, Any]: """ @@ -158,7 +163,7 @@ def genre(self, language: str = "en") -> Dict[str, Any]: if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def alternative_titles( self, movie_id: int, country: Optional[str] = None @@ -170,30 +175,35 @@ def alternative_titles( payload = {"api_key": self.api_key, "country": country} resp = get( - self.base_url + f"/movie/{movie_id}/alternative_titles", params=payload + self.base_url + f"/movie/{movie_id}/alternative_titles", + params=payload, ) if resp.status_code == 200: check_res = resp.json()["titles"] if len(check_res) <= 0: - raise ZeroResultsFound(resp.text) + raise ZeroResultsFound(resp.json()) return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) - def collection(self, collection_id: int, language: str = "en") -> Dict[str, Any]: + def collection( + self, collection_id: int, language: str = "en" + ) -> Dict[str, Any]: """ Get collection details by id. """ payload = {"api_key": self.api_key, "language": language} - resp = get(self.base_url + f"/collection/{collection_id}", params=payload) + resp = get( + self.base_url + f"/collection/{collection_id}", params=payload + ) if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def search_collection( self, query: str, language: str = "en", page: Optional[int] = 1 @@ -215,10 +225,32 @@ def search_collection( check_res = resp.json()["results"] if len(check_res) <= 0: - raise ZeroResultsFound(resp.text) + raise ZeroResultsFound(resp.json()) return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) + + def reviews( + self, movie_id: int, language: str = "en", page: Optional[int] = 1 + ) -> Dict[str, Any]: + """ + Get the user reviews for a movie. + """ + + payload = {"api_key": self.api_key, "language": language, "page": page} + + resp = get( + self.base_url + f"/movie/{movie_id}/reviews", params=payload + ) + + if resp.status_code == 200: + + check_res = resp.json()["results"] + if len(check_res) <= 0: + raise ZeroResultsFound(resp.json()) + return resp.json() + + raise TmdbApiError(resp.json()) ############################## TvShows class ############################## @@ -252,10 +284,10 @@ def search( check_res = resp.json()["results"] if len(check_res) <= 0: - raise ZeroResultsFound(resp.text) + raise ZeroResultsFound(resp.json()) return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def searchid( self, @@ -277,7 +309,7 @@ def searchid( if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def recommendations( self, tv_id: int, language: str = "en", page: Optional[int] = 1 @@ -288,11 +320,13 @@ def recommendations( payload = {"api_key": self.api_key, "language": language, "page": page} - resp = get(self.base_url + f"/tv/{tv_id}/recommendations", params=payload) + resp = get( + self.base_url + f"/tv/{tv_id}/recommendations", params=payload + ) if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def trending(self, time_win: str = "week") -> Dict[str, Any]: """ @@ -304,7 +338,7 @@ def trending(self, time_win: str = "week") -> Dict[str, Any]: if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def certification(self) -> Dict[str, Any]: """ @@ -318,7 +352,7 @@ def certification(self) -> Dict[str, Any]: if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) def genre(self, language: str = "en") -> Dict[str, Any]: """ @@ -330,22 +364,46 @@ def genre(self, language: str = "en") -> Dict[str, Any]: if resp.status_code == 200: return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) - def alternative_titles(self, tv_id: int, language: str = "en") -> Dict[str, Any]: + def alternative_titles( + self, tv_id: int, language: str = "en" + ) -> Dict[str, Any]: """ Returns all of the alternative titles for a TV show. """ payload = {"api_key": self.api_key, "language": language} - resp = get(self.base_url + f"/tv/{tv_id}/alternative_titles", params=payload) + resp = get( + self.base_url + f"/tv/{tv_id}/alternative_titles", params=payload + ) + + if resp.status_code == 200: + + check_res = resp.json()["results"] + if len(check_res) <= 0: + raise ZeroResultsFound(resp.json()) + return resp.json() + + raise TmdbApiError(resp.json()) + + def reviews( + self, tv_id: int, language: str = "en", page: Optional[int] = 1 + ) -> Dict[str, Any]: + """ + Get the user reviews for a movie. + """ + + payload = {"api_key": self.api_key, "language": language, "page": page} + + resp = get(self.base_url + f"/tv/{tv_id}/reviews", params=payload) if resp.status_code == 200: check_res = resp.json()["results"] if len(check_res) <= 0: - raise ZeroResultsFound(resp.text) + raise ZeroResultsFound(resp.json()) return resp.json() - raise TmdbApiError(resp.text) + raise TmdbApiError(resp.json()) diff --git a/setup.py b/setup.py index ce2dd54..75b3cf9 100644 --- a/setup.py +++ b/setup.py @@ -5,9 +5,10 @@ with open("README.md", "r") as f: long_description = f.read() -with open("pymoviedb/__init__.py", 'r') as f: +with open("pymoviedb/__init__.py", "r") as f: import re - version = re.search('__version__ = (\S+)', f.read()).group(1) + + version = re.search("__version__ = (\S+)", f.read()).group(1) setup( name="pymoviedb",