-
Notifications
You must be signed in to change notification settings - Fork 0
/
posters.py
60 lines (47 loc) · 1.77 KB
/
posters.py
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
import pandas as pd
import requests
import os
from PIL import Image
import time
f = open("api_key.txt", "r")
api = f.read()
start_time = time.perf_counter() # Capturing start_time
file_path = "file_path"
# Read the CSV file into a DataFrame
df = pd.read_csv(file_path, dtype={10: str})
# Function to validate image
def is_valid_image(file_path):
try:
img = Image.open(file_path)
img.verify()
return True
except (IOError, SyntaxError) as e:
print(f"Invalid image: {file_path} - {e}")
return False
# Function to download movie posters
def download_poster(id, poster_path, api_key, download_dir='posters/'):
url = f"https://image.tmdb.org/t/p/original/{poster_path}"
response = requests.get(url)
if response.status_code == 200:
# Successful download
os.makedirs(os.path.join(download_dir, str(id)), exist_ok=True)
file_path = os.path.join(download_dir, str(id), f"{id}.jpg") # Save in folder named after movie_id
with open(file_path, 'wb') as f:
f.write(response.content)
if is_valid_image(file_path):
return "Yes"
else:
os.remove(file_path)
return "No"
else:
# Failed to download
return "No"
api_key = api
# Download posters and save results
df['download_successful'] = df.apply(lambda row: download_poster(row['id'], row['poster_path'], api_key), axis=1)
output_file_path = 'movie_id_poster.csv'
df.to_csv(output_file_path, index=False)
print(f"Download complete. Results saved to {output_file_path}.")
end_time = time.perf_counter() # Capturing end_time
elapsed_time = end_time - start_time # To calculate the time taken to run this program
print(f"Time:{elapsed_time // 3600} hours, {elapsed_time % 60} minutes")