|
| 1 | +import streamlit as st |
| 2 | +import requests |
| 3 | + |
| 4 | +# Your TMDB API key |
| 5 | +API_KEY = "your_token" |
| 6 | + |
| 7 | + |
| 8 | +# Function to fetch movies based on a search query |
| 9 | +def search_movies(query): |
| 10 | + url = f"https://api.themoviedb.org/3/search/movie?api_key={API_KEY}&query={query}" |
| 11 | + response = requests.get(url) |
| 12 | + if response.status_code == 200: |
| 13 | + return response.json()["results"] |
| 14 | + else: |
| 15 | + st.error("Failed to fetch data from TMDB.") |
| 16 | + return [] |
| 17 | + |
| 18 | + |
| 19 | +# Function to fetch detailed movie information |
| 20 | +def get_movie_details(movie_id): |
| 21 | + url = f"https://api.themoviedb.org/3/movie/{movie_id}?api_key={API_KEY}" |
| 22 | + response = requests.get(url) |
| 23 | + if response.status_code == 200: |
| 24 | + return response.json() |
| 25 | + else: |
| 26 | + st.error("Failed to fetch movie details.") |
| 27 | + return None |
| 28 | + |
| 29 | + |
| 30 | +# Page configuration |
| 31 | +st.set_page_config( |
| 32 | + page_title="Movie Comparison Tool", |
| 33 | + page_icon="🎬", |
| 34 | + layout="centered", |
| 35 | +) |
| 36 | + |
| 37 | +# Header |
| 38 | +st.markdown( |
| 39 | + "<h1 style='text-align: center;'>Movie Comparison Tool</h1>", unsafe_allow_html=True |
| 40 | +) |
| 41 | +st.write("Compare movies side by side based on ratings, reviews, genres, and more!") |
| 42 | + |
| 43 | +# Search for movies |
| 44 | +movie_query = st.text_input("Search Category") |
| 45 | +if movie_query: |
| 46 | + search_results = search_movies(movie_query) |
| 47 | + |
| 48 | + # Select movies for comparison |
| 49 | + movie_options = {movie["title"]: movie["id"] for movie in search_results} |
| 50 | + selected_movies = st.multiselect( |
| 51 | + "Select up to 2 movies for comparison", |
| 52 | + list(movie_options.keys()), |
| 53 | + max_selections=2, |
| 54 | + ) |
| 55 | + |
| 56 | + if len(selected_movies) == 2: |
| 57 | + movie1 = get_movie_details(movie_options[selected_movies[0]]) |
| 58 | + movie2 = get_movie_details(movie_options[selected_movies[1]]) |
| 59 | + |
| 60 | + if movie1 and movie2: |
| 61 | + # Display side-by-side comparison |
| 62 | + col1, col2 = st.columns(2) |
| 63 | + |
| 64 | + with col1: |
| 65 | + st.image( |
| 66 | + f"https://image.tmdb.org/t/p/w500{movie1['poster_path']}", width=300 |
| 67 | + ) |
| 68 | + st.subheader(movie1["title"]) |
| 69 | + st.write(f"**Release Date:** {movie1['release_date']}") |
| 70 | + st.write(f"**Rating:** {movie1['vote_average']} / 10") |
| 71 | + st.write( |
| 72 | + f"**Genres:** {', '.join([genre['name'] for genre in movie1['genres']])}" |
| 73 | + ) |
| 74 | + st.write(f"**Overview:** {movie1['overview']}") |
| 75 | + |
| 76 | + with col2: |
| 77 | + st.image( |
| 78 | + f"https://image.tmdb.org/t/p/w500{movie2['poster_path']}", width=300 |
| 79 | + ) |
| 80 | + st.subheader(movie2["title"]) |
| 81 | + st.write(f"**Release Date:** {movie2['release_date']}") |
| 82 | + st.write(f"**Rating:** {movie2['vote_average']} / 10") |
| 83 | + st.write( |
| 84 | + f"**Genres:** {', '.join([genre['name'] for genre in movie2['genres']])}" |
| 85 | + ) |
| 86 | + st.write(f"**Overview:** {movie2['overview']}") |
0 commit comments