Not looking for the “best one” in terms of quality of software but more the most used and popular one, ie. an alternative that’s fleshed out.

TMDB is owned by a for profit company so I’d prefer something else.

  • horse_battery_staple@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    3 days ago

    To use this application, you will need to replace YOUR_API_KEY with your actual OMDB API key.

    Go to www.omdbapi.com  and sign up for an account.
    Create a new user and get an API key.
    Replace YOUR_API_KEY with the value returned in the email you received after signing up for an account.
    Run the application using Python (e.g., python app.py).
    Open your favorite web browser and navigate to http://localhost:5000 .
    

    Here’s how you can use this API:

    Send a GET request to http://localhost:5000/search?q=Movie+Title+You+Want+To+Search+For  to search for movies on the OMDB API.
    
    from flask import Flask, request, jsonify
    import requests
    
    app = Flask(__name__)
    
    # Define the OMDB API URL
    OMDB_API_URL = 'http://www.omdbapi.com'
    
    # Set up a dictionary to store the movie data from the API
    movie_data = {}
    
    @app.route('/search', methods=['GET'])
    def search_movies():
        # Get the query parameter from the request
        query = request.args.get('q')
    
        if not query:
            return jsonify({'error': 'Missing query parameter'}), 400
    
        # Build the OMDB API URL with the query parameter
        url = f'{OMDB_API_URL}?s={query}&apikey=YOUR_API_KEY'
    
        try:
            # Send a GET request to the OMDB API
            response = requests.get(url)
    
            if response.status_code == 200:
                # Parse the JSON response from the OMDB API
                data = response.json()
    
                # Extract the movie title and URL from the API response
                for i in range(1, int(data['totalResults']) + 1):
                    movie_title = data['Search'][i - 1]['Title']
                    movie_url = data['Search'][i - 1]['Year'] + ' ' + data['Search'][i - 1]['Type']
    
                    # Store the movie title and URL in the dictionary
                    movie_data[movie_title] = {'url': movie_url}
    
                return jsonify(movie_data)
            else:
                return jsonify({'error': f'Failed to retrieve movie data. Status code: {response.status_code}'}), response.status_code
        except requests.RequestException as e:
            return jsonify({'error': f'Request error: {e}'}), 500
    
    @app.route('/movie/<string:title>', methods=['GET'])
    def get_movie_info(title):
        # Get the title from the request path
        title = title.lower()
    
        if not title:
            return jsonify({'error': 'Missing movie title parameter'}), 400
    
        # Check if we have data for this movie in our dictionary
        if title in movie_data:
            movie_url = movie_data[title]['url']
    
            try:
                # Send a GET request to the OMDB API with the URL of the movie
                response = requests.get(movie_url)
    
                if response.status_code == 200:
                    # Parse the JSON response from the OMDB API
                    data = response.json()
    
                    # Extract the movie title, genre, release date, and runtime from the API response
                    try:
                        movie_title = data['Title']
                        movie_genre = data['Genre'].replace(",", ", ")
                        movie_release_date = data['ReleaseDate']
                        movie_runtime = data['Runtime']
    
                        return jsonify({
                            'title': movie_title,
                            'genre': movie_genre,
                            'release_date': movie_release_date,
                            'runtime': movie_runtime
                        })
                    except KeyError:
                        return jsonify({'error': f'Missing movie information for {movie_title}'}), 404
                else:
                    return jsonify({'error': f'Failed to retrieve movie data. Status code: {response.status_code}'}), response.status_code
            except requests.RequestException as e:
                return jsonify({'error': f'Request error: {e}'}), 500
        else:
            return jsonify({'error': f'Movie not found: {title}'}), 404
    
    if __name__ == '__main__':
        app.run(debug=True)