Last active
August 1, 2023 14:15
-
-
Save Zia-/9fb4ffba60aa9051c01269e559059421 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import websockets | |
import json | |
import time | |
import os | |
import ssl | |
from datetime import datetime, timezone | |
async def connect_ais_stream(): | |
async with websockets.connect("wss://stream.aisstream.io/v0/stream") as websocket: | |
subscribe_message = {"APIKey": "0aa8017c291bbebc2fbc4523760cf787ff6fa6bb", # Required ! | |
"BoundingBoxes": [[[-90, -180], [90, 180]]], # Required! | |
"FiltersShipMMSI": ["368207620", "367719770", "211476060"], # Optional! | |
"FilterMessageTypes": ["PositionReport"]} # Optional! | |
subscribe_message_json = json.dumps(subscribe_message) | |
await websocket.send(subscribe_message_json) | |
messages = [] | |
print('start') | |
async for message_json in websocket: | |
message = json.loads(message_json) | |
message_type = message["MessageType"] | |
messages.append(message_type) | |
print(time.time()) | |
if len(messages) > 1000: | |
print(time.time(), ' ------- ') | |
messages = [] | |
# if message_type == "PositionReport": | |
# # the message parameter contains a key of the message type which contains the message itself | |
# ais_message = message['Message']['PositionReport'] | |
# # print(f"[{datetime.now(timezone.utc)}] ShipId: {ais_message['UserID']} Latitude: {ais_message['Latitude']} Latitude: {ais_message['Longitude']}") | |
# print(time.time()) | |
if __name__ == "__main__": | |
asyncio.run(connect_ais_stream()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment