-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessAllToMySql.py
266 lines (241 loc) · 9.92 KB
/
ProcessAllToMySql.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
263
264
import ExtractInfo
import MySQLdb
f = open('NewAllGamesUrls.txt', 'r')
list = f.readlines()
start=33561
urlToParseList=list[start:33562]
#urlToParseList=list[start:15]
label=['Title', 'Publish By', 'Developed By', 'Released', 'Platform', 'Genre', 'Perspective', 'Sport', 'Non Sport', 'Misc', 'Rank', 'Score', 'Alternate Titles', 'Part of the Groups', 'The Press Says ','Release Info']
conn = MySQLdb.connect (host = "localhost",
user = "root",
passwd = "melinda",
db = "newMobygames")
cursor = conn.cursor()
idx=start+1
pltfIdx=0
for i in urlToParseList:
print "%s %s" %(idx, i)
o = ExtractInfo.GameInfo(i)
d = ExtractInfo.ReleaseInfo(i)
g = ExtractInfo.MobyRankScore(i)
rs = ExtractInfo.RatingSystems(i)
#Main page
query1="insert into Games(id, title, releaseDate) values("+str(idx)+", '"+o.getTitle()[0].replace("'","''")+"', '"+o.getGameRelease()['released'][0]+"');"
try:
cursor.execute(query1)
except :
print("Info: Exception query1: ")
print (query1)
for j in o.getGameRelease()['platform']:
sel="select id from Platform where name='"+j.replace("'","''")+"';"
cursor.execute(sel)
newPltfId=cursor.fetchone()
if(newPltfId is None):
query2="insert into Platform(name) values('"+j.replace("'","''")+"');"
try:
cursor.execute(query2)
newPltfId = pltfIdx+1
pltfIdx=pltfIdx+1
except :
print("Info: Exception query2: ")
print (query2)
else:
#when the fetchone return someting it will be in a form of a tale, as ex: (3L,)
#we must take only the number so at the position 0
newPltfId = newPltfId[0]
if(j in o.getMobyRankScore().keys()):
query3="insert into GamePlatformRankScore(gameId, platformId, rank, score) values("+str(idx)+","+str(newPltfId)+","+str(o.getMobyRankScore()[j][0].replace("...", "NULL"))+","+str(o.getMobyRankScore()[j][1].replace("...", "NULL"))+");"
else:
query3="insert into GamePlatformRankScore(gameId, platformId, rank, score) values("+str(idx)+","+str(newPltfId)+",NULL, NULL);"
try:
cursor.execute(query3)
except:
print("Info: Exception query3: ")
print(query3)
for k in o.getGameRelease()['publishBy']:
query4="insert into GamePublish(gameId, publish) values("+str(idx)+",'"+k.replace("'","''")+"');"
try:
cursor.execute(query4)
except:
print("Info: Exception query4: ")
print(query4)
for k in o.getGameRelease()['developedBy']:
query5="insert into GameDeveloped(gameId, developed) values("+str(idx)+",'"+k.replace("'","''")+"');"
try:
cursor.execute(query5)
except:
print("Info: Exception query5: ")
print(query5)
for k in o.getGameGenre()['genre']:
query6="insert into GameGenre(gameId, genre) values("+str(idx)+",'"+k.replace("'","''")+"');"
try:
cursor.execute(query6)
except:
print("Info: Exception query6: ")
print(query6)
for k in o.getGameGenre()['perspective']:
query7="insert into GamePerspective(gameId, perspective) values("+str(idx)+",'"+k.replace("'","''")+"');"
try:
cursor.execute(query7)
except:
print("Info: Exception query7: ")
print(query7)
for k in o.getGameGenre()['sport']:
query8="insert into GameSport(gameId, sport) values("+str(idx)+",'"+k.replace("'","''")+"');"
try:
cursor.execute(query8)
except:
print("Info: Exception query8: ")
print(query8)
for k in o.getGameGenre()['nonsport']:
query9="insert into GameNonSport(gameId, nonsport) values("+str(idx)+",'"+k.replace("'","''")+"');"
try:
cursor.execute(query9)
except:
print("Info: Exception query9: ")
print(query9)
for k in o.getGameGenre()['misc']:
query10="insert into GameMisc(gameId, misc) values("+str(idx)+",'"+k.replace("'","''")+"');"
try:
cursor.execute(query10)
except:
print("Info: Exception query10: ")
print (query10)
for k in o.getAllMainInfo()['altTitles']:
query11="insert into GameAlternateTitle(gameId, altTitle) values("+str(idx)+",'"+(unicode(k, errors='replace').encode('ascii', 'replace').replace('?', ' ')).replace("'","''")+"');"
#print query11
try:
cursor.execute(query11)
except:
print("Info: Exception query11: ")
print (query11)
for k in o.getAllMainInfo()['partGroup']:
query12="insert into GamePartOfGroups(gameId, partGroup) values("+str(idx)+",'"+k.replace("'","''")+"');"
try:
cursor.execute(query12)
except:
print("Info: Exception query12: ")
print (query12)
for k in o.getAllMainInfo()['pressSays']:
sel="select id from Platform where name='"+k[1].replace("'","''")+"';"
cursor.execute(sel)
id=cursor.fetchone()
if(id is None):
query13="insert into GamePressSaysMain(gameId, platformId, press, score) values("+str(idx)+",NULL,'"+k[0].replace("'","''")+"',"+k[2]+");"
else:
query13="insert into GamePressSaysMain(gameId, platformId, press, score) values("+str(idx)+","+str(id[0])+",'"+k[0].replace("'","''")+"',"+k[2]+");"
try:
cursor.execute(query13)
except:
print("Info: Exception query13: ")
print (query13)
#Release Info
dic = d.getReleaseInfo()
for pltf in dic.keys():
sel="select id from Platform where name='"+pltf.replace("'","''")+"';"
cursor.execute(sel)
id=cursor.fetchone()
if(id is None):
print "ERROR! No platform found to insert ReleseInfo query14!!!!"
else:
list=dic[pltf]
for l in list:
r=l['Release']
c=l['Country']
d=l['DevelopedBy']
p=l['PublishBy']
if (len(r) != len(c)):
print("ERROR! Country and Release have different length - query14!!")
elif(len(d)>1):
print("ERROR! DevelopedBy have more then one value - query14!!!")
elif(len(p)>1):
print("ERROR! PublishBy have more then one value - query14!!!")
else:
x=0
while x < len(r):
if (len(d)==0):
dev="NULL"
else:
dev=d[0].replace("'","''")
if(len(p)==0):
pub="NULL"
else:
pub=p[0].replace("'","''")
query14 = "insert into GameReleaseInfo(gameId, platformId, publish, developed, country, releaseDate) values("+str(idx)+","+str(id[0])+",'"+pub+"','"+dev+"','"+c[x].replace("'","''")+"','"+r[x].replace("'","''")+"');"
try:
cursor.execute(query14)
except :
print "INFO: Same Pltf, Pub, Dev, Release, Country twice!!"
x=x+1
#Rating system
rate=rs.getRatings();
for j in rate.keys():
sel="select * from Platform where name='"+j.replace("'","''")+"';"
cursor.execute(sel)
id=cursor.fetchone()
if(id is None):
print "ERROR! No platform found to insert Rating query15!!!!"
else:
for s in rate[j].keys():
query15="insert into GameRatingSys(gameId, platformId, system, value) values("+str(idx)+","+str(id[0])+",'"+s.replace("'","''")+"','"+rate[j][s]+"');"
try:
cursor.execute(query15)
except :
print "INFO: Exception query15!!"
print query15
#Moby Rank Score Press
rspress=g.getMobyRankScorePress();
for j in rspress.keys():
if(j[1] != ''):
sel="select * from Platform where name='"+j[1].replace("'","''")+"';"
cursor.execute(sel)
id=cursor.fetchone()
if(id is None):
print "ERROR! No platform found to insert Rating query16!!!!"
else:
query16="insert into GameRankScorePress(gameId, platformId, press, rankscore) values("+str(idx)+","+str(id[0])+",'"+j[0].replace("'","''")+"','"+rspress[j][0]+"');"
else:
query16="insert into GameRankScorePress(gameId, platformId, press, rankscore) values("+ str(idx)+","+"NULL"+",'"+j[0].replace("'","''")+"','"+rspress[j][0]+"');"
try:
cursor.execute(query16)
except:
print "INFO: Exception query16!!"
print query16
#Moby Rank Score Users
rsusers=g.getMobyRankScoreUsers();
if(len(rsusers.keys()) == 1):
j=rsusers.keys()[0];
if(j=='Platform'):
for k in rsusers[j]:
sel="select * from Platform where name='"+k[0].replace("'","''")+"';"
cursor.execute(sel)
id=cursor.fetchone()
if(id is None):
print "ERROR! No platform found to insert Rating query17!!!!"
else:
query17="insert into GameRankScoreUsers(gameId, platformId, category, score) values("+str(idx)+","+str(id[0])+",'"+"NULL"+"','"+k[1]+"');"
try:
cursor.execute(query17)
except:
print "INFO: Exception query17!!"
print query17
elif(j=='Category'):
sel="select platformId from GameReleaseInfo where gameId="+ str(idx) +";"
cursor.execute(sel)
id=cursor.fetchone()
if(id is None):
print "ERROR! No platform found in GamePlatformRankScore to insert Rating query17!!!!"
id[0]='NULL'
for k in rsusers[j]:
query17="insert into GameRankScoreUsers(gameId, platformId, category, score) values("+str(idx)+","+str(id[0])+",'"+k[0].replace("'","''")+"','"+k[1]+"');"
try:
cursor.execute(query17)
except:
print "INFO: Exception query17!!"
print query17
else:
print "Info! no moby rank score from users found query17!"
idx=idx+1
cursor.close()
conn.close()
#w.writerow([o.getTitle()[0]]+[concatRelease(o,'publishBy')]+ [concatRelease(o,'developedBy')] + [concatRelease(o,'released')] + [concatRelease(o,'platform')] + [concatGenre(o,'genre')]+ [concatGenre(o,'perspective')]+ [concatGenre(o,'sport')]+ [concatGenre(o,'nonsport')]+ [concatGenre(o,'misc')]+[concatMobyRankScore(o,'rank')]+[concatMobyRankScore(o,'score')]+[concatAllMainInfo(o,'altTitles')]+[concatAllMainInfo(o,'partGroup')]+[concatAllMainInfo(o,'pressSays')]+[concatAllReleaseInfo(d)])