-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.py
105 lines (87 loc) · 3.65 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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
import time, os, glob
import hashlib
def getElement(browser, xPathString):
try:
element = browser.find_element_by_xpath(xPathString)
return element
except (NoSuchElementException) as e:
return None
def getText(element):
try:
return element.get_attribute("innerHTML")
except StaleElementReferenceException:
return ''
def connectToBrowser(url):
workingFolder = os.path.abspath(os.getcwd())
mimeTypes = "application/zip,application/octet-stream,image/jpeg,application/vnd.ms-outlook,text/html,application/pdf"
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2) #tells it not to use default Downloads directory
profile.set_preference("browser.download.manager.showWhenStarting", False) #turns of showing download progress
profile.set_preference("browser.download.dir", workingFolder + '\download\\') #sets the directory for downloads
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", mimeTypes) #tells Firefox to automatically download the files of the selected mime-types
profile.set_preference("pdfjs.disabled", True); #setting pdfjs.disabled to true will prevent Firefox previewing the files.
profile.set_preference("browser.download.manager.focusWhenStarting", False)
profile.set_preference("browser.download.manager.useWindow", False)
profile.set_preference("browser.download.manager.showAlertOnComplete", False)
browser = webdriver.Firefox(firefox_profile = profile)
browser.get("https://web.whatsapp.com/")
return browser
def hashfile(path, blocksize = 65536):
afile = open(path, 'rb')
hasher = hashlib.md5()
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
afile.close()
return hasher.hexdigest()
def removeDuplicates(folder):
list_of_files = glob.glob(folder + '/*')
list_of_files_sorted = sorted(list_of_files, key=os.path.getctime)
if len(list_of_files_sorted) == 0:
return False
duplicates = []
newFile = list_of_files_sorted[-1]
for f in list_of_files_sorted:
if hashfile(newFile) == hashfile(f):
duplicates.append(f)
if len(duplicates) > 1:
os.remove(newFile)
return True
return False
def getMostRecentFileInDownloadsFolder(folder):
list_of_files = glob.glob(folder + '/*')
list_of_files_sorted = sorted(list_of_files, key=os.path.getctime)
if len(list_of_files_sorted) == 0:
return
return list_of_files_sorted[-1]
def checkTag(element, tag):
if element is not None:
try:
if element.tag_name == tag:
return True
else:
return False
except (StaleElementReferenceException, NoSuchElementException) as e:
return False
return False
def checkIfIsChildren(elementFather, elementChild):
if elementFather is None or elementChild is None:
return False
listChildren = []
try:
auxChildren = elementFather.find_elements_by_xpath(".//*")
except (StaleElementReferenceException, NoSuchElementException) as e:
return False
for child in auxChildren:
listChildren.append(child)
if elementChild in listChildren:
return True
else:
return False