Reputation: 53
Hi I am trying to use the simpletmdb python wrapper for 'The Movie Database' API and I can't get passed this problem.
When I try to create the objects and call the method for getting movie info I keep getting this error.
in info
response = TMDB._request('GET', path, params)
TypeError: unbound method _request() must be called with TMDB instance as first argument (got str instance instead)
My code for calling it is:
from tmdbsimple import TMDB
tmdb = TMDB('API_KEY')
movie = tmdb.Movies(603)
response = movie.info()
print movie.title
And the neccessary parts of the simpletmdb wrapper are, the Movies class is a subclass of TMDB:
class TMDB:
def __init__(self, api_key, version=3):
TMDB.api_key = str(api_key)
TMDB.url = 'https://api.themoviedb.org' + '/' + str(version)
def _request(method, path, params={}, json_body={}):
url = TMDB.url + '/' + path + '?api_key=' + TMDB.api_key
if method == 'GET':
headers = {'Accept': 'application/json'}
content = requests.get(url, params=params, headers=headers).content
elif method == 'POST':
for key in params.keys():
url += '&' + key + '=' + params[key]
headers = {'Content-Type': 'application/json', \
'Accept': 'application/json'}
content = requests.post(url, data=json.dumps(json_body), \
headers=headers).content
elif method == 'DELETE':
for key in params.keys():
url += '&' + key + '=' + params[key]
headers = {'Content-Type': 'application/json', \
'Accept': 'application/json'}
content = requests.delete(url, data=json.dumps(json_body), \
headers=headers).content
else:
raise Exception('method: ' + method + ' not supported.')
response = json.loads(content.decode('utf-8'))
return response
#
# Set attributes to dictionary values.
# - e.g.
# >>> tmdb = TMDB()
# >>> movie = tmdb.Movie(103332)
# >>> response = movie.info()
# >>> movie.title # instead of response['title']
class Movies:
""" """
def __init__(self, id=0):
self.id = id
# optional parameters: language
def info(self, params={}):
path = 'movie' + '/' + str(self.id)
response = TMDB._request('GET', path, params)
TMDB._set_attrs_to_values(self, response)
return response
The Wrapper can be found on here https://github.com/celiao/tmdbsimple I am just trying to follow the example found there.
Any help would be great!
Upvotes: 2
Views: 4942
Reputation: 56
As suggested by @qazwsxpawel on Github, @staticmethod decorators have been added to the TMDB class methods _request and _set_attrs_to_values. If you upgrade your version of tmdbsimple, the examples should work in Python 2.7 now. https://pypi.python.org/pypi/tmdbsimple
Upvotes: 2
Reputation: 574
This looks like a simple typo. Change:
def _request(method, path, params={}, json_body={}):
To this:
def _request(self, method, path, params={}, json_body={}):
Upvotes: 0
Reputation: 43128
It may have to do with your indentation of the method _request
. Try this code:
class TMDB:
def __init__(self, api_key, version=3):
TMDB.api_key = str(api_key)
TMDB.url = 'https://api.themoviedb.org' + '/' + str(version)
def _request(method, path, params={}, json_body={}):
url = TMDB.url + '/' + path + '?api_key=' + TMDB.api_key
if method == 'GET':
headers = {'Accept': 'application/json'}
content = requests.get(url, params=params, headers=headers).content
elif method == 'POST':
for key in params.keys():
url += '&' + key + '=' + params[key]
headers = {'Content-Type': 'application/json', \
'Accept': 'application/json'}
content = requests.post(url, data=json.dumps(json_body), \
headers=headers).content
elif method == 'DELETE':
for key in params.keys():
url += '&' + key + '=' + params[key]
headers = {'Content-Type': 'application/json', \
'Accept': 'application/json'}
content = requests.delete(url, data=json.dumps(json_body), \
headers=headers).content
else:
raise Exception('method: ' + method + ' not supported.')
response = json.loads(content.decode('utf-8'))
return response
#
# Set attributes to dictionary values.
# - e.g.
# >>> tmdb = TMDB()
# >>> movie = tmdb.Movie(103332)
# >>> response = movie.info()
# >>> movie.title # instead of response['title']
class Movies:
""" """
def __init__(self, id=0):
self.id = id
# optional parameters: language
def info(self, params={}):
path = 'movie' + '/' + str(self.id)
response = TMDB._request('GET', path, params)
TMDB._set_attrs_to_values(self, response)
return response
See this post that explains why single leading underscore methods are not imported when you use from foo import *
syntax
Upvotes: 0