-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate_tco.py
64 lines (63 loc) · 1.96 KB
/
populate_tco.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
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
# copyright 2016 Steve Harville
import cx_Oracle
import os
import sys
import json
import urllib2
from random import random
from time import sleep
db = cx_Oracle.connect(os.getenv("CX_ORACLE_USERNAME"),
os.getenv("CX_ORACLE_PASSWORD"),
os.getenv("CX_ORACLE_TNSENTRY"))
print 'Database connection status : ' + str(db)
cr=db.cursor()
print 'Cursor status : ' + str(cr)
cr.execute("""merge into tco t
using (select style_id from styles ) s
on (t.style_id = s.style_id)
when not matched then
insert (style_id, tco)
values (s.style_id,999999999)
""")
cr.execute("""select style_id from tco where tco = 999999999 """)
style_tup=cr.fetchall()
v_api='https://api.edmunds.com/v1/api/tco/'
a_key='TopSecret'
for style in style_tup:
style_id=str(style[0])
tco=999999999
print style_id
print tco
tco_url=v_api + 'newtruecosttoownbystyleidandzip/' + style_id + '/41018' + '?fmt=json&api_key=' + a_key
print tco_url
try:
tco_info=json.load(urllib2.urlopen(tco_url))
except urllib2.HTTPError, error:
contents = error.read()
print contents
if "TCO is not supported for style id" in contents:
continue
print tco_info
tco=tco_info['value']
print tco
insert_values=[style_id, tco]
print insert_values
sql_string="""
merge into tco e
using (
select '""" + style_id + """' style_id,
'""" + str(tco) + """' tco
from dual) d
on (e.style_id = d.style_id)
when matched then
update set e.tco = d.tco
when not matched then
insert (style_id,
tco)
values(:1,:2)
"""
cr.execute(sql_string,insert_values)
db.commit()
sleep(random()/45)
db.close()