-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
75 lines (53 loc) · 2.74 KB
/
db.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import psycopg2
import os
import urllib.request
def start_db(json_dict={}, execute='create database'):
connection = psycopg2.connect(database = 'test',
user='postgres',
password='inspiron15',
host='localhost',
port='5432')
print('connected')
cur = connection.cursor()
#This query will return None if table named 'test' is not present
cur.execute("SELECT * FROM information_schema.tables WHERE table_name='test';")
print(cur.fetchone())
if execute == 'insert':
if cur.fetchone() is None: #Create table named test if it is not present in db.
cur.execute(
'CREATE TABLE test (id serial PRIMARY KEY, copyright varchar, date date, explanation varchar,hdurl varchar, media_type varchar, title varchar, url varchar, img_file varchar);')
current_dir = os.getcwd()
new_dir = current_dir+'/apod/'
if os.path.isdir(new_dir) == False: #make directory if not already present
os.mkdir(new_dir)
for item in json_dict:
print(os.path.basename(item['hdurl']))
filepath_on_disk = current_dir+'/apod/' + os.path.basename(item['hdurl'])
#don't download if the file already exists on disk
if os.path.isfile(filepath_on_disk) == False:
urllib.request.urlretrieve(item['hdurl'], filepath_on_disk)
item['filepath_on_disk'] = filepath_on_disk
if 'copyright' not in item.keys():
item['copyright'] = 'none'
for key in item.keys():
item[key] = item[key].replace("'", "''")
#this query is used to check if title in api response already exists in the db table.
cur.execute('SELECT title FROM test WHERE title =' + "'" + item['title'] + "'" )
if cur.fetchone() is None: #don't insert row if it already exists.
cur.execute("INSERT INTO test(copyright, date, explanation, hdurl, media_type, title, url, img_file) \n"
"VALUES("
+"'"+ item['copyright'] + "',"
+"'"+ item['date'] + "',"
+"'"+ item['explanation'] + "',"
+"'"+ item['hdurl'] + "',"
+"'"+ item['media_type'] + "',"
+"'"+ item['title'] + "',"
+"'"+ item['url']+"',"
+"'"+ item['filepath_on_disk'] + "');")
connection.commit()
cur.close()
connection.close()
def create_table(dict_keys):
pass
if __name__ == '__main__':
start_db()