使用TMDB API检索的python对象

时间:2013-02-11 21:43:24

标签: python api object printing themoviedb-api

如何使用TMDB API检索的一些数据?

这是功能:

class Movies(Core):
    def __init__(self, title="", limit=False):
        self.limit = limit
        self.update_configuration()
        title = self.escape(title)
        self.movies = self.getJSON(config['urls']['movie.search'] % (title,str(1)))
        pages = self.movies["total_pages"]
        if not self.limit:
            if int(pages) > 1:                  #
                for i in range(2,int(pages)+1): #  Thanks @tBuLi
                    self.movies["results"].extend(self.getJSON(config['urls']['movie.search'] % (title,str(i)))["results"])

    def __iter__(self):
        for i in self.movies["results"]:
            yield Movie(i["id"])

    def get_total_results(self):
        if self.limit:
            return len(self.movies["results"])
        return self.movies["total_results"]

    def iter_results(self):
        for i in self.movies["results"]:
            yield i

电话:

def search_tmdb(title):
  tmdb.configure(TMDB_KEY)
  movie = tmdb.Movies(title,limit=True)

问题是,我如何看到和使用对象电影的结果?

我很抱歉这个愚蠢的问题,但我现在正在接近python

1 个答案:

答案 0 :(得分:0)

看起来你可以做类似的事情:

movies = tmdb.Movies(title,limit=True)
#if you want to deal with Movie objects
for movieresult in movies:
    #do something with the Movie result (here I'm just printing it)
    print movieresult

#if you want to deal with raw result (not wrapped with a Movie object)
for result in movies.iter_results():
    #do something with the raw result
    print result

tmdb.Movies(title, limit=True)会创建一个Movies对象。由于定义了__iter__方法,因此您可以使用for movie in moviesobject来查看Movies对象中的结果。您还可以使用Movie获取movielist = list(movies)个对象的列表。