-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate_ratings.py
100 lines (96 loc) · 3.33 KB
/
populate_ratings.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
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
# 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 rating_top t
using (select style_id from styles ) s
on (t.style_id = s.style_id)
when not matched then
insert (style_id, grade, summary)
values (s.style_id,'Z','??')
""")
db.commit()
cr.execute("""select style_id from rating_top where grade='Z' """)
style_tup=cr.fetchall()
v_api='https://api.edmunds.com/api/vehicle/v2/styles/'
a_key='TopSecret'
def insert_rating(style_id,title,grade,score,summary):
"rating info insert into table"
insert_values=[style_id,title,grade,score,summary]
print insert_values
sql_string="""
merge into ratings e
using (
select '""" + style_id + """' style_id,
'""" + title + """' title,
'""" + grade + """' grade,
'""" + score + """' score,
'""" + summary + """' summary
from dual) d
on (e.style_id = d.style_idi and e.title=d.title)
when matched then
update set e.grade = d.grade,
e.score = d.score,
e.summary= d.summary
when not matched then
insert (style_id,
title,
grade,
score,
summary)
values(:1,:2,:3,:4,:5)
"""
cr.execute(sql_string,insert_values)
db.commit()
return ;
for style in style_tup:
style_id=str(style[0])
grade='Z'
summary='??'
print style_id
grade_url=v_api + style_id + '?fmt=json&api_key=' + a_key
print grade_url
grade_info=json.load(urllib2.urlopen(grade_url))
print json.dumps(grade_info,indent=4)
try:
grade=grade_info['grade']
print grade
summary=grade_info['summary']
insert_values=[style_id, grade, summary]
print insert_values
sql_string="""
merge into rating_top e
using (
select '""" + style_id + """' style_id,
'""" + grade + """' grade,
'""" + summary + """' summary
from dual) d
on (e.style_id = d.style_id)
when matched then
update set e.grade = d.grade,
e.summary= d.summary
when not matched then
insert (style_id,
grade,
summary)
values(:1,:2,:3)
"""
cr.execute(sql_string,insert_values)
db.commit()
for category in grade_info['ratings']:
insert_rating(style_id,category['title'],category['grade'],category['score'],category['summary'])
for rate_detail in category['subRatings']:
insert_rating(style_id,rate_detail['title'],rate_detail['grade'],rate_detail['score'],rate_detail['summary'])
sleep(random()/45)
db.close()