File size: 2,910 Bytes
d997e10
 
c1b881b
65575bf
d997e10
 
 
 
 
 
 
 
 
 
 
 
 
65575bf
d997e10
 
 
 
 
c1b881b
d997e10
 
 
 
 
 
 
 
 
65575bf
d997e10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65575bf
d997e10
 
 
 
 
 
 
 
65575bf
 
d997e10
 
 
 
65575bf
d997e10
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
import pandas as pd
import os
from tqdm import tqdm  # Import tqdm for progress bar

# Load CSV
try:
    df = pd.read_csv('/Users/moizpro/Desktop/MoviesRecommender/MovieRecommender/imdb_movie_codes.csv')
    print("df loaded..")
except FileNotFoundError:
    print("Error: CSV file not found at path")
    exit()

# Define column names for the DataFrame
column_names = [
    "id", "Title", "Year", "Rated", "Runtime", "Genre1", "Genre2", "Genre3",
    "Director", "Writer", "Plot", "Awards", "IMDb", "Rotten Tomatoes",
    "Metascore", "IMDb_votes", "Type", "BoxOffice", "Poster"  # Add Musab, Moiz, Udisha columns for personal rating if needed
]
database = pd.DataFrame(columns=column_names)

# Define function to fetch movie details and save in DataFrame
def get_movie(movie_id):
    api_key = os.getenv("API_KEY")
    url = f"http://www.omdbapi.com/?apikey={api_key}&i={movie_id}&plot=full"
    try:
        response = requests.get(url)
        if response.status_code == 200:
            data = response.json()
            # Check for valid response
            if data.get("Response") == "True":
                # Extract genres
                genres = (data.get("Genre") or "").split(", ")
                genre1, genre2, genre3 = genres[:3] if len(genres) >= 3 else genres + [None] * (3 - len(genres))
                # Append the movie data to the DataFrame
                database.loc[len(database)] = [
                    movie_id,
                    data.get("Title"),
                    data.get("Year"),
                    data.get("Rated"),
                    data.get("Runtime"),
                    genre1, genre2, genre3,
                    data.get("Director"),
                    data.get("Writer"),
                    data.get("Plot"),
                    data.get("Awards"),
                    data.get("imdbRating"),
                    data.get("Ratings")[1]["Value"] if len(data.get("Ratings", [])) > 1 else None,
                    data.get("Metascore"),
                    data.get("imdbVotes"),
                    data.get("Type"),
                    data.get("BoxOffice"),
                    data.get("Poster"),
                ]
            else:
                print(f"Error in API response for movie_id {movie_id}: {data.get('Error')}")
        else:
            print(f"HTTP error {response.status_code} for movie_id {movie_id}")
    except Exception as e:
        print(f"Exception occurred for movie_id {movie_id}: {e}")

# Loop through movie IDs in the CSV and fetch details with a progress bar
for i in tqdm(range(len(df)), desc="Fetching movie details", unit="movie"):
    movie_id = df.iloc[i, 0]
    get_movie(movie_id)

# Save the DataFrame to a CSV file
output_file = '/Users/moizpro/Desktop/MoviesRecommender/MovieRecommender/MovieDatabase.csv'
database.to_csv(output_file, index=False)
print(f"Movie database saved to {output_file}")