Finalized as of 2019-08-11T15:30:45 -04:00 (EDT)
This commit is contained in:
parent
385b1ef421
commit
faa50c294b
158
acfst.py
Normal file → Executable file
158
acfst.py
Normal file → Executable file
@ -1,24 +1,15 @@
|
|||||||
from datetime import datetime, timedelta
|
#!/usr/local/python3.7/bin/python3
|
||||||
from email.mime.multipart import MIMEMultipart
|
from datetime import datetime
|
||||||
from email.mime.text import MIMEText
|
|
||||||
from email.mime.base import MIMEBase
|
|
||||||
from email import encoders
|
|
||||||
import time
|
import time
|
||||||
|
import math
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import csv
|
import csv
|
||||||
import math
|
from os import path
|
||||||
import smtplib, ssl
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
def sendreport(message):
|
min_speed=10.0
|
||||||
context = ssl.create_default_context()
|
min_width=15.0
|
||||||
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
|
|
||||||
server.login(sender_email, password)
|
|
||||||
server.sendmail(sender_email,receiver_email,message)
|
|
||||||
time_msg=datetime.today()
|
|
||||||
time_msg = time_msg.strftime("%m/%d/%Y, %H:%M:%S")
|
|
||||||
print('Message sent at time {}'.format(time_msg))
|
|
||||||
|
|
||||||
def getships():
|
def getships():
|
||||||
"""
|
"""
|
||||||
@ -27,82 +18,35 @@ def getships():
|
|||||||
If a vessel has a speed over 10 kts, the estimated wave height at distance 1m, 1000m, 2000m, and 3000m, are recorded.
|
If a vessel has a speed over 10 kts, the estimated wave height at distance 1m, 1000m, 2000m, and 3000m, are recorded.
|
||||||
This is to be used in concert with the rest of ACF_ShipTracker python script to save as a CSV and email once a day.
|
This is to be used in concert with the rest of ACF_ShipTracker python script to save as a CSV and email once a day.
|
||||||
"""
|
"""
|
||||||
response = requests.get(
|
matrix = []
|
||||||
'https://services.marinetraffic.com/api/exportvessels/v:8/ea1f4a05afbd000ba30772a9979bf7aa743092cb/timespan:60/msgtype:extended/protocol:json')
|
req_time=datetime.today().strftime("%m/%d/%Y, %H:%M:%S")
|
||||||
json_data=response.content
|
redata = json.loads(requests.get(
|
||||||
redata=json.loads(json_data)
|
'https://services.marinetraffic.com/api/exportvessels/v:8/ea1f4a05afbd000ba30772a9979bf7aa743092cb/timespan:60/msgtype:extended/protocol:json').content)
|
||||||
req_time0=datetime.today()
|
for ship in range(len(redata)) :
|
||||||
req_time=req_time0.strftime("%m/%d/%Y, %H:%M:%S")
|
ship_speed_kts=float(redata[ship][5])/10.0 #in kts
|
||||||
for XX in range(len(redata)) :
|
ship_width_m=float(redata[ship][17])
|
||||||
speed_0=float(redata[XX][5])
|
if ship_speed_kts >= min_speed and ship_width_m >= min_width:
|
||||||
ship_speed_kts=speed_0/10 #in kts
|
shipdata = redata[ship]
|
||||||
if ship_speed_kts>max_speed:
|
|
||||||
shipdata = redata[XX]
|
|
||||||
lat = shipdata[3]
|
lat = shipdata[3]
|
||||||
lon = shipdata[4]
|
lon = shipdata[4]
|
||||||
parameters = {'latitude': lat, 'longitude': lon}
|
parameters = {'latitude': lat, 'longitude': lon}
|
||||||
depth_request = requests.get('https://www.gmrt.org/services/PointServer?', params=parameters)
|
depth = float(str(requests.get('https://www.gmrt.org/services/PointServer?', params=parameters).content, 'utf-8'))*-1 # depth in m
|
||||||
depth = float(str(depth_request.content, 'utf-8'))*-1 # depth in m
|
shipdata.extend([req_time, depth])
|
||||||
shipdata.extend([req_time])
|
|
||||||
shipdata.extend([depth])
|
|
||||||
ship_speed_ms=ship_speed_kts/1.944
|
ship_speed_ms=ship_speed_kts/1.944
|
||||||
froude=ship_speed_ms/math.sqrt(9.8*depth)
|
froude=ship_speed_ms/math.sqrt(9.8*depth)
|
||||||
if shipdata[23]=='Tug':
|
if shipdata[23]=='Tug':
|
||||||
alpha_1=1
|
alpha_1=1.0
|
||||||
else:
|
else:
|
||||||
alpha_1=0.35
|
alpha_1=0.35
|
||||||
alpha_1=float(alpha_1)
|
|
||||||
distance_interest=[1, 1000, 2000, 3000]
|
distance_interest=[1, 1000, 2000, 3000]
|
||||||
for II in distance_interest:
|
for dist in distance_interest:
|
||||||
wave_height=depth*alpha_1*((II/depth)**-0.33)*froude**4
|
wave_height=depth*alpha_1*((dist/depth)**-0.33)*froude**4
|
||||||
shipdata.extend([round(wave_height,2)])
|
shipdata.extend([round(wave_height,2)])
|
||||||
matrix.append(shipdata)
|
matrix.append(shipdata)
|
||||||
time_ships=datetime.today()
|
time_ships=datetime.today().strftime("%m/%d/%Y, %H:%M:%S")
|
||||||
time_ships = time_ships.strftime("%m/%d/%Y, %H:%M:%S")
|
print('Ship data collected at {}'.format(time_ships), file=sys.stderr)
|
||||||
print('Ship data collected at {}'.format(time_ships))
|
print('\n'.join(map(str, matrix)), file=sys.stderr)
|
||||||
|
return matrix
|
||||||
|
|
||||||
exe_question=1
|
|
||||||
max_speed=10
|
|
||||||
|
|
||||||
port = 465 # For SSL
|
|
||||||
sender_email='acf.shiptracker@gmail.com'
|
|
||||||
password = 'ACFships123'
|
|
||||||
|
|
||||||
#receiver_email=input('Where should the email be sent? ')
|
|
||||||
#send_hour=input('When should the email be sent? (0-23) ')
|
|
||||||
receiver_email=sys.argv[1]
|
|
||||||
|
|
||||||
|
|
||||||
print('At 0:00, an email will be sent to {}'.format(receiver_email))
|
|
||||||
|
|
||||||
message_start = MIMEMultipart()
|
|
||||||
message_start["From"] = sender_email
|
|
||||||
message_start["To"] = receiver_email
|
|
||||||
message_start["Subject"] = "Starting ACF ShipTracker"
|
|
||||||
|
|
||||||
body_start = """
|
|
||||||
This is an automated ACF ShipTracker Email, which has begun tracking ships.\n
|
|
||||||
|
|
||||||
An email containing information about these vessels will be sent to this email ({}).\n
|
|
||||||
|
|
||||||
This email will contain a CSV file listing all of the information about these vessels for the previous day.\n
|
|
||||||
|
|
||||||
Please do not respond to this email. For assistance, please contact Justin Blancher (jeblancher@gmail.com) for any
|
|
||||||
troubleshooting.
|
|
||||||
""".format(receiver_email)
|
|
||||||
|
|
||||||
message_start.attach(MIMEText(body_start, 'plain'))
|
|
||||||
text_start = message_start.as_string()
|
|
||||||
sendreport(text_start)
|
|
||||||
|
|
||||||
x=datetime.today()
|
|
||||||
start_time=x.strftime("%m/%d/%Y, %H:%M:%S")
|
|
||||||
y = x.replace(day=x.day, hour=x.hour, second=0, minute=0, microsecond=0) + timedelta(hours=1)
|
|
||||||
delta_t=y-x
|
|
||||||
secs=delta_t.total_seconds()
|
|
||||||
#print(secs)
|
|
||||||
|
|
||||||
|
|
||||||
rowheadings=[
|
rowheadings=[
|
||||||
'MMSI', 'IMO', 'SHIP_ID', 'LAT', 'LON', 'SPEED',
|
'MMSI', 'IMO', 'SHIP_ID', 'LAT', 'LON', 'SPEED',
|
||||||
@ -113,52 +57,18 @@ rowheadings=[
|
|||||||
'Est Wave Height 2000m', 'Est Wave Height 3000m'
|
'Est Wave Height 2000m', 'Est Wave Height 3000m'
|
||||||
]
|
]
|
||||||
|
|
||||||
time.sleep(secs)
|
x2 = datetime.now()
|
||||||
matrix = []
|
matrix = getships()
|
||||||
counter=0
|
csv_name = "/var/log/acfst/ACFshipdata_{}-{}-{}.csv".format(x2.year, '{:02d}'.format(x2.month), '{:02d}'.format(x2.day))
|
||||||
|
if path.exists(csv_name): # ship log exists
|
||||||
while exe_question>0:
|
with open(csv_name, 'a') as outfile:
|
||||||
x2=datetime.today()
|
writer = csv.writer(outfile)
|
||||||
y2 = x2.replace(day=x2.day, hour=x2.hour, minute=0, second=0, microsecond=0) + timedelta(hours=1)
|
writer.writerows(matrix)
|
||||||
delta_t2 = y2 - x2
|
outfile.close()
|
||||||
secs2 = delta_t2.total_seconds()
|
else:
|
||||||
#print(secs2)
|
with open(csv_name, 'w+') as outfile:
|
||||||
time.sleep(secs2)
|
|
||||||
getships()
|
|
||||||
if x2.hour == 23:
|
|
||||||
csv_name = "ACFshipdata_{}_{}_{}.csv".format(x2.month, x2.day,x2.year)
|
|
||||||
with open(csv_name, 'w') as outfile:
|
|
||||||
writer = csv.writer(outfile)
|
writer = csv.writer(outfile)
|
||||||
writer.writerow(rowheadings)
|
writer.writerow(rowheadings)
|
||||||
writer.writerows(matrix)
|
writer.writerows(matrix)
|
||||||
outfile.close()
|
outfile.close()
|
||||||
attachment = open(csv_name, "rb")
|
|
||||||
p = MIMEBase('application', 'octet-stream')
|
|
||||||
p.set_payload((attachment).read())
|
|
||||||
encoders.encode_base64(p)
|
|
||||||
p.add_header('Content-Disposition', "attachment; filename= %s" % csv_name)
|
|
||||||
attachment.close()
|
|
||||||
message = MIMEMultipart()
|
|
||||||
message.attach(p)
|
|
||||||
message["From"] = sender_email
|
|
||||||
message["To"] = receiver_email
|
|
||||||
message["Subject"] = "Mobile Bay Ship information from ".format(x2.minute)
|
|
||||||
|
|
||||||
body = """
|
|
||||||
This is an automated ACF ShipTracker Email, which has been running since {}.\n
|
|
||||||
\n
|
|
||||||
In the past {} hours, a total of {} ships were detected as going faster than {} kts.\n
|
|
||||||
\n
|
|
||||||
Attached is the information for these ships. \n
|
|
||||||
\n
|
|
||||||
Please do not respond to this email. For assistance, please contact Justin Blancher (jeblancher@gmail.com) for any
|
|
||||||
troubleshooting.
|
|
||||||
""".format(start_time,counter,len(matrix),max_speed)
|
|
||||||
|
|
||||||
message.attach(MIMEText(body, 'plain'))
|
|
||||||
text = message.as_string()
|
|
||||||
sendreport(text)
|
|
||||||
matrix = []
|
|
||||||
counter+=1
|
|
||||||
|
|
||||||
|
|
||||||
|
19
email_recipients.sh
Executable file
19
email_recipients.sh
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
[[ -z "${@}" ]] && >&2 echo "Must supply email address list!" && exit 1
|
||||||
|
|
||||||
|
date=$(date -d '-1 day' +%F)
|
||||||
|
while [[ ! -z "${@}" ]]; do
|
||||||
|
addr=${1}
|
||||||
|
shift
|
||||||
|
|
||||||
|
mail -r acf.shiptracker@gmail.com \
|
||||||
|
-s "ACF ShipTracker Log" \
|
||||||
|
-a /var/log/acfst/ACFshipdata_${date}.csv \
|
||||||
|
${addr} << EOF
|
||||||
|
This is an automated ACF ShipTracker email. Attached is the log from yesterday (${date}). It contains all of the vessels coming through Mobile Bay that meet the thresholds (currently traveling over 10.0 knots and at least 15.0 meters wide). The format of the file is Comma Separated Values (CSV).
|
||||||
|
|
||||||
|
Please do not respond to this email. For assistance, please contact Justin Blancher (jeblancher@gmail.com) for any troubleshooting.
|
||||||
|
|
||||||
|
EOF
|
||||||
|
done
|
Loading…
Reference in New Issue
Block a user