-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvesselfinder.py
208 lines (169 loc) · 6.67 KB
/
vesselfinder.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
import re
import time
from datetime import datetime, timezone
from typing import Any, Dict, List, Tuple
from bs4 import BeautifulSoup
from botasaurus import bt
from botasaurus.task import task
from botasaurus.browser import browser, Driver
from botasaurus.soupify import soupify
from botasaurus.user_agent import UserAgent
from logger import ScraperLog
from settings import settings
js_script = """
function getWorkerData(text, eivn, compId) {
return new Promise((resolve, reject) => {
const myWorker = window.__gsfworker;
const handler = (event) => {
myWorker.removeEventListener("message", handler); // Cleanup
resolve(event.data); // Resolve the Promise with the worker response
};
myWorker.addEventListener("message", handler);
// Send request
myWorker.postMessage({ cmd: "ms", text, eivn, compId });
// Optional: Reject if no response in 5 seconds
setTimeout(() => {
myWorker.removeEventListener("message", handler);
reject(new Error("Worker timeout"));
}, 5000);
});
}
return getWorkerData(args, "", 123);
"""
def convert_time_format(datetime_str: str) -> str:
match = re.search(r"([A-Za-z]+ \d{1,2}), (\d{2}:\d{2})", datetime_str)
if not match:
ScraperLog.info(f"Date format not able to match pattern: {datetime_str}")
return ""
date_part, time_part = match.groups()
dt = datetime.strptime(f"{date_part}, {time_part}", "%b %d, %H:%M")
dt = dt.replace(year=datetime.now(timezone.utc).year)
return dt.strftime("%Y-%m-%d %H:%M")
@browser(
max_retry=1,
output=None,
reuse_driver=settings.reuse_browser,
proxy=settings.proxy,
close_on_crash=True,
raise_exception=True,
block_images_and_css=True,
wait_for_complete_page_load=False,
headless=settings.headless,
user_agent=UserAgent.RANDOM,
) # type: ignore
def scrape_html(driver: Driver, data: Dict[str, Any]) -> Tuple[str, str]:
detail_page_url = ""
link = data["link"]
search_text = data["search_text"]
wait_time = 5
sleep_time = 2
if driver.config.is_new:
ScraperLog.debug(f"Opening new driver for search term {search_text}")
driver.get(link)
time.sleep(sleep_time)
btns = driver.select_all(".qc-cmp2-footer button", wait=wait_time)
clicked = False
for btn in btns:
if btn.text.lower() == "agree":
btn.click()
clicked = True
break
if clicked:
time.sleep(sleep_time // 2)
response_data = driver.run_js(js_script, search_text)
results = response_data.get("list", [])
for result in results:
if (
result.get("type") == "Container Ship"
and result.get("name", "").lower() == search_text.lower()
):
imo = result["imo"]
detail_page_url = f"https://www.vesselfinder.com/vessels/details/{imo}"
driver.get_via(detail_page_url, referer=link)
break
else:
if len(results) == 1:
imo = results[0]["imo"]
detail_page_url = f"https://www.vesselfinder.com/vessels/details/{imo}"
driver.get_via(detail_page_url, referer=link)
else:
ScraperLog.warning(f"Not found in result! Skipping {search_text}")
results = [
result.get("name", "") + " | " + result.get("type", "")
for result in results
]
filename = f"{search_text.lower().replace(' ', '_')}_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.png"
driver.save_screenshot(filename=filename)
ScraperLog.debug(f"Saved screenshot to {filename}")
ScraperLog.debug(f"Other Options: {results}")
driver.reload()
return "", detail_page_url
time.sleep(sleep_time)
html: str = driver.page_html
return html, detail_page_url
def extract_data(soup: BeautifulSoup) -> Dict[str, Any]:
last_port_name = None
next_port_name = None
last_port_code = None
next_port_code = None
last_port_time = None
next_port_time = None
status = None
next_port_parent = soup.select_one("div.s0 > .flx.vcenter")
last_port_parent = soup.select_one("div.s0 > div.flx.vcenter._rLk01")
if last_port_parent:
last_port_tag = last_port_parent.select_one("a._npNa")
if last_port_tag is None:
last_port_tag = last_port_parent.select_one("._3-Yih")
if last_port_tag:
last_port_name = last_port_tag.text if last_port_tag else None
if next_port_parent:
next_port_tag = next_port_parent.select_one("a._npNa")
if next_port_tag is None:
next_port_tag = next_port_parent.select_one("._3-Yih")
if next_port_tag:
next_port_name = next_port_tag.text if next_port_tag else None
if last_port_parent:
last_port_time_tag = last_port_parent.select_one("div._value")
if last_port_time_tag:
last_port_time = convert_time_format(last_port_time_tag.text.strip())
if next_port_parent:
next_port_time_tag = next_port_parent.select_one("div._value")
if next_port_time_tag:
if "ARRIVED" in next_port_time_tag.text:
status = "Arrived"
else:
status = "Estimated"
next_port_time = convert_time_format(next_port_time_tag.text.strip())
return {
"last_port_name": last_port_name,
"last_port_code": last_port_code,
"last_port_etd": last_port_time,
"next_port_name": next_port_name,
"next_port_code": next_port_code,
"next_port_date": next_port_time,
"next_port_date_status": status,
}
def write_to_file(data: List[Dict[str, Any]], result: List[Dict[str, Any]]) -> None:
for d, r in zip(data, result):
r["search_text"] = d["search_text"]
settings.output_dir.mkdir(parents=True, exist_ok=True)
bt.write_json(result, settings.output_dir / "vesselfinder.json")
@task(
output=write_to_file,
close_on_crash=True,
create_error_logs=False,
parallel=settings.parallel,
raise_exception=True,
max_retry=2,
) # type: ignore
def scrape_data(data: Dict[str, Any]) -> Dict[str, Any]:
search_text = data["search_text"]
ScraperLog.info(f"Scraping vesselfinder for {search_text}")
html, detail_page_url = scrape_html(data)
if html == "":
return {}
return {**extract_data(soupify(html)), "url": detail_page_url}
def scrape_vesselfinder(data: List[Dict[str, str]]) -> List[Dict[str, Any]]:
result: List[Dict[str, Any]] = scrape_data(data)
return result