-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql_miner.py
180 lines (157 loc) · 7.37 KB
/
sql_miner.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import pymysql.cursors
"""
PREREQUISITE:
CREATE TABLE deal_detail(
id INT(11) UNSIGNED AUTO_INCREMENT,
name TEXT,
time TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
exp_date TEXT,
orig_price TEXT,
sale_price TEXT,
description TEXT,
short_description TEXT,
fine_print TEXT,
address TEXT,
city TEXT,
href TEXT,
yelp_info TEXT,
opt_count INT(30),
opt_number TEXT,
parent_ID TEXT,
PRIMARY KEY (id)
);
CREATE TABLE deals(
id INT(11) UNSIGNED AUTO_INCREMENT,
href TEXT,
item_id TEXT,
opt_number TEXT,
bought_count TEXT,
temp_price TEXT,
groupon_rating TEXT,
facebook_count TEXT,
twitter_count TEXT,
sold_out INT(2),
expired INT(2),
alive INT(2),
time DATETIME DEFAULT CURRENT_TIMESTAMP,
primary key (id)
);
drop table deals;
drop table deal_detail;
truncate table deals;
truncate table deal_detail;
"""
class sql_miner:
deal_data = {}
connection = None
def __init__(self, datamine):
global deal_data
global connection
deal_data = datamine
connection = self.connect()
def connect(self):
# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
password='',
db='', #
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
return connection
def insert_single(self):
try:
with connection.cursor() as cursor:
sql = "INSERT INTO `deal_detail` (`name`, `exp_date`, `orig_price`, `sale_price`, `description`, `short_description`, `fine_print`, `address`, `city`, `href`, `yelp_info`, `opt_count`, `opt_number`, `parent_ID`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql,
(deal_data['name'], deal_data['exp_date'], deal_data['orig_price'], deal_data['sale_price'],
deal_data['description'], deal_data['short_description'], deal_data['fine_print'],
deal_data['address'], deal_data['city'], deal_data['href'], deal_data['yelp_info'],
deal_data['opt_count'], deal_data['opt_number'], deal_data['parent_ID']))
connection.commit()
except Exception as e:
print(e)
pass
finally:
connection.close()
def insert_single_price(self):
try:
with connection.cursor() as cursor:
sql = "SELECT id FROM `deal_detail` WHERE `href` = %s AND `parent_ID` = %s"
cursor.execute(sql, (deal_data['href'], 0))
item_id = cursor.fetchall()[0]['id']
sql = "INSERT INTO `deals` (`href`, `item_id`, `opt_number`, `bought_count`, `temp_price`, `groupon_rating`, `facebook_count`, `twitter_count`, `sold_out`, `expired`, `alive`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql, (deal_data['href'], item_id, deal_data['opt_number'], deal_data['bought_count'], deal_data['temp_price'], deal_data['groupon_rating'],
deal_data['facebook_count'], deal_data['twitter_count'], deal_data['sold_out'], deal_data['expired'], deal_data['alive']))
connection.commit()
except Exception as e:
print(e)
pass
finally:
connection.close()
def insert_option(self):
# Precondition: Parent Deal has been inserted already.
try:
with connection.cursor() as cursor:
sql = "SELECT id FROM `deal_detail` WHERE `href` = %s" #load parent ID
cursor.execute(sql, (deal_data['href']))
parent_ID = cursor.fetchall()[0]['id']
sql = "INSERT INTO `deal_detail` (`name`, `exp_date`, `orig_price`, `sale_price`, `description`, `short_description`, `fine_print`, `address`, `city`, `href`, `yelp_info`, `opt_count`, `opt_number`, `parent_ID`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql,
(deal_data['name'], deal_data['exp_date'], deal_data['orig_price'], deal_data['sale_price'],
deal_data['description'], deal_data['short_description'], deal_data['fine_print'],
deal_data['address'], deal_data['city'], deal_data['href'], deal_data['yelp_info'],
deal_data['opt_count'], deal_data['opt_number'], parent_ID))
connection.commit()
except Exception as e:
print(e)
pass
finally:
connection.close()
def insert_option_price(self):
try:
with connection.cursor() as cursor:
sql = "SELECT id FROM `deal_detail` WHERE `href` = %s AND `opt_number` = %s"
cursor.execute(sql, (deal_data['href'], deal_data['opt_number']))
item_id = cursor.fetchall()[0]['id']
sql = "INSERT INTO `deals` (`href`, `item_id`, `opt_number`, `bought_count`, `temp_price`, `groupon_rating`, `facebook_count`, `twitter_count`, `sold_out`, `expired`, `alive`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(sql, (deal_data['href'], item_id, deal_data['opt_number'], deal_data['bought_count'], deal_data['temp_price'], deal_data['groupon_rating'],
deal_data['facebook_count'], deal_data['twitter_count'], deal_data['sold_out'], deal_data['expired'], deal_data['alive']))
connection.commit()
except Exception as e:
print(e)
pass
finally:
connection.close()
def read(self):
print("reading from database...")
try:
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT * FROM `deal_detail`" # WHERE `email`=%s"
cursor.execute(sql)
result = cursor.fetchall()
print(result)
#result = cursor.fetchone()
#while result != None:
# print(result['password'])
# result = cursor.fetchone()
except Exception as e:
print(e)
pass
finally:
connection.close()
def display(self):
print(deal_data['name'])
print(deal_data['exp_date'])
print(deal_data['orig_price'], deal_data['sale_price'])
print(deal_data['description'])
print(deal_data['mobile_description'])
print(deal_data['fine_print'])
print(deal_data['address'])
print(deal_data['city'])
print(deal_data['href'])
print(deal_data['yelp_info'])
print(deal_data['opt_count'])
print(deal_data['bought_count'])
print(deal_data['temp_price'])
print(deal_data['groupon_rating'], deal_data['facebook_count'], deal_data['twitter_count'], deal_data['sold_out'], deal_data['expired'])