Skip to content

Commit

Permalink
First release into git
Browse files Browse the repository at this point in the history
Signed-off-by: starry69 <[email protected]>
  • Loading branch information
starry-shivam committed Sep 6, 2020
1 parent ee0cfb4 commit 363e31b
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 1 deletion.
59 changes: 58 additions & 1 deletion README.md
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.
11 changes: 11 additions & 0 deletions pymoviedb/__init__.py
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.
17 changes: 17 additions & 0 deletions pymoviedb/excs.py
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)
190 changes: 190 additions & 0 deletions pymoviedb/pytmdb.py
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())
31 changes: 31 additions & 0 deletions setup.py
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'
])
11 changes: 11 additions & 0 deletions test/__init__.py
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.

0 comments on commit 363e31b

Please sign in to comment.