-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: starry69 <[email protected]>
- Loading branch information
1 parent
ee0cfb4
commit 363e31b
Showing
6 changed files
with
318 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,59 @@ | ||
# PyMovieDB | ||
A simple wrapper over themoviedb.org API | ||
<b>A simple python3 wrapper over themoviedb.org API</b> | ||
|
||
[![made-with-python](https://img.shields.io/badge/Made%20with-Python-1f425f.svg)](https://www.python.org/) | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | ||
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) | ||
|
||
## Install from PyPi | ||
```pip install pymoviedb``` | ||
|
||
## Synopsis | ||
```python | ||
>>> from pymoviedb.pytmdb import Movie, TvShows | ||
>>> APIKEY = "you-api-key" | ||
|
||
# Create a Movie object | ||
>>> mv = Movie(APIKEY) | ||
|
||
# search for queries | ||
>>> mv.search("Titanic") | ||
# search using id | ||
>>> mv.searchid(597) | ||
# Get trending movies for "day" or "week" | ||
# by default it's "week" | ||
>>> mv.trending("day") | ||
# Get recommendations by id | ||
>>> mv.recommendations(597) | ||
|
||
# Similarly for tvshows | ||
>>> tv = TvShows(APIKEY) | ||
>>> tv.search("Breaking bad") | ||
|
||
# All methods can be found by using dir() function | ||
print(dir(Movie)) | ||
``` | ||
|
||
|
||
## Handling exceptions | ||
Exceptions can be imported from `pymoviedb.excs` for easy error handling. | ||
Example: | ||
|
||
```python | ||
from pymoviedb.excs import ZeroResultsFound | ||
tv = TvShows(APIKEY) | ||
|
||
try: | ||
print(tv.search("xsxsxsx")) | ||
except ZeroResultsFound: | ||
print("Nothing found!") | ||
``` | ||
|
||
|
||
## See also | ||
TMDB API docs: https://developers.themoviedb.org/3/ | ||
|
||
Remember that this module not support all TMDB API methods yet. | ||
## License | ||
|
||
This module is licensed under the MIT license. See LICENSE file for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2020 Stɑrry Shivɑm | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class TmdbApiError(ValueError): | ||
""" | ||
raised when API response is not `OK 200` | ||
""" | ||
|
||
def __init__(self, message): | ||
super().__init__(message) | ||
|
||
|
||
class ZeroResultsFound(ValueError): | ||
""" | ||
raise when zero results are found | ||
against search query. | ||
""" | ||
|
||
def __init__(self, message): | ||
super().__init__(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2020 Stɑrry Shivɑm | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
|
||
from abc import ABC, abstractmethod | ||
from requests import get | ||
from excs import TmdbApiError, ZeroResultsFound | ||
|
||
|
||
class AbsMovieDB(ABC): | ||
"""Abstract class for tmdb""" | ||
|
||
def __init__(self, api_key): | ||
""" | ||
:param api_key :type str themoviedb API key. | ||
""" | ||
self.api_key = api_key | ||
self.base_url = "https://api.themoviedb.org/3" | ||
|
||
@abstractmethod | ||
def search(self): | ||
pass | ||
|
||
|
||
class Movie(AbsMovieDB): | ||
"""This class handles all movie related tasks""" | ||
|
||
def search(self, query, language="en", page=1, inlude_adult="true"): | ||
""" | ||
search for the query and return data in json format | ||
""" | ||
|
||
payload = { | ||
"api_key": self.api_key, | ||
"language": language, | ||
"query": query, | ||
"page": page, | ||
"inlude_adult": inlude_adult, | ||
} | ||
|
||
resp = get(self.base_url + "/search/movie", params=payload) | ||
|
||
if resp.status_code == 200: | ||
|
||
check_res = resp.json()["results"] | ||
if len(check_res) <= 0: | ||
raise ZeroResultsFound(resp.text) | ||
else: | ||
return resp.json() | ||
else: | ||
raise TmdbApiError(resp.json()) | ||
|
||
def searchid(self, movie_id, language="en", append_to_response="videos"): | ||
""" | ||
returns movie detais for the movie_id in json format | ||
""" | ||
|
||
payload = { | ||
"api_key": self.api_key, | ||
"language": language, | ||
"append_to_response": append_to_response, | ||
} | ||
|
||
resp = get(self.base_url + f"/movie/{movie_id}", params=payload) | ||
|
||
if resp.status_code == 200: | ||
return resp.json() | ||
else: | ||
raise TmdbApiError(resp.json()) | ||
|
||
def recommendations(self, movie_id, language="en", page=1): | ||
""" | ||
returns recommendations data for the movie_id in json format | ||
""" | ||
|
||
payload = {"api_key": self.api_key, "language": language, "page": page} | ||
|
||
resp = get(self.base_url + f"/movie/{movie_id}/recommendations", params=payload) | ||
|
||
if resp.status_code == 200: | ||
return resp.json() | ||
else: | ||
raise TmdbApiError(resp.json()) | ||
|
||
def trending(self, time_win="week"): | ||
""" | ||
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) | ||
|
||
if resp.status_code == 200: | ||
return resp.json() | ||
else: | ||
raise TmdbApiError(resp.json()) | ||
|
||
|
||
class TvShows(AbsMovieDB): | ||
"""This class handles all tv related tasks""" | ||
|
||
def search(self, query, language="en", page=1, inlude_adult="true"): | ||
""" | ||
search for the query and return data in json format | ||
""" | ||
|
||
payload = { | ||
"api_key": self.api_key, | ||
"language": language, | ||
"query": query, | ||
"page": page, | ||
"inlude_adult": inlude_adult, | ||
} | ||
|
||
resp = get(self.base_url + "/search/tv", params=payload) | ||
|
||
if resp.status_code == 200: | ||
|
||
check_res = resp.json()["results"] | ||
if len(check_res) <= 0: | ||
raise ZeroResultsFound(resp.text) | ||
else: | ||
return resp.json() | ||
|
||
else: | ||
raise TmdbApiError(resp.json()) | ||
|
||
def searchid(self, tv_id, language="en", append_to_response="videos"): | ||
""" | ||
returns tv detais for the tv_id in json format | ||
""" | ||
|
||
payload = { | ||
"api_key": self.api_key, | ||
"language": language, | ||
"append_to_response": append_to_response, | ||
} | ||
|
||
resp = get(self.base_url + f"/tv/{tv_id}", params=payload) | ||
|
||
if resp.status_code == 200: | ||
return resp.json() | ||
else: | ||
raise TmdbApiError(resp.json()) | ||
|
||
def recommendations(self, tv_id, language="en", page=1): | ||
""" | ||
returns recommendations data for the tv_id in json format | ||
""" | ||
|
||
payload = {"api_key": self.api_key, "language": language, "page": page} | ||
|
||
resp = get(self.base_url + f"/movie/{tv_id}/recommendations", params=payload) | ||
|
||
if resp.status_code == 200: | ||
return resp.json() | ||
else: | ||
raise TmdbApiError(resp.json()) | ||
|
||
def trending(self, time_win="week"): | ||
""" | ||
returns trending tvshows for time_win (day / week) in json format | ||
""" | ||
payload = {"api_key": self.api_key} | ||
|
||
resp = get(self.base_url + f"/trending/tv/{time_win}", params=payload) | ||
|
||
if resp.status_code == 200: | ||
return resp.json() | ||
else: | ||
raise TmdbApiError(resp.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from setuptools import setup, find_packages | ||
from os import path | ||
|
||
this_dir = path.abspath(path.dirname(__file__)) | ||
with open("README.md", "r") as f: | ||
long_description = f.read() | ||
|
||
setup( | ||
name='pymoviedb', | ||
description='A simple wrapper over themoviedb.org API', | ||
long_description=long_description, | ||
long_description_content_type='text/markdown', | ||
url='https://github.com/starry69/PyMovieDB', | ||
author='Stɑrry Shivɑm', | ||
author_email='[email protected]', | ||
version='1.1', | ||
license='MIT', | ||
packages=find_packages(), | ||
install_requires=['requests'], | ||
python_requires='~=3.5', | ||
keywords='api development tmdb themoviedb wrapper library python3', | ||
classifiers=[ | ||
'Programming Language :: Python :: 3.5', | ||
'Programming Language :: Python :: 3.6', | ||
'Programming Language :: Python :: 3.7', | ||
'Programming Language :: Python :: 3.8', | ||
'License :: OSI Approved :: MIT License', | ||
'Operating System :: OS Independent', | ||
'Intended Audience :: Developers', | ||
'Development Status :: 4 - Beta' | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2020 Stɑrry Shivɑm | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. |