-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTorFirefox.py
169 lines (129 loc) · 4.94 KB
/
TorFirefox.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
'''
getFirefoxBrowser():
This file creates the object for Firefox webdriver with Tor settings.
It also set the downloading preferences and blocks the window pop-up that is shown in firefox after file is downloaded
unzipFile(filepath,destination_folder):
Unzips file nly after the it is confirmed that file s completely downloaded
checkDownStatus.txt:
this file is updated by the file `watchdogg.py` after the zip file is completely downloaded
'''
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from pyvirtualdisplay import Display
import os
import time
import multiprocessing
import watchdogg
def main():
print os.getcwd()
# unzipFile("/home/gugli/Documents/script_py/Dainik_Jagron/pdfcompressor.zip","/home/gugli/Documents/script_py/Dainik_Jagron")
b = getFirefoxBrowser()
time.sleep(20)
b.close()
def getFirefoxBrowser():
print "===== getFirefoxBrowser"
profile=webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9150)
profile.set_preference("browser.download.folderList", 2) # 0 means to download to the desktop, 1 means to download to the default "Downloads" directory, 2 means to use the directory
# profile.set_preference("browser.helperApps.alwaysAsk.force", False)
# profile.set_preference("browser.download.manager.showWhenStarting",False)
# profile.set_preference("browser.download.dir", "/home/gugli/Downloads")
# profile.set_preference('browser.download.folderList', 2)
# profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference("browser.helperApps.neverAsk.saveToDisk","application/xml,text/plain,text/xml,image/jpeg,text/eml,application/zip");
print "======profile set===="
'''
options = Options()
options.add_argument( "--headless" )
# options.add_argument( "--screenshot test.jpg http://google.com/" )
# driver = webdriver.Firefox( firefox_options=options )
browser=webdriver.Firefox(firefox_profile=profile,firefox_options=options)
'''
# display.start()
browser=webdriver.Firefox(profile)
return browser
# return browser,display
# browser.get("http://yahoo.com")
# browser.save_screenshot("screenshot.png")
# browser.close()
def unzipFile(filepath,destination_folder):
startWatchdog()
print "===== unzipFile"
cmd = "unzip " + filepath + " -d " + destination_folder
# cmd = "jar xvf" + filepath
# filestatus = os.path.isfile(filepath)
# while( os.path.isfile(filepath) == 0) :
# time.sleep(5)
# print "=======File not present, already slept 5 sec since I last checked;======="
# continue
'''
while not os.path.exists(filepath):
print " ----sleeping----"
time.sleep(10)
'''
### read checkDownStatus for for the status of the file
fileObject = open(os.getcwd()+"/checkDownStatus.txt" ,"r")
status = fileObject.read()
print status
while( status != "DONE"):
fileObject = open(os.getcwd()+"/checkDownStatus.txt" ,"r")
status = fileObject.read()
print " ----sleeping----" , status, status != "DONE"
time.sleep(5)
## this check isn't necessary now
if os.path.isfile(filepath):
os.system(cmd)
else:
raise ValueError("%s isn't a file!" % filepath)
fileObject.close()
# try:
# with io.FileIO(filepath, "r+") as fileObj:
# '''
# Deal with case where FTP client uses extensions such as ".part" and '.filepart" for part of the incomplete downloaded file.
# To do this, make sure file exists before adding it to list of completedFiles.
# '''
# if(os.path.isfile(fileName)):
# completedFiles.append(fileName)
# print "File=" + file_path + " has completely loaded."
# except IOError as ioe:
# print str(ioe)
# while(1):
# try:
# os.system(cmd)
# print "++++++++++++++++++++"
# break
# except Exception as e:
# time.sleep(5)
# print "=======File not present, already slept 5 sec since I last checked;======="
# continue
def closeBrowser(browser):
browser.close()
print "=======Browser Closed===="
def closeDisplay(display):
display.close()
print "=======Display Closed===="
def startDisplay(display):
display.start()
print "======= Invisible Display Started [visible=0, size=(1024, 768)]===="
def watchdog():
### running daemon that checks if the file is downoaded completely or not
w = watchdogg.Watcher()
w.run()
def startWatchdog():
'''
### below updation is necessary in order to open watchdog
coz after the last step is done (prev run) the file is updated to "DONE"
and this same string is also required to close watchdog after all unzipping is done
'''
with open(os.getcwd()+"/checkDownStatus.txt",'w') as outFile:
outFile.write("blah")
p1 = multiprocessing.Process(name='p1', target=watchdog)
p1.start()
# p2 = multiprocessing.Process(name='p', target=sud)
# p2.start()
if __name__ == "__main__":
main()