|
| 1 | +import streamlit as st |
| 2 | +import requests |
| 3 | +import time |
| 4 | + |
| 5 | +# Your TMDB API key |
| 6 | +API_KEY = "Imdb_api_key" |
| 7 | + |
| 8 | + |
| 9 | +# Function to fetch trending movies from TMDB |
| 10 | +def fetch_trending_movies(): |
| 11 | + url = f"https://api.themoviedb.org/3/trending/movie/week?api_key={API_KEY}" |
| 12 | + response = requests.get(url) |
| 13 | + if response.status_code == 200: |
| 14 | + return response.json()["results"] |
| 15 | + else: |
| 16 | + st.error("Failed to fetch data from TMDB.") |
| 17 | + return [] |
| 18 | + |
| 19 | + |
| 20 | +# Function to display loading spinner |
| 21 | +def show_loading(): |
| 22 | + with st.spinner("Fetching trending movies..."): |
| 23 | + time.sleep(2) # Simulate a delay |
| 24 | + return fetch_trending_movies() |
| 25 | + |
| 26 | + |
| 27 | +# Configure the page |
| 28 | +st.set_page_config( |
| 29 | + page_title="Trending Movies", |
| 30 | + page_icon="🎥", |
| 31 | +) |
| 32 | + |
| 33 | +# Custom CSS for enhanced styling |
| 34 | +st.markdown( |
| 35 | + """ |
| 36 | +<style> |
| 37 | + body { |
| 38 | + background-color: #f4f4f4; |
| 39 | + } |
| 40 | + .movie-card { |
| 41 | + background-color: #ffffff; |
| 42 | + border-radius: 10px; |
| 43 | + box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); |
| 44 | + transition: transform 0.3s ease, box-shadow 0.3s ease; |
| 45 | + margin: 20px; |
| 46 | + padding: 20px; |
| 47 | + text-align: center; |
| 48 | + cursor: pointer; |
| 49 | + } |
| 50 | + .movie-card:hover { |
| 51 | + transform: translateY(-5px); |
| 52 | + box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2); |
| 53 | + } |
| 54 | + .movie-title { |
| 55 | + font-size: 1.5rem; |
| 56 | + font-weight: bold; |
| 57 | + color: #2c3e50; |
| 58 | + margin: 10px 0; |
| 59 | + } |
| 60 | + .movie-overview { |
| 61 | + color: #34495e; |
| 62 | + font-size: 0.9rem; |
| 63 | + height: 60px; /* Fixed height for uniformity */ |
| 64 | + overflow: hidden; /* Hide overflow text */ |
| 65 | + } |
| 66 | + .movie-release { |
| 67 | + color: #7f8c8d; |
| 68 | + font-size: 0.85rem; |
| 69 | + margin-top: 5px; |
| 70 | + } |
| 71 | + .movie-poster { |
| 72 | + border-radius: 10px; |
| 73 | + width: 100%; |
| 74 | + height: auto; |
| 75 | + max-height: 300px; /* Set max height for uniform poster sizes */ |
| 76 | + object-fit: cover; /* Maintain aspect ratio */ |
| 77 | + } |
| 78 | + .loading { |
| 79 | + display: flex; |
| 80 | + justify-content: center; |
| 81 | + align-items: center; |
| 82 | + height: 50px; |
| 83 | + } |
| 84 | +</style> |
| 85 | +""", |
| 86 | + unsafe_allow_html=True, |
| 87 | +) |
| 88 | + |
| 89 | +# Header |
| 90 | +st.markdown( |
| 91 | + """ |
| 92 | + <h1 style="text-align: center; color: #2980b9;">Trending Movies</h1> |
| 93 | + <p style="text-align: center; color: #34495e;">Check out the latest trending movies of the week!</p> |
| 94 | + """, |
| 95 | + unsafe_allow_html=True, |
| 96 | +) |
| 97 | + |
| 98 | +# Fetch trending movies |
| 99 | +trending_movies = show_loading() |
| 100 | + |
| 101 | +# Display the movies |
| 102 | +if trending_movies: |
| 103 | + # Create a container for movie cards |
| 104 | + cols = st.columns(3) # Adjust the number of columns as needed |
| 105 | + for i, movie in enumerate(trending_movies): |
| 106 | + title = movie["title"] |
| 107 | + overview = movie["overview"] |
| 108 | + release_date = movie["release_date"] |
| 109 | + poster_path = movie["poster_path"] |
| 110 | + poster_url = f"https://image.tmdb.org/t/p/w500{poster_path}" |
| 111 | + |
| 112 | + # Create a card for each movie |
| 113 | + with cols[i % 3]: # Cycle through columns |
| 114 | + st.markdown( |
| 115 | + f""" |
| 116 | + <div class="movie-card" onclick="window.open('https://www.themoviedb.org/movie/{movie['id']}', '_blank');"> |
| 117 | + <img src="{poster_url}" alt="{title}" class="movie-poster"/> |
| 118 | + <div class="movie-title">{title}</div> |
| 119 | + <p class="movie-overview">{overview}</p> |
| 120 | + <div class="movie-release">Release Date: {release_date}</div> |
| 121 | + </div> |
| 122 | + """, |
| 123 | + unsafe_allow_html=True, |
| 124 | + ) |
| 125 | +else: |
| 126 | + st.warning("No trending movies available.") |
0 commit comments