Skip to content

Commit 9c8b275

Browse files
committed
initial commit
1 parent 10e9efc commit 9c8b275

File tree

6 files changed

+217
-0
lines changed

6 files changed

+217
-0
lines changed

omdb/__init__.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
from .omdb import OMDB
3+
from .exceptions import OMDBException, OMDBNoResults, OMDBLimitReached
4+
5+
__author__ = 'Tyler Barrus'
6+
__maintainer__ = 'Tyler Barrus'
7+
__email__ = '[email protected]'
8+
__license__ = 'MIT'
9+
__version__ = '0.0.1'
10+
__url__ = 'https://github.com/barrust/pyomdbapi'
11+
__bugtrack_url__ = '{0}/issues'.format(__url__)
12+
__all__ = ['OMDB', 'OMDBException', 'OMDBNoResults', 'OMDBLimitReached']

omdb/exceptions.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
3+
class OMDBException(Exception):
4+
5+
def __init__(self, message):
6+
self._message = message
7+
super(OMDBException, self).__init__(self.message)
8+
9+
@property
10+
def message(self):
11+
''' str: The MediaWiki exception message '''
12+
return self._message
13+
14+
15+
class OMDBNoResults(OMDBException):
16+
17+
def __init__(self, error, params):
18+
self._params = params
19+
self._error = error
20+
super(OMDBNoResults, self).__init__('message: {}\tparams: {}'.format(self._error, self._params))
21+
22+
@property
23+
def error(self):
24+
''' str: The MediaWiki exception message '''
25+
return self._message
26+
27+
@property
28+
def params(self):
29+
''' str: The MediaWiki exception message '''
30+
return self._params
31+
32+
33+
class OMDBLimitReached(OMDBException):
34+
35+
def __init__(self, api_key):
36+
self._api_key = api_key
37+
super(OMDBLimitReached, self).__init__('Limit reached for API Key: {}'.format(self._api_key))
38+
39+
@property
40+
def api_key(self):
41+
''' str: The MediaWiki exception message '''
42+
return self._api_key

omdb/omdb.py

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import requests
2+
3+
from .exceptions import OMDBException, OMDBNoResults, OMDBLimitReached
4+
5+
6+
class OMDB(object):
7+
8+
def __init__(self, api_key, timeout=5):
9+
self._api_url = 'https://www.omdbapi.com/'
10+
self._timeout = timeout
11+
self._api_key = api_key
12+
self._session = requests.Session()
13+
14+
def close(self):
15+
if self._session:
16+
self._session.close()
17+
self._session = None
18+
19+
def search(self, title, **params):
20+
prms = {
21+
's': title,
22+
'apikey': self._api_key
23+
}
24+
prms.update(params)
25+
26+
data = self._get_response(prms)
27+
return self.__format_results(data, prms)
28+
29+
def get(self, title=None, imdbid=None, **params):
30+
prms = {
31+
'apikey': self._api_key
32+
}
33+
if imdbid:
34+
prms['i'] = imdbid
35+
if title:
36+
prms['t'] = title
37+
else:
38+
raise OMDBException("Either title or imdbid is required!")
39+
40+
prms.update(params)
41+
42+
data = self._get_response(prms)
43+
return self.__format_results(data, prms)
44+
45+
def search_movie(self, title, **params):
46+
prms = {'type': 'movie'}
47+
prms.update(params)
48+
data = self.search(title, **prms)
49+
return self.__format_results(data, prms)
50+
51+
def search_series(self, title, **params):
52+
prms = {'type': 'series'}
53+
prms.update(params)
54+
return self.search(title, **prms)
55+
56+
# def search_episode(self, title, **params):
57+
# prms = {'type': 'episode'}
58+
# prms.update(params)
59+
# return self.search(title, **prms)
60+
61+
def get_movie(self, title=None, imdbid=None, **params):
62+
prms = {'type': 'movie'}
63+
prms.update(params)
64+
return self.get(title, imdbid, **prms)
65+
66+
def get_series(self, title=None, imdbid=None, **params):
67+
prms = {'type': 'series'}
68+
prms.update(params)
69+
return self.get(title, imdbid, **prms)
70+
71+
def get_episode(self, title=None, imdbid=None, season=1, episode=1, **params):
72+
prms = {'type': 'episode'}
73+
if season:
74+
prms['Season'] = season
75+
if episode:
76+
prms['Episode'] = episode
77+
prms.update(params)
78+
return self.get(title, imdbid, **prms)
79+
80+
def get_episodes(self, title=None, imdbid=None, season=1, **params):
81+
return self.get_episode(title, imdbid, season, None, **params)
82+
83+
def _get_response(self, params):
84+
return self._session.get(self._api_url, params=params,
85+
timeout=self._timeout).json(encoding='utf8')
86+
87+
def __format_results(self, res, params):
88+
if not isinstance(res, dict):
89+
raise TypeError('Expecting dict type, recieved type(res)')
90+
91+
keys = res.keys()
92+
for key in keys:
93+
val = res.pop(key)
94+
if isinstance(val, dict):
95+
val = self.format_results(val)
96+
if isinstance(val, list):
97+
tmp = list()
98+
for _, itm in enumerate(val):
99+
if isinstance(itm, dict):
100+
tmp.append(self.__format_results(itm))
101+
else:
102+
tmp.append(itm)
103+
val = tmp
104+
105+
# convert camel case to lowercase
106+
res[key.lower()] = val
107+
108+
if 'response' in res and res['response'] == 'False':
109+
raise OMDBNoResults(res['error'], params)
110+
111+
return res

requirements/python

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

setup.cfg

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
[PEP8]
3+
max-line-length=120
4+
5+
[flake8]
6+
max-line-length=120

setup.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import setuptools
2+
import io
3+
from omdb import (__version__, __author__, __license__, __email__,
4+
__url__, __bugtrack_url__)
5+
6+
KEYWORDS = ['python', 'omdb', 'omdb-api', 'API']
7+
8+
def read_file(filepath):
9+
''' read the file '''
10+
with io.open(filepath, 'r') as filepointer:
11+
res = filepointer.read()
12+
return res
13+
14+
15+
setuptools.setup(
16+
name = 'pyomdbapi', # mediawiki was taken
17+
version = __version__,
18+
author = __author__,
19+
author_email = __email__,
20+
description = 'OMDB API python wrapper',
21+
license = __license__,
22+
keywords = ' '.join(KEYWORDS),
23+
url = __url__,
24+
download_url = '{0}/tarball/v{1}'.format(__url__, __version__),
25+
bugtrack_url = __bugtrack_url__,
26+
install_requires = read_file('./requirements/python').splitlines(),
27+
packages = ['omdb'],
28+
long_description = read_file('README.md'),
29+
classifiers = [
30+
'Development Status :: 3 - Alpha',
31+
'Intended Audience :: Developers',
32+
'Intended Audience :: Information Technology',
33+
'Intended Audience :: Science/Research',
34+
'Topic :: Software Development :: Libraries',
35+
'Topic :: Utilities',
36+
'Topic :: Internet',
37+
'License :: OSI Approved',
38+
'License :: OSI Approved :: MIT License',
39+
'Programming Language :: Python :: 3',
40+
'Programming Language :: Python :: 3.3',
41+
'Programming Language :: Python :: 3.4',
42+
'Programming Language :: Python :: 3.5',
43+
'Programming Language :: Python :: 3.6'
44+
]
45+
)

0 commit comments

Comments
 (0)