Exemple #1
0
def process(input, entities):
    output = {}
    try:
        movie = entities['movie'][0]['value']

        with requests_cache.enabled('movie_cache', backend='sqlite', expire_after=86400):
            # Make a search request to the API to get the movie's TMDb ID
            r = requests.get('http://api.themoviedb.org/3/search/movie', params={
                'api_key': TMDB_API_KEY,
                'query': movie,
                'include_adult': False
            })
            data = r.json()

            assert (len(data['results']) > 0)
            tmdb_id = str(data['results'][0]['id'])

            # Make another request to the API using the movie's TMDb ID to get the movie's IMDb ID
            r = requests.get('https://api.themoviedb.org/3/movie/' + tmdb_id, params={
                'api_key': TMDB_API_KEY,
                'append_to_response': 'videos'
            })
            data = r.json()

        # Fetch movie rating from IMDb
        ia = IMDb()
        imdb_id = data['imdb_id']
        imdb_movie = ia.get_movie(imdb_id[2:])
        imdb_movie.fetch()

        template = TextTemplate('Title: ' + data['title'] +
                                '\nYear: ' + data['release_date'][:4] +
                                '\nIMDb Rating: ' + str(imdb_movie.__dict__['rating']) + ' / 10' +
                                '\nOverview: ' + data['overview'])
        text = template.get_text()
        template = ButtonTemplate(text)
        template.add_web_url('IMDb Link', 'https://www.imdb.com/title/' + data['imdb_id'] + '/')

        videos = data['videos']['results']
        # Append first Trailer URL if one exists
        for video in videos:
            if video['type'] == 'Trailer' and video['site'] == 'YouTube':
                template.add_web_url('YouTube Trailer', YouTubeUtil.get_video_url(video['key']))
                break

        output['input'] = input
        output['output'] = template.get_message()
        output['success'] = True
    except:
        error_message = 'I couldn\'t find that movie.'
        error_message += '\nPlease ask me something else, like:'
        error_message += '\n  - batman movie'
        error_message += '\n  - iron man 2 movie plot'
        error_message += '\n  - What is the rating of happyness movie?'
        output['error_msg'] = TextTemplate(error_message).get_message()
        output['success'] = False
    return output
Exemple #2
0
def process(input, entities):
    output = {}
    try:
        movie = entities['movie'][0]['value']

        with requests_cache.enabled('movie_cache',
                                    backend='sqlite',
                                    expire_after=86400):
            # Make a search request to the API to get the movie's TMDb ID
            r = requests.get('http://api.themoviedb.org/3/search/movie',
                             params={
                                 'api_key': TMDB_API_KEY,
                                 'query': movie,
                                 'include_adult': False
                             })
            data = r.json()

            assert (len(data['results']) > 0)
            tmdb_id = str(data['results'][0]['id'])

            # Make another request to the API using the movie's TMDb ID to get the movie's IMDb ID
            r = requests.get('https://api.themoviedb.org/3/movie/' + tmdb_id,
                             params={
                                 'api_key': TMDB_API_KEY,
                                 'append_to_response': 'videos'
                             })
            data = r.json()

        # Fetch movie rating from IMDb
        ia = IMDb()
        imdb_id = data['imdb_id']
        imdb_movie = ia.get_movie(imdb_id[2:])
        imdb_movie.fetch()

        template = TextTemplate('Title: ' + data['title'] + '\nYear: ' +
                                data['release_date'][:4] + '\nIMDb Rating: ' +
                                str(imdb_movie.__dict__['rating']) + ' / 10' +
                                '\nOverview: ' + data['overview'])
        text = template.get_text()
        template = ButtonTemplate(text)
        template.add_web_url(
            'IMDb Link', 'https://www.imdb.com/title/' + data['imdb_id'] + '/')

        videos = data['videos']['results']
        # Append first Trailer URL if one exists
        for video in videos:
            if video['type'] == 'Trailer' and video['site'] == 'YouTube':
                template.add_web_url('YouTube Trailer',
                                     YouTubeUtil.get_video_url(video['key']))
                break

        output['input'] = input
        output['output'] = template.get_message()
        output['success'] = True
    except:
        error_message = 'I couldn\'t find that movie.'
        error_message += '\nPlease ask me something else, like:'
        error_message += '\n  - batman movie'
        error_message += '\n  - iron man 2 movie plot'
        error_message += '\n  - What is the rating of happyness movie?'
        output['error_msg'] = TextTemplate(error_message).get_message()
        output['success'] = False
    return output
Exemple #3
0
    def populate(self, config):
        from imdbparser import IMDb

        image_cache = config["image_cache"]

        imdb = IMDb()
        movie = imdb.get_movie(self.identifier)
        movie.fetch()

        self.actors.clear()
        self.writers.clear()
        self.directors.clear()
        self.genres.clear()
        self.languages.clear()
        self.countries.clear()

        AlternativeTitle.objects.filter(metadata=self).delete()

        self.title = movie.title
        self.rating = movie.rating
        self.votes = movie.votes
        self.duration = movie.duration
        self.year = movie.year
        self.plot = movie.plot
        self.synopsis = movie.description

        if movie.cover:
            self.cover = movie.cover
            image_cache.get_image_path(self.cover)

        person_target_map = [
            (movie.actors, self.actors),
            (movie.writers, self.writers),
            (movie.directors, self.directors),
        ]

        for persons, target in person_target_map:
            for p in persons:
                person, _ = Person.objects.get_or_create(
                    identifier=p.imdb_id, defaults={"name": p.name})
                if p.name != person.name:
                    person.name = p.name
                    person.save()

                target.add(person)

        for genre in movie.genres:
            genre, _ = Genre.objects.get_or_create(name=genre)
            self.genres.add(genre)

        primary_language = None
        for language in movie.languages:
            language, _ = Language.objects.get_or_create(name=language)
            if not primary_language:
                primary_language = language
            self.languages.add(language)

        self.primary_language = primary_language

        for country in movie.countries:
            country, _ = Country.objects.get_or_create(name=country)
            self.countries.add(country)

        for alt_title in movie.alternative_titles:
            if alt_title:
                AlternativeTitle.objects.create(title=alt_title, metadata=self)