Beispiel #1
0
    def test_movie_model(self):

        movie_object = {
            "imdb_ID": "tt1596363",
            "title": "The Big Short",
            "year": 2015,
            "runtime": "130 min",
            "release_date": "23 Dec 2015",
            "plot":
            """In 2006-2007 a group of investors bet against the US mortgage market. 
                    In their research they discover how flawed and corrupt the market is.""",
            "genre": {
                "genreList": ["Biography", "Comedy", "Drama", "History"]
            },
            "director": {
                "directorList": ["Adam McKay"]
            },
            "writer": {
                "writerList": [
                    "Charles Randolph (screenplay by)",
                    "Adam McKay (screenplay by)",
                    "Michael Lewis (based upon the book by)"
                ]
            },
            "actors": {
                "actorsList": [
                    "Ryan Gosling", "Rudy Eisenzopf", "Casey Groves",
                    "Charlie Talbert"
                ]
            },
            "language": {
                "languageList": ["English"]
            },
            "country": {
                "countryList": ["USA"]
            },
            "awards": "Won 1 Oscar. Another 37 wins & 80 nominations.",
            "imdb_rating": "7.8/10",
            "rotten_tomatoes": "88%",
            "metascore": "81/100",
            "poster_url":
            "https://m.media-amazon.com/images/M/MV5BNDc4MThhN2EtZjMzNC00ZDJmLThiZTgtNThlY2UxZWMzNjdkXkEyXkFqcGdeQXVyNDk3NzU2MTQ@._V1_SX300.jpg",
            "box_office": "N/A"
        }

        movie = Movie(**movie_object)
        db.session.add(movie)

        movieRes = Movie.query.filter_by(imdb_ID="tt1596363").first()
        self.assertTrue(isinstance(movieRes, Movie))
Beispiel #2
0
    def get_total_pages(query):
        try:
            results_per_page = current_app.config['RESULTS_PER_PAGE']
            res, totalResults = Movie.search(query, 1, results_per_page)
            total_pages = int(totalResults) / int(results_per_page) or 1

            return total_pages, 200

        except BaseException:
            LOG.error(
                "Search query was not able to complete. Please try again later", exc_info=True)
            response_object = {
                'status': 'Error',
                'message': 'Failed fetching details. Try later.'
            }

            return response_object, 500
Beispiel #3
0
    def search_for_movie(query, page):
        try:
            print(query)
            # body={'query': {'multi_match': {'query': query, 'fields': "*"}}}

            # r = requests.post("http://localhost:9200/movie/_search", data=body)
            # print(r.status_code)
            results_per_page = current_app.config['RESULTS_PER_PAGE']
            res, totalResults = Movie.search(query, page, results_per_page)
            res_objects = res.all()

            movie_results = [movie for movie in res_objects]
            return movie_results, 200

        except BaseException as e:
            LOG.error(
                "Search query was not able to complete. Please try again later", exc_info=True)
            response_object = {
                'status': 'Error',
                'message': 'Failed fetching details. Try later.'
            }

            return response_object, 500
Beispiel #4
0
    def add_to_seen_list(data):
        try:
            imdb_ID = data.get('imdb_ID_list')
            title = data.get("movie_list")

            if imdb_ID is None or imdb_ID == "":
                res, totalResults = Movie.search(title, 1, 5)
                res_objects = res.all()

                movie = Movie.query.filter_by(
                    imdb_ID=res_objects[0].imdb_ID).first()
                user = User.query.filter_by(id=current_user.id).first()

                if len(user.seen_list_titles) == 0:
                    print("Khali Hai")
                    movie_list = []
                    imdb_ID_list = []

                else:
                    movie_list = user.seen_list_titles['movie_list']
                    imdb_ID_list = user.seen_list_IDs['imdb_ID_list']

                if movie.imdb_ID not in imdb_ID_list:
                    movie_list.append(movie.title)
                    imdb_ID_list.append(movie.imdb_ID)

                    res = {
                        'movie_list': movie_list,
                        'imdb_ID_list': imdb_ID_list
                    }

                    print("RES")
                    print(res)

                    setattr(user, 'seen_list_IDs', {
                            'imdb_ID_list': imdb_ID_list})
                    db.session.commit()
                    setattr(user, 'seen_list_titles', {
                            'movie_list': movie_list})
                    db.session.commit()
                    setattr(user, 'seen_list_IDs', {
                            'imdb_ID_list': imdb_ID_list})
                    db.session.commit()

            else:
                user = User.query.filter_by(id=current_user.id).first()
                movie = Movie.query.filter_by(imdb_ID=imdb_ID).first()

                if len(user.seen_list_titles) == 0:
                    print("Khali Hai")
                    movie_list = []
                    imdb_ID_list = []

                else:
                    movie_list = user.seen_list_titles['movie_list']
                    imdb_ID_list = user.seen_list_IDs['imdb_ID_list']

                print(movie_list)
                print(imdb_ID_list)

                if movie.imdb_ID not in imdb_ID_list:
                    movie_list.append(movie.title)
                    imdb_ID_list.append(movie.imdb_ID)

                    res = {
                        'movie_list': movie_list,
                        'imdb_ID_list': imdb_ID_list
                    }

                    print("RES")
                    print(res)

                    setattr(user, 'seen_list_IDs', {
                            'imdb_ID_list': imdb_ID_list})
                    db.session.commit()
                    setattr(user, 'seen_list_titles', {
                            'movie_list': movie_list})
                    db.session.commit()
                    setattr(user, 'seen_list_IDs', {
                            'imdb_ID_list': imdb_ID_list})
                    db.session.commit()

            user = User.query.filter_by(id=current_user.id).first()

            movie_list = user.seen_list_titles
            return movie_list, 200

        except Exception:
            LOG.error("Movie couldn't be added to List.", exc_info=True)
            response_object = {
                'status': 'fail',
                'message': 'Try again later. '
            }

            return response_object, 500
Beispiel #5
0
    def get_by_imdb_id(imdb_ID):
        try:
            movie = Movie.query.filter_by(imdb_ID=imdb_ID).first()

            if movie is not None:
                print("SEEDHA DB SE UTHAYA, BADA MAZAA AAYA")
                return movie, 200

            apikey = current_app.config['OMDB_API_KEY']
            key_params = {'apikey': apikey, 'i': imdb_ID}
            base_url = "http://www.omdbapi.com"

            request = requests.get(base_url, key_params)

            result_json = request.json()
            result = {}

            result['imdb_ID'] = imdb_ID
            result['title'] = result_json['Title']
            result['year'] = result_json['Year'].strip("-")
            result['release_date'] = result_json.get('Released', '')
            result['runtime'] = result_json.get('Runtime', '')
            result['plot'] = result_json.get('Plot', '')

            genres = result_json['Genre'].split(", ")
            result['genre'] = {"genreList": genres}

            directors = result_json['Director'].split(", ")
            result['director'] = {'directorList': directors}

            writers = result_json['Writer'].split(", ")
            result['writer'] = {'writerList': writers}

            actors = result_json['Actors'].split(", ")
            result['actors'] = {'actorsList': actors}

            languages = result_json['Language'].split(", ")
            result['language'] = {'languageList': languages}

            countries = result_json['Country'].split(", ")
            result['country'] = {'countryList': countries}

            result['awards'] = result_json.get('Awards', '')

            try:
                ratings = result_json['Ratings']
                result['imdb_rating'] = ratings[0]['Value']
                result['rotten_tomatoes'] = ratings[1]['Value']
                result['metascore'] = ratings[2]['Value']
            except Exception:
                result['imdb_rating'] = "N/A"
                result['rotten_tomatoes'] = "N/A"
                result['metascore'] = "N/A"

            result['poster_url'] = result_json.get('Poster', '')
            result['box_office'] = result_json.get('BoxOffice', '')

            movie = Movie(**result)
            return result, 200

        except BaseException:
            LOG.error('Details couldn\'t be fetched for ID: {}'.format(
                imdb_ID), exc_info=True)
            response_object = {
                'status': 'Error',
                'message': 'Failed fetching details. Try later.'
            }

            return response_object, 500