-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlwriter.py
More file actions
390 lines (328 loc) · 14.4 KB
/
Copy pathsqlwriter.py
File metadata and controls
390 lines (328 loc) · 14.4 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import os
from general import *
from domain import *
from urllib.parse import urlparse
from PIL import ImageFile
from PIL import Image
from io import BytesIO
from getMediaDetails import handle_attr_capture
import time
import datetime
CATEGORY = ''
def handle_sql_insert(path,response, link, parent_link):
parsedlink = urlparse(link)
readresponse = response.read()
header = response.getheader('Content-Type')
#TODO
name = '\'index.html\''
if parsedlink.path.split('/')[-1]:
name = '\'' + (parsedlink.path.split('/')[-1]) + '\''
creatorname = get_domain_name(link).split('.')[0]
creationtime = '\'' + response.getheader('Date') + '\''
modificationtime = creationtime
containsdagrs = '0'
if 'html' in header:
containsdagrs = '1'
filesize = '\'' + str(len(readresponse)) + '\''
storagepath = '\'' + link + '\''
keywords = creatorname
for keyword in parsedlink.path.split('/'):
if keyword:
keywords = keywords + ', '+ keyword
keywords = '\'' + keywords + '\''
domain = creatorname
creatorname = '\'' + creatorname + '\''
#category = creatorname
domainpath = 'scripts/domains_covered.txt'
domains = file_to_list(domainpath)
if domain not in domains:
create_CATEGORY(path, domain, domain)
insert_CATEGORY(path)
append_to_file(domainpath, domain)
insert_DAGR(path, name, creatorname, creationtime, modificationtime, containsdagrs, filesize, storagepath, keywords)
#insert_CATEGORY(path, category)
if 'html' in header:
insert_HTML(path)
else :
if 'image' in header or link.endswith('.png') or link.endswith('.jpg') or link.endswith('jpeg') or link.endswith('JPG') or link.endswith('PNG'):
print('img')
dimensions = get_image_dimensions(response)
resolution = getDPI(response)
insert_IMAGE(path, dimensions,resolution)
else:
if 'video' in header or link.endswith('.mp4') or link.endswith('.mov'):
if link.endswith('.mp4') :
filename = generate_temp(response, '.mp4')
length = '\'' + handle_attr_capture(filename, 'Duration').replace('\n', '').replace('\r', '') + '\''
framewidth = leading_number(handle_attr_capture(filename, 'Width'))
frameheight = leading_number(handle_attr_capture(filename, 'Height'))
framerate = leading_number(handle_attr_capture(filename, 'Frame rate '))
insert_VIDEO(path, length, framewidth, frameheight, framerate)
if link.endswith('.mov') :
filename = generate_temp(response, '.mov')
length = '\'' + handle_attr_capture(filename, 'Duration').replace('\n', '').replace('\r', '') + '\''
framewidth = leading_number(handle_attr_capture(filename, 'Width'))
frameheight = leading_number(handle_attr_capture(filename, 'Height'))
framerate = leading_number(handle_attr_capture(filename, 'Frame rate '))
insert_VIDEO(path, length, framewidth, frameheight, framerate)
#insert_CATEGORY(path, 'VIDEO')
else:
if 'audio' in header or link.endswith('.mp3') or link.endswith('.wav'):
if link.endswith('.mp3') :
filename = generate_temp(response, '.mp3')
length = '\'' + handle_attr_capture(filename, 'Duration').replace('\n', '').replace('\r', '') + '\''
bitrate = leading_number(handle_attr_capture(filename, 'Overall bit rate ')) #keep the spaces in this string
channels = leading_number(handle_attr_capture(filename, 'Channel(s)'))
audiosamplerate = leading_number(handle_attr_capture(filename, 'Sampling rate'))
insert_AUDIO(path, length, bitrate, channels, audiosamplerate)
if link.endswith('.wav') :
filename = generate_temp(response, '.wav')
length = '\'' + handle_attr_capture(filename, 'Duration').replace('\n', '').replace('\r', '') + '\''
bitrate = leading_number(handle_attr_capture(filename, 'Overall bit rate ')) #keep the spaces in this string
channels = leading_number(handle_attr_capture(filename, 'Channel(s)'))
audiosamplerate = leading_number(handle_attr_capture(filename, 'Sampling rate'))
insert_AUDIO(path, length, bitrate, channels, audiosamplerate)
#insert_CATEGORY(path, 'AUDIO')
else:
if link.endswith('.txt') :
filename = generate_temp(response, '.txt')
wordcount = str(get_wordcount(filename))
charcount = str(get_charcount(filename))
insert_TEXT(path, wordcount, charcount)
#insert_CATEGORY(path, 'TEXT')
if link.endswith('.pdf') or link.endswith('.doc') or link.endswith('.docx'):
insert_TEXT(path, '0', '0')
#insert_CATEGORY(path, 'TEXT')
else:
copyright = creatorname
fileversion = '\'1.0\''
language = "\'English\'"
insert_SOFTWARE(path,copyright,fileversion,language)
#insert_CATEGORY(path, 'SOFTWARE')
if parent_link:
insert_HAS( path, parent_link)
insert_HAS(path,domain)
def leading_number(string):
temp = string.split()
ret = ''
for item in temp:
if item.isdigit() or '.' in item:
ret += item
return ret
def generate_temp(response, ext):
filename = 'tempfile' + ext
data = response.read()
with open(filename , 'wb') as out:
out.write(data)
return filename
def get_image_dimensions(response):
# get image size (None if not known)
p = ImageFile.Parser()
while 1:
data = response.read(1024)
if not data:
break
p.feed(data)
if p.image:
height, width = p.image.size
return '\'' + str(height) + 'X' + str(width) + '\''
break
return '\'0x0\''
def get_image_dimensions_file(file):
# get image size (None if not known)
p = ImageFile.Parser()
while 1:
data = file.read(1024)
if not data:
break
p.feed(data)
if p.image:
height, width = p.image.size
return '\'' + str(height) + 'X' + str(width) + '\''
break
return '\'0x0\''
def getDPI(response):
try:
img = Image.open(BytesIO(response.read()))
info = img.info
if info['dpi']:
dpix, dpiy = info['dpi']
return '\'' + str(dpix) + 'dpiX' + str(dpiy) +'dpi\''
else:
return '\'72dpix72dpi\''
except:
return '\'72dpix72dpi\''
def getDPI_file(file):
try:
img = Image.open(BytesIO(file.read()))
info = img.info
if info['dpi']:
dpix, dpiy = info['dpi']
return '\'' + str(dpix) + 'dpiX' + str(dpiy) +'dpi\''
else:
return '\'72dpix72dpi\''
except:
return '\'72dpix72dpi\''
def get_wordcount(filename):
count = 0
with open(filename,'r') as file:
words = file.read().split()
count = len(words)
return count
def get_charcount(filename):
count = 0
with open(filename,'r') as file:
words = file.read()
count = len(words)
return count
def insert_DAGR(path, name, creatorname, creationtime, modificationtime, containsdagrs, filesize, storagepath, keywords):
statement = 'SET @GUID = (SELECT UUID());'
print(statement)
append_to_file(path, statement)
statement = 'insert into DAGR (guid,name,creatorname,creationtime,modificationtime,containsdagrs,filesize,storagepath,keywords) values ( @GUID ,' + name + ',' + creatorname + ',' + creationtime + ',' + modificationtime +',' + containsdagrs + ','+ filesize + ','+ storagepath+ ',' + keywords + ' );\n'
print(statement)
append_to_file(path, statement)
def insert_TEXT(path, wordcount, charcount):
statement = 'insert into TEXT (id,wordcount,charcount) values ( @GUID,' + wordcount + ',' + charcount + ');\n'
print(statement)
append_to_file(path, statement)
def insert_IMAGE(path, dimensions,resolution):
statement = 'insert into IMAGE (id,dimensions,resolution) values ( @GUID,' + dimensions + ',' + resolution+');\n'
print(statement)
append_to_file(path, statement)
def insert_VIDEO(path, length, framewidth, frameheight, framerate):
statement = 'insert into VIDEO (id,length,framewidth,frameheight,framerate) values ( @GUID,' + length + ',' + framewidth + ',' + frameheight + ',' + framerate + ');\n'
print(statement)
append_to_file(path, statement)
def insert_AUDIO(path, length, bitrate, channels, audiosamplerate):
statement = 'insert into AUDIO (id,length,bitrate,channels,audiosamplerate) values ( @GUID,' + length + ',' + bitrate + ',' + channels + ',' + audiosamplerate +');\n'
print(statement)
append_to_file(path, statement)
def insert_SOFTWARE(path, copyright,fileversion,language):
statement = 'insert into SOFTWARE (id,copyright,fileversion,language) values ( @GUID ,' + copyright + ',' + fileversion + ',' + language + ');\n'
print(statement)
append_to_file(path, statement)
def insert_HTML(path):
statement = 'insert into HTML (id) values (@GUID);\n'
print(statement)
append_to_file(path, statement)
def insert_HAS(path, parent_link):
statement = 'SET @HID = (SELECT GUID FROM DAGR WHERE STORAGEPATH=\'' + parent_link + '\');'
print(statement)
append_to_file(path, statement)
statement = 'insert into HAS (guid,hid) values ( @HID, @GUID );\n'
print(statement)
append_to_file(path, statement)
def insert_HAS_from_databased_Items(path,link, parent_link):
print(link + ' already in database, adding to HAS')
statement = 'SET @GUID = (SELECT GUID FROM DAGR WHERE STORAGEPATH=\'' + link + '\');'
print(statement)
append_to_file(path, statement)
insert_HAS(path, parent_link)
def insert_CATEGORY(path):
statement = 'insert into CATEGORY (id) values (@GUID);\n'
print(statement)
append_to_file(path, statement)
def create_CATEGORY(path, category, filepath):
print('creating category: ' + category)
keywords = '\'category, ' + category + '\''
category = '\'' + category + '\''
time = '\'' + str(datetime.datetime.now()) + '\''
notapplicable = '\'N/A\''
filepath = '\'' + filepath + '\''
insert_DAGR(path, category, notapplicable , time , time , '1', '0', filepath, keywords)
def handle_sql_insert_filepath(path,filepath):
name = filepath.split('/')[-1]
creatorname = 'user submission'
#if not filepath.split('/')[1] == 'uploadedFiles':
# creatorname = filepath.split('/')[1]
creationtime = '\'' + str(datetime.datetime.now()) + '\''
modificationtime = creationtime
containsdagrs = '0'
#category = get_path(filepath)
#if not category:
#category = 'user submission'
#category = '\'' + category +'\''
filesize = str(float(leading_number(handle_attr_capture(filepath, 'File size')))*1000*1000)
filesize = filesize.split('.')[0]
storagepath = '\'' + filepath + '\''
keywords = creatorname
for keyword in filepath.split('\\'):
if keyword:
keywords = keywords + ', '+ keyword
keywords += ', USERMADE'
keywords = '\'' + keywords + '\''
creatorname = '\'' + creatorname + '\''
if filepath.endswith('html') :
containsdagrs = 1;
name = '\'' + name + '\''
insert_DAGR(path, name, creatorname, creationtime, modificationtime, containsdagrs, filesize, storagepath, keywords)
#insert_CATEGORY(path, category)
if filepath.endswith('html') :
insert_HTML(path)
#insert_CATEGORY(path, 'HTML')
else :
if filepath.endswith('.png') or filepath.endswith('.jpg') or filepath.endswith('jpeg') or filepath.endswith('JPG') or filepath.endswith('PNG'):
print('img')
file = open(filepath , 'rb')
dimensions = get_image_dimensions_file(file)
resolution = getDPI_file(file)
file.close()
insert_IMAGE(path, dimensions,resolution)
#insert_CATEGORY(path, 'IMAGE')
else:
if filepath.endswith('.mp4') or filepath.endswith('.mov'):
if filepath.endswith('.mp4') :
length = '\'' + handle_attr_capture(filepath, 'Duration').replace('\n', '').replace('\r', '') + '\''
framewidth = leading_number(handle_attr_capture(filepath, 'Width'))
frameheight = leading_number(handle_attr_capture(filepath, 'Height'))
framerate = leading_number(handle_attr_capture(filepath, 'Frame rate ')) # keep spaces here
print('framerate: ' + framerate)
insert_VIDEO(path, length, framewidth, frameheight, framerate)
if filepath.endswith('.mov') :
length = '\'' + handle_attr_capture(filepath, 'Duration').replace('\n', '').replace('\r', '') + '\''
framewidth = leading_number(handle_attr_capture(filepath, 'Width'))
frameheight = leading_number(handle_attr_capture(filepath, 'Height'))
framerate = leading_number(handle_attr_capture(filepath, 'Frame rate ')) # keep spaces here
insert_VIDEO(path, length, framewidth, frameheight, framerate)
#insert_CATEGORY(path, 'VIDEO')
else:
if filepath.endswith('.mp3') or filepath.endswith('.wav'):
if filepath.endswith('.mp3') :
length = '\'' + handle_attr_capture(filepath, 'Duration').replace('\n', '').replace('\r', '') + '\''
bitrate = leading_number(handle_attr_capture(filepath, 'Overall bit rate ')) #keep the spaces in this string
channels = leading_number(handle_attr_capture(filepath, 'Channel(s)'))
audiosamplerate = leading_number(handle_attr_capture(filepath, 'Sampling rate'))
insert_AUDIO(path, length, bitrate, channels, audiosamplerate)
if filepath.endswith('.wav') :
length = '\'' + handle_attr_capture(filepath, 'Duration').replace('\n', '').replace('\r', '') + '\''
bitrate = leading_number(handle_attr_capture(filepath, 'Overall bit rate ')) #keep the spaces in this string
channels = leading_number(handle_attr_capture(filepath, 'Channel(s)'))
audiosamplerate = leading_number(handle_attr_capture(filepath, 'Sampling rate'))
insert_AUDIO(path, length, bitrate, channels, audiosamplerate)
#insert_CATEGORY(path, 'AUDIO')
else:
if filepath.endswith('.txt') :
wordcount = str(get_wordcount(filepath))
charcount = str(get_charcount(filepath))
insert_TEXT(path, wordcount, charcount)
#insert_CATEGORY(path, 'TEXT')
else:
if filepath.endswith('.pdf') or filepath.endswith('.doc') or filepath.endswith('.docx'):
insert_TEXT(path, '0', '0')
#insert_CATEGORY(path, 'TEXT')
else:
copyright = creatorname
fileversion = '\'1.0\''
language = "\'English\'"
insert_SOFTWARE(path,copyright,fileversion,language)
#insert_CATEGORY(path, 'SOFTWARE')
folders = filepath.split('/')
folder = ''
for i in range(0,len(folders)-2):
folder+= folders[i] + '/'
folder += folders[-2]
if len(folders) > 1 and folders[-2] != 'uploadedFiles':
insert_HAS(path, folder)
# for i in range(0, len(folders)-1):
# insert_HAS(path, folders[i])