-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
411 lines (352 loc) · 13 KB
/
utils.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import html
import os
import time
from difflib import SequenceMatcher
from urllib.parse import unquote
from selenium.webdriver.common.keys import Keys
from constants import DOWNLOAD_DIRECTORY
def open_new_tab(driver):
driver.find_element_by_tag_name("body").send_keys(Keys.COMMAND + "t")
def close_tab(driver):
driver.find_element_by_tag_name("body").send_keys(Keys.COMMAND + "w")
def download_file(driver, data):
download_retries = 0
is_download_successful = False
if not isinstance(data["file_link"], str):
return True
elif data["file_link"] == "":
return True # this happens due to the `add` item
while not is_download_successful and download_retries < 3:
has_downloaded_raw_file = False
# Download file
try:
driver.get(data["file_link"])
time.sleep(2)
except:
break
# Check if file has been downloaded
while not has_downloaded_raw_file:
has_downloaded_raw_file = True
for i in os.listdir(DOWNLOAD_DIRECTORY):
if i.endswith(".crdownload") and data["source_file_name"] != i:
has_downloaded_raw_file = False
break
if not has_downloaded_raw_file:
time.sleep(1)
# Check if file exists in output folder
file_exists = False
for filepath in os.listdir(DOWNLOAD_DIRECTORY):
try:
filename, file_extension = os.path.splitext(filepath)
except:
filename = ""
file_extension = ""
if file_extension:
file_exists = True
raw_path = filepath
break
# if (
# data["source_file_name"].lower() in filepath.lower()
# or data["source_file_name"].lower() == filepath.lower()
# or data["destination_file_name"].lower() in filepath.lower()
# or SequenceMatcher(None, data["source_file_name"], filepath).ratio() >= 0.3
# ):
# file_exists = True
# raw_path = filepath
# break
if file_exists:
try:
# Rename file to reposition to specific
# job folder
os.rename(
os.path.join(DOWNLOAD_DIRECTORY, raw_path),
os.path.join(
DOWNLOAD_DIRECTORY,
data["job_id"],
data["folder"],
data["destination_file_name"] + data["file_extension"],
),
)
# Do not continue if file still exists
file_exists = True
retries = 0
while file_exists and retries < 5:
file_exists = False
for filepath in os.listdir(DOWNLOAD_DIRECTORY):
if raw_path == filepath:
file_exists = True
raw_path = filepath
retries += 1
if file_exists:
print("Still renaming %s" % data["source_file_name"])
time.sleep(2)
else:
is_download_successful = True
except FileExistsError:
# This error exists when there is a file with same destination name as specified in data.
# In this case, we use its raw name
print(
"File %s already exists. Putting raw file name: %s to specific folder"
% (data["source_file_name"], raw_path)
)
try:
os.rename(
os.path.join(DOWNLOAD_DIRECTORY, raw_path),
os.path.join(
DOWNLOAD_DIRECTORY, data["job_id"], data["folder"], raw_path
),
)
except:
break
# Do not continue if file still exists
file_exists = True
retries = 0
while file_exists and retries < 5:
file_exists = False
for filepath in os.listdir(DOWNLOAD_DIRECTORY):
if raw_path == filepath:
file_exists = True
raw_path = filepath
retries += 1
if file_exists:
print("Still transferring %s to specific folder" % raw_path)
time.sleep(2)
else:
is_download_successful = True
time.sleep(3)
except Exception as e:
# This is for unknown failures
is_download_successful = False
else:
is_download_successful = False
print("File does not exist: " + data["source_file_name"])
download_retries += 1
return is_download_successful
def add_images_to_data(driver, job_id, folder_name, all_data, failed_data, images):
added_data = []
for image in images:
try:
try:
file_name = unquote(html.unescape(image["name"].strip()))
except:
file_name = ""
try:
file_link = image["file_link"]
except:
file_link = ""
try:
name, ext = os.path.splitext(file_name)
except:
name = ""
ext = ""
if ext:
ext = ""
data = {
"job_id": job_id,
"folder": "PhotoDocuments",
"file_extension": ext,
"file_link": file_link,
"source_file_name": file_name,
"destination_file_name": file_name,
}
all_data.append(data)
added_data.append(data)
except:
pass
return added_data
def add_all_files_to_data(driver, job_id, folder_name, all_data, failed_data, files):
added_data = []
for file in files:
try:
try:
file_name = unquote(html.unescape(file["name"].strip()))
except:
file_name = ""
try:
file_link = file["file_link"]
except:
file_link = ""
try:
name, ext = os.path.splitext(file_name)
except:
name = ""
ext = ""
try:
filename, file_extension = os.path.splitext(file_link)
except:
filename = ""
file_extension = ""
if ext:
file_extension = ""
try:
source_file_name = html.unescape(
file_link.split(".com/")[1].replace("%2F", "_")
)
except:
source_file_name = ""
data = {
"job_id": job_id,
"folder": folder_name,
"file_extension": file_extension,
"file_link": file_link,
"source_file_name": source_file_name,
"destination_file_name": file_name,
}
all_data.append(data)
added_data.append(data)
except:
pass
return added_data
def click_job_number(driver):
has_clicked_job_number = False
max_retries = 10
retries = 0
while not has_clicked_job_number and retries < max_retries:
try:
wrapper = driver.find_element_by_class_name("job-number")
wrapper.click()
has_clicked_job_number = True
except:
retries += 1
print(
"Cannot find and click job number. Refinding job number (%d)" % retries
)
pass
if not has_clicked_job_number:
print(
"Conclusion: Cannot find job number after 10 tries. Continue to the next process."
)
def add_files_to_data(driver, job_id, folder_name, all_data, failed_data, files):
"""
This method is deprecated
"""
added_data = []
for file in files:
try:
div_id = file.get_attribute("id")
file_link = None
try:
file_link = driver.find_element_by_css_selector(
"div[id='%s'] > div > div > a" % div_id
).get_attribute("href")
except:
try:
file_link = driver.find_element_by_css_selector(
"div[id='%s'] > div > div > a > img" % div_id
).get_attribute("src")
except:
try:
file_link = driver.find_element_by_css_selector(
"div[id='%s'] > div > div > div > a" % div_id
).get_attribute("href")
except:
pass
try:
filename, file_extension = os.path.splitext(file_link)
except:
filename = ""
file_extension = ""
try:
destination_file_name = unquote(
html.unescape(
driver.find_element_by_css_selector(
"div[id='%s'] div.image-title > p" % div_id
)
.get_attribute("innerHTML")
.strip()
)
)
except:
destination_file_name = ""
try:
dest_filename, dest_fileext = os.path.splitext(destination_file_name)
except:
dest_filename = ""
dest_fileext = ""
if dest_fileext:
file_extension = ""
try:
source_file_name = html.unescape(
file_link.split(".com/")[1].replace("%2F", "_")
)
except:
source_file_name = ""
data = {
"job_id": job_id,
"folder": folder_name,
"file_link": file_link,
"file_extension": file_extension,
"source_file_name": source_file_name,
"destination_file_name": destination_file_name,
}
all_data.append(data)
added_data.append(data)
except Exception as e:
try:
destination_file_name = unquote(
html.unescape(
driver.find_element_by_css_selector(
"div[id='%s'] div.image-title > p" % div_id
)
.get_attribute("innerHTML")
.strip()
)
)
except:
destination_file_name = ""
failed_data.append(
{
"job_id": job_id,
"folder": folder_name,
"file_extension": "",
"file_link": "",
"source_file_name": "",
"destination_file_name": destination_file_name,
}
)
print("error file", file, str(e))
return added_data
def get_all_files(driver):
try:
files = driver.execute_script(
"""
files = document.querySelectorAll("div.job-estimate-col");
var result = [];
for (var i=0, max=files.length; i < max; i++) {
var name;
var file_link;
try {
name = files[i].querySelector("div.image-title > p").innerHTML
} catch(err) {
name = ""
}
try {
file_link = files[i].querySelector("ul.dropdown-menu > li:nth-child(2) > a").getAttribute("href")
} catch(err) {
try {
file_link = files[i].querySelector("div > div > div > a").getAttribute("href")
} catch(err) {
try {
file_link = files[i].querySelector("div > div > div > div > a").getAttribute("href")
} catch(err) {
try {
file_link = file_link = files[i].querySelector("div > div > a").getAttribute("href")
} catch(err) {
file_link = ""
}
}
}
}
innerResults = {
name: name,
file_link: file_link
};
result.push(innerResults)
}
return result;
"""
)
except Exception as e:
print(str(e))
files = []
return files