-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_load.py
49 lines (37 loc) · 1.34 KB
/
database_load.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
import json
import psycopg2
import requests
from psycopg2.extras import execute_values
class Asteroid:
def __init__(self, json_object):
self.name = json_object.get('name')
self.nametype = json_object.get('nametype')
self.recclass = json_object.get('recclass')
self.mass = json_object.get('mass')
self.fall = json_object.get('fall')
self.year = json_object.get('year')
self.reclat = json_object.get('reclat')
self.reclong = json_object.get('reclong')
#self.geolocation = json_object.get('geolocation') #convert off dict
def serialize(self):
return(json.loads(self.__dict__))
def main():
astroid_strikes = requests.get("https://data.nasa.gov/resource/y77d-th95.json")
cur, conn = connect_db()
listthin = []
for x in astroid_strikes.json():
new_asteroid = Asteroid(x)
vals = tuple(new_asteroid.__dict__.values())
listthin.append(vals)
cur.executemany("INSERT into nasa_data.asteroids (name,nametype,recclass,mass,fall,year,reclat,reclong) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)",listthin)
conn.commit()
conn.close()
def connect_db():
conn = psycopg2.connect(
host="localhost",
database="nasa_db",
user="postgres",
password="darling")
cur = conn.cursor()
return cur, conn
main()