-
Notifications
You must be signed in to change notification settings - Fork 6
/
cruises
executable file
·46 lines (45 loc) · 2.13 KB
/
cruises
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
#!/usr/bin/env python3
# fetch incoming auckland port ship data
import requests
import bs4
import json
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument("-j", "--json", help="json output", action="store_true")
parser.add_argument("-c", "--csv", help="csv output", action="store_true")
if not len(sys.argv) > 1:
parser.print_help()
exit()
shipstatusuri = 'https://www.poal.co.nz/operations/schedules/cruise'
try:
shipstatus = requests.get(shipstatusuri)
except requests.exceptions.RequestException as e:
print(e)
exit(1)
if shipstatus.status_code != 200:
print('error fetching ship status')
exit(1)
table = bs4.BeautifulSoup(shipstatus.text, 'html.parser').find('table', {'class': 'makeResponsive vessels cruiseSchedule'})
if parser.parse_args().json:
cruises = []
for row in table.find_all('tr'):
cols = row.find_all('td')
if len(cols) > 0:
cruises.append({'vessel': cols[0].text.replace('\n', ' ').replace('\t', ' ').strip(),
'wharf': cols[1].text.replace('\n', ' ').replace('\t', ' ').strip(),
'arrives': cols[2].text.replace('\n', ' ').replace('\t', ' ').strip(),
'departs': cols[3].text.replace('\n', ' ').replace('\t', ' ').strip(),
'previous': cols[4].text.replace('\n', ' ').replace('\t', ' ').strip(),
'next': cols[5].text.replace('\n', ' ').replace('\t', ' ').strip()})
print(json.dumps(cruises, indent=4))
if parser.parse_args().csv:
for row in table.find_all('tr'):
cols = row.find_all('td')
if len(cols) > 0:
print(cols[0].text.replace('\n', ' ').replace('\t', ' ').strip() + ',' +
cols[1].text.replace('\n', ' ').replace('\t', ' ').strip() + ',' +
cols[2].text.replace('\n', ' ').replace('\t', ' ').strip() + ',' +
cols[3].text.replace('\n', ' ').replace('\t', ' ').strip() + ',' +
cols[4].text.replace('\n', ' ').replace('\t', ' ').strip() + ',' +
cols[5].text.replace('\n', ' ').replace('\t', ' ').strip())