Repository navigation
Expand file tree
/
Copy pathscript.py
More file actions
130 lines (109 loc) · 3.96 KB
/
Copy pathscript.py
File metadata and controls
130 lines (109 loc) · 3.96 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import psycopg2
import requests
import csv
import time
import os
# Database connection settings
DB_HOST = 'localhost'
DB_PORT = '5432'
DB_NAME = 'BDP'
DB_USER = 'postgres'
DB_PASSWORD = 'PASSWORD'
# OMDb API settings
OMDB_API_KEY = 'API_KEY'
OMDB_API_URL = 'https://www.omdbapi.com/'
OUTPUT_FILE = 'omdb_api_data.csv'
# Connect to PostgreSQL
conn = psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
database=DB_NAME,
user=DB_USER,
password=DB_PASSWORD
)
cur = conn.cursor()
try:
# Add retrieval_status column if it doesn't already exist
cur.execute("""
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name='netflix_original' AND column_name='retrieval_status'
) THEN
ALTER TABLE public.netflix_original ADD COLUMN retrieval_status TEXT;
END IF;
END $$;
""")
conn.commit()
# Query the imdbID column where retrieval_status is NULL
cur.execute("""
SELECT const FROM public.netflix_original
WHERE const IS NOT NULL AND retrieval_status IS NULL;
""")
imdb_ids = cur.fetchall()
file_exists = os.path.isfile(OUTPUT_FILE)
# Open CSV file in append mode if it exists, otherwise in write mode
with open(OUTPUT_FILE, mode='a' if file_exists else 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
# Write the header row only if the file is new
if not file_exists:
writer.writerow([
'imdbID', 'Title', 'Year', 'Rated', 'Released', 'Runtime', 'Genre',
'Director', 'Writer', 'Actors', 'Plot', 'Language', 'Country',
'Awards', 'Poster', 'Ratings', 'Metascore', 'imdbRating',
'imdbVotes', 'Type', 'totalSeasons', 'Response'
])
# Loop through imdbID values
for row in imdb_ids:
imdb_id = row[0].strip()
if not imdb_id:
continue
# Replace spaces in the imdbID if needed
imdb_id = imdb_id.replace(" ", "+")
# Make a request to OMDb API
response = requests.get(OMDB_API_URL, params={'i': imdb_id, 'apikey': OMDB_API_KEY})
data = response.json()
# Skip if no valid response
if data.get('Response') != 'True':
print(f"Skipping {imdb_id}: {data.get('Error', 'Unknown error')}")
continue
# Write data to CSV
writer.writerow([
data.get('imdbID', ''),
data.get('Title', ''),
data.get('Year', ''),
data.get('Rated', ''),
data.get('Released', ''),
data.get('Runtime', ''),
data.get('Genre', ''),
data.get('Director', ''),
data.get('Writer', ''),
data.get('Actors', ''),
data.get('Plot', ''),
data.get('Language', ''),
data.get('Country', ''),
data.get('Awards', ''),
data.get('Poster', ''),
str(data.get('Ratings', [])),
data.get('Metascore', ''),
data.get('imdbRating', ''),
data.get('imdbVotes', ''),
data.get('Type', ''),
data.get('totalSeasons', ''),
data.get('Response', '')
])
# Mark as success in the database
cur.execute("""
UPDATE public.netflix_original
SET retrieval_status = 'success'
WHERE const = %s;
""", (imdb_id,))
conn.commit()
print(f"Fetched and marked success for {imdb_id}")
# Wait for 1 second to avoid rate-limiting
# time.sleep(1)
finally:
# Close the database connection
cur.close()
conn.close()