-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathload.py
302 lines (274 loc) · 10.3 KB
/
load.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""
load.py
This script loads the tables for the StreamData RestAPI
======
Copyright 2019 Moy IT Solutions
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import urllib.request
import mysql.connector
import json
import ssl
import appconfig
mydb = mysql.connector.connect(
host=appconfig.db_host,
user=appconfig.db_user,
password=appconfig.db_user_password,
database=appconfig.db_name,
auth_plugin='mysql_native_password'
)
def main():
print('Clearing Staging Tables')
truncateStagingTables()
print('Downloading stream data...')
regions = ['us', 'ca', 'uk', 'au']
for region in regions:
downloadStreamData(region)
loadStagingTables(region)
print('Downloading title database')
downloadTitleIDs()
loadRegions()
loadSites()
loadTitles()
loadMALIds()
loadLinks()
cleanup()
mydb.close()
print("Done")
def downloadStreamData(region):
print('Downloading Stream Data: ' + region)
ssl._create_default_https_context = ssl._create_unverified_context
urllib.request.urlretrieve('https://bcmoe.blob.core.windows.net/assets/'+region+'.json', region + '.json');
def downloadTitleIDs():
print('Downloading Title Ids')
ssl._create_default_https_context = ssl._create_unverified_context
urllib.request.urlretrieve('https://raw.githubusercontent.com/manami-project/anime-offline-database/master/anime-offline-database.json', 'anime-offline-database.json');
def loadMALIds():
print('Loading MyAnimeList title ids')
with open('anime-offline-database.json') as json_file:
titledata = json.load(json_file)
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT titleid, title, mal_id FROM titles WHERE mal_id IS NULL;"
mycursor.execute(sql)
results = mycursor.fetchall()
if len(results) > 0:
for title in results:
for titlerecord in titledata['data']:
comparetitle = title['title']
ntitle = titlerecord['title'];
match = False
if ntitle.upper() == comparetitle.upper():
match = True
else:
for synonym in titlerecord['synonyms']:
if synonym.upper() == comparetitle.upper():
match = True
break
if match:
for source in titlerecord['sources']:
if "myanimelist.net" in source:
tmpstr = source.replace("https://myanimelist.net/anime/", "")
titleidint = int(tmpstr)
updateAddMALID(title["titleid"], titleidint)
break
print('Loading MyAnimeList title ids done')
def truncateStagingTables():
print('Truncating staging tables')
mycursor = mydb.cursor()
sql = "TRUNCATE TABLE staging"
mycursor.execute(sql)
mydb.commit()
def loadStagingTables(region):
print('Loading ' + region + '.json')
with open(region + '.json') as json_file:
data = json.load(json_file)
for show in data['shows']:
mycursor = mydb.cursor()
sql = "INSERT INTO staging (title, streamsitetitle, streamsiteurl, region) VALUES (%s, %s, %s, %s)"
for streamingsite in show["sites"].keys():
if "http://" in show["sites"][streamingsite] or "https://" in show["sites"][streamingsite]:
val = [show["name"], streamingsite, show["sites"][streamingsite],region];
mycursor.execute(sql, val)
mydb.commit()
print('Finished Loading ' + region + '.json')
def loadRegions():
print('Loading regions table')
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT DISTINCT region FROM staging;"
mycursor.execute(sql)
results = mycursor.fetchall()
for result in results:
if checkRegion(result["region"]) == True:
continue
else:
insertcursor = mydb.cursor()
sql = "INSERT INTO region (regionname) VALUES (%s)"
val = [result["region"]]
insertcursor.execute(sql,val)
mydb.commit()
print('Loading regions table done')
def checkRegion(region):
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT id, regionname FROM region WHERE regionname = %s;"
val = [region]
mycursor.execute(sql,val)
results = mycursor.fetchall()
if len(results) > 0:
return True
return False
def loadLinks():
print('Loading links table')
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT * FROM staging;"
mycursor.execute(sql)
results = mycursor.fetchall()
for result in results:
titleid = lookupTitle(result["title"])
siteid = lookupSite(result["streamsitetitle"])
regionid = lookupRegion(result["region"])
url = result["streamsiteurl"]
if titleid == -1 | siteid == -1 | regionid == -1:
continue
else:
if (checkLink(titleid,regionid,siteid)):
updatelink(titleid,url,siteid,regionid)
else:
insertLink(titleid,url,siteid,regionid)
print('Loading links table done')
def insertLink(titleid, url, siteid, regionid):
insertcursor = mydb.cursor()
sql = "INSERT INTO links (titleid, url, siteid, regionid) VALUES (%s, %s, %s, %s)"
val = [titleid, url, siteid, regionid]
insertcursor.execute(sql, val)
mydb.commit()
def updatelink(titleid, url, siteid, regionid):
updatecursor = mydb.cursor()
sql = "UPDATE links SET url = %s WHERE titleid = %s AND siteid = %s AND regionid = %s"
val = [url, titleid, siteid, regionid]
updatecursor.execute(sql,val)
mydb.commit()
def checkLink(titleid, regionid, siteid):
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT id, titleid, regionid, siteid FROM links WHERE titleid = %s AND regionid = %s AND siteid = %s;"
val = [titleid, regionid, siteid]
mycursor.execute(sql,val)
results = mycursor.fetchall()
if len(results) > 0:
return True
return False
def loadTitles():
print('Loading titles table')
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT DISTINCT title FROM staging;"
mycursor.execute(sql)
results = mycursor.fetchall()
for result in results:
if checkTitle(result["title"]) == True:
continue
else:
insertcursor = mydb.cursor()
sql = "INSERT INTO titles (title) VALUES (%s)"
val = [result["title"]]
insertcursor.execute(sql,val)
mydb.commit()
print('Loading titles table done')
def updateAddMALID(id, malid):
updatecursor = mydb.cursor()
sql = "UPDATE titles SET mal_id = %s WHERE titleid = %s"
val = [malid, id]
updatecursor.execute(sql,val)
mydb.commit()
def checkTitle(title):
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT titleid, title FROM titles WHERE title = %s;"
val = [title]
mycursor.execute(sql,val)
results = mycursor.fetchall()
if len(results) > 0:
return True
return False
def loadSites():
print('Loading sites table')
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT DISTINCT streamsitetitle FROM staging;"
mycursor.execute(sql)
results = mycursor.fetchall()
for result in results:
if checkSite(result["streamsitetitle"]) == True:
continue
else:
insertcursor = mydb.cursor()
sql = "INSERT INTO sites (sitename) VALUES (%s)"
val = [result["streamsitetitle"]]
insertcursor.execute(sql,val)
mydb.commit()
def checkSite(sitename):
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT id, sitename FROM sites WHERE sitename = %s;"
val = [sitename]
mycursor.execute(sql,val)
results = mycursor.fetchall()
if len(results) > 0:
return True
return False
def lookupTitle(title):
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT titleid, title FROM titles WHERE title = %s;"
val = [title]
mycursor.execute(sql,val)
results = mycursor.fetchall()
if len(results) > 0:
return results[0]["titleid"]
return -1
def lookupSite(site):
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT id, sitename FROM sites WHERE sitename = %s;"
val = [site]
mycursor.execute(sql,val)
results = mycursor.fetchall()
if len(results) > 0:
return results[0]["id"]
return -1
def lookupRegion(region):
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT id, regionname FROM region WHERE regionname = %s;"
val = [region]
mycursor.execute(sql,val)
results = mycursor.fetchall()
if len(results) > 0:
return results[0]["id"]
return -1
def cleanup():
print('Performing cleanup')
mycursor = mydb.cursor(dictionary=True)
sql = "SELECT l.id, t.title, s.sitename, r.regionname, l.url, t.mal_id from region AS r, links AS l, sites AS s, titles AS t WHERE l.titleid = t.titleid AND l.siteid = s.id AND l.regionid = r.id"
mycursor.execute(sql)
loadedresults = mycursor.fetchall()
sql = "SELECT * FROM staging"
mycursor.execute(sql)
stagingresults = mycursor.fetchall()
for loadedresult in loadedresults:
found = False
for stageresult in stagingresults:
if loadedresult["url"] == stageresult["streamsiteurl"] and loadedresult["regionname"] == stageresult["region"]:
found = True
if not found:
print("Stream no longer exist, deleting URL: " + loadedresult["url"])
deleteLink(loadedresult["id"])
print("Cleanup complete")
def deleteLink(id):
deletecursor = mydb.cursor()
sql = "DELETE FROM links WHERE id = %s"
val = [id]
deletecursor.execute(sql, val)
mydb.commit()
if __name__== "__main__":
main()