2

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!

Stacky Mark
  • 53
  • 1
  • 7

3 Answers3

2

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

celiao
  • 56
  • 5
0

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

Community
  • 1
  • 1
smac89
  • 39,374
  • 15
  • 132
  • 179
  • I thought that could be it but it made no difference. I changed all the occurrences of _request with make_request and I still got the same error. – Stacky Mark Dec 12 '13 at 18:52
0

This looks like a simple typo. Change:

    def _request(method, path, params={}, json_body={}):

To this:

    def _request(self, method, path, params={}, json_body={}):
brandonsimpkins
  • 546
  • 1
  • 3
  • 13