-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
149 lines (101 loc) · 4.29 KB
/
main.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
import json
import requests
from bs4 import BeautifulSoup
from requests_html import HTMLSession
from multiprocessing import Pool as ThreadPool
import re
import os
'''
get_flights(CARRIER_CDE) -> [str]
Gets the list of flight summary strings for a particular carrier code
'''
def get_flights(CARRIER_CDE, offset):
ret = []
links_found = 0
print("======== Get Flight Listings ========")
URL = "https://flightaware.com/live/fleet/"+CARRIER_CDE+"?;offset="+str(offset)
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
links = soup.findAll("a")
# get all links but filter out the relevant ones that give info on flights
for link in links:
if "/live/flight/id/" + CARRIER_CDE in link['href']:
flight_link = "https://flightaware.com" + link['href']
print("\tFound link: {}".format(flight_link))
links_found += 1
ret.append(flight_link)
print("Found {} links".format(links_found))
return ret
def flight_data_load(FLIGHT_URL):
session = HTMLSession()
r = session.get(FLIGHT_URL)
r.html.render(timeout=15, wait=5, retries=10)
soup = BeautifulSoup(r.html.html, 'html.parser')
'''
Get STATION info:
> orig_code: IATA airport code (departure station)
> orig_delay_msg: message that indicates some sort of delay going on at departure airport - defaults to 'None'
> dest_code: IATA airport code (arrival station)
> dest_delay_msg: message that indicates some sort of delay going on at arrival airport - defaults to 'None'
'''
orig = soup.select("div.flightPageSummaryAirports div.flightPageSummaryOrigin")
# for some reason, this seems to contain a list of 2 results (at least) ... just pick the first
orig_code = BeautifulSoup(str(orig[0]), 'html.parser').select("span.displayFlexElementContainer")[0].text.strip()
try:
orig_delay_msg = BeautifulSoup(str(orig[0]), 'html.parser').select("span.flightPageSummaryAirportDelay")[0][
'data-tip'].strip()
except IndexError:
orig_delay_msg = "None"
# try to get the delay message ...
delay_messages = soup.select("div.flightPageDelayMessage ul")
#print(delay_messages)
for delay_message in delay_messages:
print("\t|---> " + delay_message.text.strip())
dest = soup.select("div.flightPageSummaryAirports div.flightPageSummaryDestination")
# for some reason, this seems to contain a list of 2 results (at least) ... just pick the first
dest_code = BeautifulSoup(str(dest[0]), 'html.parser').select("span.displayFlexElementContainer")[0].text.strip()
try:
dest_delay_msg = BeautifulSoup(str(dest[0]), 'html.parser').select("span.flightPageSummaryAirportDelay")[0][
'data-tip'].strip()
except IndexError:
dest_delay_msg = "None"
print(orig_code, orig_delay_msg)
print(dest_code, dest_delay_msg)
# Find the JSON data string from the script code
pattern = "<script>[\\n]?var[ ]*trackpollBootstrap[ ]*=[ ]*({\"version\":.*)[ ]*;[ ]*[\\n]?<\/script>"
p = re.compile(pattern)
JSON_DATA_STRING = p.search(r.html.html).group(1)
try:
JSON_DATA = json.loads(JSON_DATA_STRING)
print(JSON_DATA)
print(type(JSON_DATA))
print("+++found-data+++")
except ValueError as e:
print("Error Parsing JSON string")
# Entry Point
if __name__ == '__main__':
import time
import os
start_time = time.time()
WORKERS = 5
offset = 0
keep_going = True
while keep_going:
#list of flight urls to parse
flights = get_flights('ACA', offset)
# exit condition for loop: the run produced no links
if len(flights) == 0:
break
# Make the Pool of workers
pool = ThreadPool(WORKERS)
# Open the URLs in their own threads
# and return the results
results = pool.map(flight_data_load, flights)
# Close the pool and wait for the work to finish
pool.close()
pool.join()
offset += 20 # try next batch of flights
print("batch# "+ str((offset/20)+1) +"complete - time to sleep ..")
os.system("killall chrome")
time.sleep(2)
print("--- %s seconds ---" % (time.time() - start_time))