-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc_tropicair.py
219 lines (166 loc) · 6.84 KB
/
sc_tropicair.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
from class_tropicair import *
import config
import sys
from scrapex import *
from scrapex import common
from scrapex.node import Node
from scrapex.excellib import *
from scrapex.http import Proxy
from proxy_list import random_proxy
# DB
from datetime import datetime
from datetime import timedelta
import argparse
import pytz
import thread
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def date_thread_list(thread_number):
tz = pytz.timezone('America/Los_Angeles')
currentdate = datetime.now(tz)
start_step = 0
date_list = []
for step in range(start_step, start_step + config.DAYS_TO_BE_SCRAPED):
date_list.append(datetime.now(tz) + timedelta(days=step))
result_list = []
lists = []
total_result = len(date_list)
for temp_date in date_list:
lists.append(temp_date)
if len(lists) == int(total_result / thread_number):
obj = {}
obj["no_result"] =0
obj["status"] = "none"
obj["date"] = lists
obj["error_count"] = 0
result_list.append(obj)
lists = []
if len(lists) > 0:
obj = {}
obj["no_result"] =0
obj["status"] = "none"
obj["date"] = lists
obj["error_count"] = 0
result_list.append(obj)
return result_list
def start_scraping(threads_number):
global config
website_type = config.CLASS_TYPE_TROPICAIR
global_sc_obj = Scraper(
use_cache=False, #enable cache globally
retries=3,
use_default_logging = False
)
tropicair_depart_arrival_list = []
try:
with open(config.AIRPORT_RELATIONSHIP_FILE) as csvfile:
reader = csv.reader(csvfile)
for i, item in enumerate(reader):
if i > 0 and item[0] != "" and item[1] != "":
obj = {}
obj["Departure"] = item[0]
obj["Arrival"] = item[1]
obj["Type"] = item[2]
if obj["Type"] == config.CLASS_TYPE_TROPICAIR_STR:
tropicair_depart_arrival_list.append(obj)
except Exception as e:
print (e)
return
sc_obj_list = []
for i in range(0, threads_number):
sc_obj = Scraper(
use_cache=False, #enable cache globally
retries=3,
timeout=60,
use_default_logging = False
)
sc_obj_list.append(sc_obj)
tz = pytz.timezone('America/Los_Angeles')
depart_arrival_list = tropicair_depart_arrival_list
if len(depart_arrival_list) == 0:
print ('None depart arrival info')
return
filename = "{}.csv".format(common_lib.get_webiste_str(website_type))
for i, depart_arrival_info in enumerate(depart_arrival_list):
threads = []
currentdate = datetime.now(tz)
print ("Current Date & Time: {} , {}".format(currentdate.strftime('%Y-%m-%d'), currentdate.strftime('%H:%M')))
departure = depart_arrival_info["Departure"]
arrival = depart_arrival_info["Arrival"]
departure_abbr = ""
arrival_abbr = ""
start_step = 0
departure_abbr = departure.split("-")[1].strip()
arrival_abbr = arrival.split("-")[1].strip()
# for step in range(start_step, start_step + config.DAYS_TO_BE_SCRAPED):
# date_list.append({"date":datetime.now(tz) + timedelta(days=step), "status":"none", "error_count":0})
date_list = date_thread_list(threads_number)
while len(date_list) > 0:
if len(threads) < threads_number:
start_date = None
bStop = True
for date in date_list:
if date["status"] != "complete":
bStop = False
if date["status"] == "none":
start_date = date
start_date["status"] = "pending"
break
if bStop == True:
break
if start_date == None:
continue
print ("++++++++++++++++++++++++++++++")
print ("Depart List = " + str(len(depart_arrival_list)) + " Index =" + str(i))
# print (depart_arrival_info)
print (departure_abbr + "," + arrival_abbr)
print ("++++++++++++++++++++++++++++++")
sleep(config.DRIVER_SHORT_WAITING_SECONDS)
proxy_ip, proxy_port, proxy_user, proxy_pass = random_proxy()
if proxy_user != None:
auth_str = "{}:{}".format(proxy_user, proxy_pass)
proxy = Proxy(proxy_ip, proxy_port, auth_str)
else:
proxy = Proxy(proxy_ip, proxy_port)
s = sc_obj_list[len(date_list) % threads_number]
s.proxy_manager.session_proxy = proxy
class_obj = TropicAir(s, start_date, departure, arrival, currentdate, tz,
departure_abbr, arrival_abbr)
thread_obj = threading.Thread(target=class_obj.parse_website,
args=(config.DRIVER_VALUE_PHANTOMJS,))
# args=(config.DRIVER_VALUE_CHROME,))
threads.append(thread_obj)
thread_obj.start()
for thread in threads:
if not thread.is_alive():
thread.join()
threads.remove(thread)
# filename = "{}_{}_{}_{}.csv".format(common_lib.get_webiste_str(website_type), departure_abbr, arrival_abbr, currentdate.strftime('%Y-%b-%d %H'))
no_result = 0
for item in date_list:
no_result += item["no_result"]
stopdate = datetime.now(tz)
print ("Finish Date & Time: {} , {}".format(stopdate.strftime('%Y-%m-%d'), stopdate.strftime('%H:%M')))
global_sc_obj.save([
"Departure", departure,
"Arrival", arrival,
"No Result", no_result,
"File Name", filename,
"Start", currentdate.strftime('%Y-%m-%d %H:%M'),
"Finish", stopdate.strftime('%Y-%m-%d %H:%M')
], "output/output_{}.csv".format(website_type))
print ( "*************************")
# break
try:
common_lib.upload_file(filename, "output/")
print "Upload"
except:
print ( "Error while upload :" + filename)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Do something.")
parser.add_argument('-t', '--threads', type=int, required=False, default=1, help='Number of threads, defaults to 5')
args = parser.parse_args()
# Set config
threads_number = args.threads
start_scraping(threads_number)