-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_movie_data.py
More file actions
41 lines (30 loc) · 988 Bytes
/
get_movie_data.py
File metadata and controls
41 lines (30 loc) · 988 Bytes
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
import sqlite3
import requests
import csv
import re
from simplejson.errors import JSONDecodeError
# create the database
connection = sqlite3.connect("movies.db")
cursor = connection.cursor()
# get API data
file = open('Horror_urls_2_correct.csv')
csv_file = csv.reader(file)
for row in csv_file:
try:
url = re.sub(r'.*http', 'http', row[0])
response = requests.get(url).json()
print(response)
title = response['Title']
genre = response['Genre']
year = response['Year']
rating = response['Rated']
plot = response['Plot']
print("titles is:", title)
print("genres are:", genre)
# title, genre, plot, rated, year
cursor.execute("INSERT INTO movies (title, genre, plot, rated, year) VALUES (?, ?, ?, ?, ?)", (title, genre, plot, rating, year))
connection.commit()
print("Added to database")
except (KeyError, JSONDecodeError):
continue
connection.close()