Exemple #1
0
    def post(self, request, *arg, **kwargs ):
        content = request.data
        ia = Imdb()

        try:
            #pass in imdb movie id to get movie details
            data = content['movieId']
            try:
                movie = ia.get_title(data)
                return HttpResponse(json.dumps(movie))
            except ValueError:
                return HttpResponse("Invalid IMDB id", status = 400)
        except KeyError:
            pass




        try:
            #pass int argument between 1-100 to get a list
            #of the top movies right now
            numtop = content['top']
            top100 = ia.get_popular_movies()
            tosend = []

            index = 0

            for m in top100['ranks']:
                index = index + 1
                if index > int(numtop):
                    break
                m_dict = {}
                imdbId =  m['id'][7:16]
                m_dict['imdbId'] = str(imdbId)
                m_dict['title'] = str(m['title'])
                m_dict['posterUrl'] = str(m['image']['url'])
                m_dict['year'] = str(m['year'])
                tosend.append(m_dict)
    
            try:
                return HttpResponse(json.dumps(tosend))
            except ValueError:
                return HttpResponse("ValueError, int between 1-100 plz", status = 400)
        except KeyError:
            pass


        try:
            #pass imdb id to get a list of similar titles
            data = content['similar']
            try:
                similarTitles = ia.get_title_similarities(data)
                return HttpResponse(json.dumps(similarTitles))
            except ValueError:
                return HttpResponse("Invalid IMDB id", status = 400)
        except KeyError:
            return HttpResponse('Request Not Understood.', status = 400)