Skip to content

Commit

Permalink
V1.8: Add reviews method & improved code formatting
Browse files Browse the repository at this point in the history
Signed-off-by: starry69 <[email protected]>
  • Loading branch information
starry-shivam committed Oct 16, 2020
1 parent 755f795 commit b3f8d56
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 35 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

```


Expand Down
2 changes: 1 addition & 1 deletion pymoviedb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pymoviedb/excs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class TmdbApiError(Exception):
"""

def __init__(self, message):
super().__init__(message)
super().__init__(message["status_message"])


class ZeroResultsFound(Exception):
Expand Down
116 changes: 87 additions & 29 deletions pymoviedb/pytmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -116,23 +116,28 @@ 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]:
"""
returns trending movies for time_win (day / week) in json format
"""
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]:
"""
Expand All @@ -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]:
"""
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 ##############################
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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]:
"""
Expand All @@ -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]:
"""
Expand All @@ -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]:
"""
Expand All @@ -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())
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b3f8d56

Please sign in to comment.