-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
232 lines (201 loc) · 8.83 KB
/
main.py
File metadata and controls
232 lines (201 loc) · 8.83 KB
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
import time
import json
import threading
import concurrent.futures
from typing import List, Dict, Any
from selenium.webdriver.common.by import By
from selenium_scraper import SeleniumScraper
from google_flight import *
from utils import *
from logging_config import setup_logger
MAX_WORKERS = 10
SELECTORS = {
"price": 'div.YMlIz.FpEdX > span',
"airline": 'div.sSHqwe.tPgKwe.ogfYpf',
"time": 'div.zxVSec.YMlIz.tPgKwe.ogfYpf > span',
"duration": 'div.Ak5kof > div',
"type": 'div.EfT7Ae.AdWm1c.tPgKwe > span'
}
logger = setup_logger('GFS')
stop_event = threading.Event()
def print_configurations(search_params, urls):
"""Print the script configurations."""
logger.info("Configuration:")
logger.info(f" From Airports : {', '.join(search_params['FromAirports'])}")
logger.info(f" To Airports : {', '.join(search_params['ToAirports'])}")
logger.info(f" First Departure Date : {search_params['FirstDepartureDate']}")
logger.info(f" Last Departure Date : {search_params['LastDepartureDate']}")
logger.info(f" How Many Days : {search_params['HowManyDays']}")
logger.info(f" Flex Days : {search_params['FlexDays']}")
logger.info(f" Only Weekend : {search_params['OnlyWeekend']}")
logger.info(f" Total Combinations : {len(urls)}")
logger.info(f" Max Workers : {MAX_WORKERS}")
logger.info(f" Workers Needed : {len(urls) // MAX_WORKERS + 1}")
def print_summary(summary):
"""Print the summary of the scraping process."""
logger.info("Summary:")
logger.info(f" Total URLs : {summary['total_urls']}")
logger.info(f" Successful URLs : {summary['successful']}")
logger.info(f" Failed URLs : {summary['failed']}")
logger.info(f" Total Duration (s) : {summary['total_duration_seconds']:.2f}")
logger.info(f" Average Time/URL (s) : {summary['average_time_per_url']:.2f}")
def extract_flight_data(scraper: SeleniumScraper, timestamp: float) -> Dict[str, Any]:
"""Extract and clean flight information from the current page."""
results = {}
scraper.accept_google_cookies()
for key, selector in SELECTORS.items():
elements = scraper.wait_for_elements(By.CSS_SELECTOR, selector)
results[key] = [clean_text(elem.text) for elem in elements if elem.text]
screenshot = scraper.take_screenshot(f"{config.CURRENT_RESULTS_FOLDER}/screenshots/{timestamp}.png")
return {
"values": results,
"screenshot": screenshot
}
def process_url(url: str, max_retries: int = 2) -> Dict[str, Any]:
"""
Process a single URL with a SeleniumScraper instance.
This function will be called concurrently for different URLs.
Args:
url: The URL to process
max_retries: Maximum number of retry attempts for failures
Returns:
Dictionary containing the processing results
"""
timestamp = time.time()
result = {
"status": "failed",
"url": url["url"],
"from": url["from"],
"to": url["to"],
"outbound": url["outbound"],
"inbound": url["inbound"],
"timestamp": timestamp,
"duration": 0,
"attempts": 0,
"screenshot": None,
"data": None,
"error": None
}
# Create a new scraper instance for this thread
with SeleniumScraper() as scraper:
attempts = 0
success = False
while attempts < max_retries and not success:
attempts += 1
if stop_event.is_set():
scraper.logger.warning("Stop event set. Exiting thread.")
return result
try:
# Run the complete scraping task using our generic scraper
scraping_result = scraper.run_scraping_task(
url["url"],
lambda s: extract_flight_data(s, timestamp))
# Check if the scraping was successful
if scraping_result["success"]:
result["status"] = "success"
result["data"] = scraping_result["data"]["values"]
result["screenshot"] = scraping_result["data"]["screenshot"]
success = True
else:
scraper.logger.warning(f"Attempt {attempts} failed: {scraping_result['error']}")
if attempts < max_retries:
time.sleep(2)
except Exception as e:
scraper.logger.error(f"Error in attempt {attempts}: {str(e)}")
result["error"] = str(e)
if attempts < max_retries:
time.sleep(2)
# Include retry information
result["attempts"] = attempts
result["duration"] = time.time() - timestamp
return result
def get_scraping_summary(results: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Generate a summary of the scraping results."""
summary = {
"total_urls": len(results),
"successful": sum(1 for r in results if r["status"] == "success"),
"failed": sum(1 for r in results if r["status"] != "success"),
"total_duration_seconds": sum(r["duration"] for r in results),
"average_time_per_url": sum(r["duration"] for r in results) / len(results) if results else 0
}
return summary
def process_urls_concurrently(urls: List[str], max_workers: int = 5) -> List[Dict[str, Any]]:
"""
Process multiple URLs concurrently using ThreadPoolExecutor.
Args:
urls: List of URLs to process
max_workers: Maximum number of concurrent workers
Returns:
List of result dictionaries for each URL
"""
logger.info("Starting scraping...")
results = []
processed_urls = 0
# Using ThreadPoolExecutor to run tasks concurrently
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
# Submit all URL processing tasks
future_to_url = {executor.submit(process_url, url): url for url in urls}
try:
# Process results as they complete
for future in concurrent.futures.as_completed(future_to_url):
if stop_event.is_set():
break
url = future_to_url[future]
try:
result = future.result()
except Exception as e:
logger.exception(f"Thread for URL {url} generated an exception: {e}")
result = {"url": url, "status": "exception", "error": str(e)}
finally:
save_worker_result(result)
results.append(result)
processed_urls += 1
logger.info(f"Thread for URL {url} completed. Status: {result['status']}")
logger.info(f"Processed: {processed_urls}/{len(urls)} - Remaining: {len(urls) - processed_urls}")
except KeyboardInterrupt:
logger.warning("Keyboard interrupt received, shutting down executor...")
stop_event.set()
executor.shutdown(wait=False, cancel_futures=True)
os._exit(1)
logger.info("Scraping completed.")
return results
def save_worker_result(result: Dict[str, Any]) -> None:
"""Save the result to JSON and CSV files."""
append_result_to_json(result)
append_result_to_csv(result)
def main():
try:
with open('settings.json', 'r') as file:
search_params = json.load(file)
if not search_params:
logger.warning("No settings.json found. Exiting.")
return
if not is_date_range_valid(search_params):
logger.warning("Dates from settings.json are invalid. Exiting.")
return
urls = generate_google_flight_urls(search_params)
if not urls:
logger.warning("No URLs generated. Exiting.")
return
print_configurations(search_params, urls)
user_confirm = lambda prompt: input(prompt).strip().lower() in ['', 'y', 'yes']
if not user_confirm("Do you want to continue? (y/n): "):
logger.warning("User chose not to continue. Exiting.")
return
config.init()
create_results_folder()
results = process_urls_concurrently(urls, max_workers=MAX_WORKERS)
if not results:
logger.warning("No results obtained. Exiting.")
return
summary = get_scraping_summary(results)
if not summary:
logger.warning("No summary generated. Exiting.")
return
save_summary_to_json(summary)
print_summary(summary)
except KeyboardInterrupt:
stop_event.set()
logger.warning("Process interrupted by user.")
if __name__ == "__main__":
main()