-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_random_data.py
262 lines (224 loc) · 7.93 KB
/
insert_random_data.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python3
#This script inserts random data to database cisc637
#Use this script after initialize_database.py
import pymysql
import random
import uuid
import hashlib
import time
from random_name_pool import RandomNamePool
from lorem_ipsum import LoremIpsum
import sql
USER_CNT=100
FOLLOW_USER_CNT_MIN=2
FOLLOW_USER_CNT_MAX=4
USER_POST_CNT_MIN=2
USER_POST_CNT_MAX=4
POST_INGREDIENT_CNT_MIN=4
POST_INGREDIENT_CNT_MAX=8
USER_LIKE_POST_CNT_MIN=4
USER_LIKE_POST_CNT_MAX=8
POST_COMMENT_CNT_MIN=2
POST_COMMENT_CNT_MAX=4
TAG_CNT=8
POST_TAG_CNT_MIN=1
POST_TAG_CNT_MAX=4
USER_FOLLOW_TAG_CNT_MIN=2
USER_FOLLOW_TAG_CNT_MAX=4
class UserPool:
def __init__(self):
self.users=[]
def new_user_info(self,user_name,user_email,user_intro):
user={
"user_id":uuid.uuid4().hex,
"user_name":user_name,
"user_email":user_email,
"user_intro":user_intro
}
self.users.append(user)
return user
class PostPool:
def __init__(self):
self.posts=[]
def new_post_info(self,post_title,post_text,post_time_stamp,user_id):
post={
"post_id":uuid.uuid4().hex,
"post_title":post_title,
"post_text":post_text,
"post_time_stamp":post_time_stamp,
"user_id":user_id,
"post_title_img":"/place_holder_img/"+uuid.uuid4().hex
}
self.posts.append(post)
return post
class CommentPool:
def __init__(self):
self.comments=[]
def new_comment_info(self,comment_text,comment_time_stamp,post_id,user_id):
comment={
"comment_id":uuid.uuid4().hex,
"comment_text":comment_text,
"comment_time_stamp":comment_time_stamp,
"post_id":post_id,
"user_id":user_id,
}
self.comments.append(comment)
return comment
class TagPool:
def __init__(self):
self.tags=[]
def new_tag_info(self,tag_name):
tag={
"tag_id":uuid.uuid4().hex,
"tag_name":tag_name
}
self.tags.append(tag)
return tag
random_name_pool=RandomNamePool()
lorem_ipsum=LoremIpsum()
user_pool=UserPool()
post_pool=PostPool()
comment_pool=CommentPool()
tag_pool=TagPool()
###
database_host=input("[database host]")
database_user=input("[database user name]")
database_password=input("[database password]")
conn=pymysql.connect(host=database_host,user=database_user,password=database_password,database="cisc637",autocommit=True)
cursor=conn.cursor()
#user_info
#user_security
for i in range(USER_CNT):
name=random_name_pool.generate_name()
if not name:
break
user=user_pool.new_user_info(
user_name="{first_name} {last_name}".format(first_name=name["first_name"],last_name=name["last_name"]),
user_email="{first_name}.{last_name}@example.com".format(first_name=name["first_name"],last_name=name["last_name"]),
user_intro=lorem_ipsum.generate_sentence(4,8,6)
)
user["user_password"]=hashlib.sha256("PASSWORD".encode("utf-8")).hexdigest()
for i in range(len(user_pool.users)):
user=user_pool.users[i]
cursor.execute(sql.sql_insert_user_info.format(user_id=user["user_id"],user_name=user["user_name"],user_intro=user["user_intro"]))
cursor.execute(sql.sql_insert_user_security.format(user_id=user["user_id"],user_email=user["user_email"],user_password=user["user_password"]))
#user_follow
for i in range(len(user_pool.users)):
user=user_pool.users[i]
follow_user_cnt=random.randint(FOLLOW_USER_CNT_MIN,FOLLOW_USER_CNT_MAX)
follow_user_index_pool=[]
while(True):
if len(follow_user_index_pool)>=follow_user_cnt:
break
follow_user_index=random.randint(0,len(user_pool.users)-1)
if follow_user_index not in follow_user_index_pool:
if follow_user_index!=i:
follow_user_index_pool.append(follow_user_index)
for j in range(len(follow_user_index_pool)):
follow_user=user_pool.users[follow_user_index_pool[j]]
cursor.execute(sql.sql_insert_user_follow.format(user_id=user["user_id"],follow_id=follow_user["user_id"]))
#post_info
for i in range(len(user_pool.users)):
user=user_pool.users[i]
post_cnt=random.randint(USER_POST_CNT_MIN,USER_POST_CNT_MAX)
for j in range(post_cnt):
post_title=lorem_ipsum.generate_sentence(4,8,6)
post_text=lorem_ipsum.generate_paragraph(4,8,8,16,4)
post_time_stamp=int(time.time())
post_pool.new_post_info(
post_title,
post_text,
post_time_stamp,
user["user_id"]
)
for i in range(len(post_pool.posts)):
post=post_pool.posts[i]
cursor.execute(sql.sql_insert_post_info.format(
post_id=post["post_id"],
post_title=post["post_title"],
post_text=post["post_text"],
post_title_img=post["post_title_img"],
post_time_stamp=post["post_time_stamp"],
user_id=post["user_id"],
))
#post_ingredient
for i in range(len(post_pool.posts)):
post=post_pool.posts[i]
ingredient_cnt=random.randint(POST_INGREDIENT_CNT_MIN,POST_INGREDIENT_CNT_MAX)
ingredient_pool=random.sample(lorem_ipsum.lorem_ipsum_words,ingredient_cnt)
for j in range(len(ingredient_pool)):
ingredient=ingredient_pool[j]
cursor.execute(sql.sql_insert_post_ingredient.format(
post_id=post["post_id"],
ingredient_id=uuid.uuid4().hex,
ingredient_text=ingredient.capitalize(),
))
#user_like_post
for i in range(len(user_pool.users)):
user=user_pool.users[i]
like_post_cnt=random.randint(USER_LIKE_POST_CNT_MIN,USER_LIKE_POST_CNT_MAX)
like_psot_sample=random.sample(post_pool.posts,like_post_cnt)
for j in range(len(like_psot_sample)):
post=like_psot_sample[j]
cursor.execute(sql.sql_insert_user_like_post.format(
user_id=user["user_id"],
post_id=post["post_id"]
))
#comment_info
for i in range(len(post_pool.posts)):
post=post_pool.posts[i]
comment_cnt=random.randint(POST_COMMENT_CNT_MIN,POST_COMMENT_CNT_MAX)
for j in range(comment_cnt):
user=user_pool.users[random.randint(0,len(user_pool.users)-1)]
comment_text=lorem_ipsum.generate_paragraph(2,4,8,16,4)
comment_time_stamp=int(time.time())
comment_pool.new_comment_info(
comment_text,
comment_time_stamp,
post["post_id"],
user["user_id"]
)
for i in range(len(comment_pool.comments)):
comment=comment_pool.comments[i]
cursor.execute(sql.sql_insert_comment_info.format(
comment_id=comment["comment_id"],
comment_text=comment["comment_text"],
comment_time_stamp=comment["comment_time_stamp"],
user_id=comment["user_id"],
post_id=comment["post_id"],
))
#tag_info
tag_name_pool=random.sample(lorem_ipsum.lorem_ipsum_words,TAG_CNT)
for i in range(len(tag_name_pool)):
tag_name=tag_name_pool[i]
tag_pool.new_tag_info(tag_name.capitalize())
for i in range(len(tag_pool.tags)):
tag=tag_pool.tags[i]
cursor.execute(sql.sql_insert_tag_info.format(
tag_id=tag["tag_id"],
tag_name=tag["tag_name"]
))
#post_tag
for i in range(len(post_pool.posts)):
post=post_pool.posts[i]
tag_cnt=random.randint(POST_TAG_CNT_MIN,POST_TAG_CNT_MAX)
tag_sample=random.sample(tag_pool.tags,tag_cnt)
for j in range(len(tag_sample)):
tag=tag_sample[j]
cursor.execute(sql.sql_insert_post_tag.format(
post_id=post["post_id"],
tag_id=tag["tag_id"]
))
#user_follow_tag
for i in range(len(user_pool.users)):
user=user_pool.users[i]
tag_cnt=random.randint(USER_FOLLOW_TAG_CNT_MIN,USER_FOLLOW_TAG_CNT_MAX)
tag_sample=random.sample(tag_pool.tags,tag_cnt)
for j in range(len(tag_sample)):
tag=tag_sample[j]
cursor.execute(sql.sql_insert_user_follow_tag.format(
user_id=user["user_id"],
tag_id=tag["tag_id"]
))
cursor.close()
conn.close()