Base implementation – requests, classes
This commit is contained in:
commit
f2b227f4b6
11 changed files with 2297 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
__pycache__/
|
||||
*.pyc
|
||||
venv/
|
19
LICENSE
Normal file
19
LICENSE
Normal file
|
@ -0,0 +1,19 @@
|
|||
Copyright (c) 2022 Kumi Mitterer <pycruisemapper@kumi.email>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
0
README.md
Normal file
0
README.md
Normal file
2126
doc/example_vessel.json
Normal file
2126
doc/example_vessel.json
Normal file
File diff suppressed because it is too large
Load diff
23
pyproject.toml
Normal file
23
pyproject.toml
Normal file
|
@ -0,0 +1,23 @@
|
|||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "pycruisemapper"
|
||||
version = "0.0.1"
|
||||
authors = [
|
||||
{ name="Kumi Mitterer", email="pycruisemapper@kumi.email" },
|
||||
]
|
||||
description = "Simple Python script to fetch data from CruiseMapper"
|
||||
readme = "README.md"
|
||||
license = { file="LICENSE" }
|
||||
requires-python = ">=3.8"
|
||||
classifiers = [
|
||||
"Programming Language :: Python :: 3",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
"Homepage" = "https://kumig.it/kumitterer/pycruisemapper"
|
||||
"Bug Tracker" = "https://kumig.it/kumitterer/"
|
0
src/pycruisemapper/__init__.py
Normal file
0
src/pycruisemapper/__init__.py
Normal file
1
src/pycruisemapper/classes/__init__.py
Normal file
1
src/pycruisemapper/classes/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .api import CruiseMapper
|
48
src/pycruisemapper/classes/api.py
Normal file
48
src/pycruisemapper/classes/api.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
from .http import HTTPRequest
|
||||
from .vessel import Vessel, Cruise, ShipLine, Flag
|
||||
from ..const import SHIPS_URL, SHIP_URL
|
||||
|
||||
from urllib.parse import urlencode
|
||||
from datetime import datetime
|
||||
from typing import List, Dict
|
||||
|
||||
import json
|
||||
|
||||
|
||||
class CruiseMapper:
|
||||
def request_vessels(self, **kwargs) -> List[Dict]:
|
||||
payload = {
|
||||
"minLat": kwargs.get("min_lat", -90),
|
||||
"maxLat": kwargs.get("max_lat", 90),
|
||||
"minLon": kwargs.get("min_lon", -180),
|
||||
"maxLon": kwargs.get("max_lon", 180),
|
||||
"filter": ",".join(kwargs.get("filter", [str(i) for i in range(100)])),
|
||||
"zoom": "",
|
||||
"imo": kwargs.get("imo", ""),
|
||||
"mmsi": kwargs.get("mmsi", ""),
|
||||
"t": int(kwargs.get("timestamp", datetime.now().timestamp()))
|
||||
}
|
||||
|
||||
request = HTTPRequest(f"{SHIPS_URL}?{urlencode(payload)}")
|
||||
|
||||
return json.loads(request.open().read())
|
||||
|
||||
def request_vessel(self, **kwargs) -> Dict:
|
||||
payload = {
|
||||
"imo": kwargs.get("imo", ""),
|
||||
"mmsi": kwargs.get("mmsi", ""),
|
||||
"zoom": ""
|
||||
}
|
||||
|
||||
request = HTTPRequest(f"{SHIP_URL}?{urlencode(payload)}")
|
||||
|
||||
return json.loads(request.open().read())
|
||||
|
||||
def get_vessels(self, **kwargs) -> List[Vessel]:
|
||||
pass
|
||||
|
||||
def get_vessel(self, **kwargs) -> Vessel:
|
||||
pass
|
||||
|
||||
def fill_vessel(self, vessel: Vessel):
|
||||
pass
|
12
src/pycruisemapper/classes/http.py
Normal file
12
src/pycruisemapper/classes/http.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
from urllib.request import Request, urlopen
|
||||
|
||||
from ..const import REQUIRED_HEADERS
|
||||
|
||||
|
||||
class HTTPRequest(Request):
|
||||
def __init__(self, url, *args, **kwargs):
|
||||
super().__init__(url, *args, **kwargs)
|
||||
self.headers.update(REQUIRED_HEADERS)
|
||||
|
||||
def open(self):
|
||||
return urlopen(self)
|
60
src/pycruisemapper/classes/vessel.py
Normal file
60
src/pycruisemapper/classes/vessel.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
from datetime import datetime, timedelta
|
||||
from typing import Optional, List, Tuple
|
||||
|
||||
|
||||
class Cruise:
|
||||
name: Optional[str]
|
||||
url: Optional[str]
|
||||
start_date: Optional[datetime]
|
||||
end_date: Optional[datetime]
|
||||
itinerary: Optional[List[Optional[Tuple[str, str]]]]
|
||||
|
||||
@property
|
||||
def days(self) -> Optional[int]:
|
||||
if self.end_date and self.start_date:
|
||||
return (self.end_date - self.start_date).days
|
||||
|
||||
class Flag:
|
||||
code: str
|
||||
name: str
|
||||
|
||||
class ShipLine:
|
||||
title: str
|
||||
id: int
|
||||
url: Optional[str]
|
||||
|
||||
class Vessel:
|
||||
id: Optional[int]
|
||||
name: Optional[str]
|
||||
url: Optional[str]
|
||||
url_deckplans: Optional[str]
|
||||
url_staterooms: Optional[str]
|
||||
image: Optional[str]
|
||||
flag: Flag
|
||||
line: Optional[ShipLine]
|
||||
spec_length: Optional[int] # stored in meters
|
||||
spec_passengers: Optional[int]
|
||||
year_built: Optional[int]
|
||||
last_report: Optional[str]
|
||||
imo: int
|
||||
mmsi: int
|
||||
latitude: float
|
||||
longitude: float
|
||||
cog: int # Course over Ground
|
||||
sog: int # Speed over Ground
|
||||
heading: int
|
||||
timestamp: datetime
|
||||
icon: int
|
||||
hover: str
|
||||
cruise: Optional[Cruise]
|
||||
path: Optional[List[Optional[Tuple[float, float]]]]
|
||||
ports: Optional[List[Optional[Tuple[datetime, float, float]]]]
|
||||
destination: str
|
||||
eta: Optional[datetime]
|
||||
current_temperature: Optional[float] # Celsius
|
||||
minimum_temperature: Optional[float] # Celsius
|
||||
maximum_temperature: Optional[float] # Celsius
|
||||
wind_degrees: Optional[float]
|
||||
wind_speed: Optional[float] # m/s
|
||||
wind_gust: Optional[float] # m/s
|
||||
utc_offset: Optional[timedelta]
|
5
src/pycruisemapper/const.py
Normal file
5
src/pycruisemapper/const.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
SHIPS_URL = "https://www.cruisemapper.com/map/ships.json"
|
||||
SHIP_URL = "https://www.cruisemapper.com/map/ships.json"
|
||||
|
||||
REQUIRED_HEADERS = {"X-Requested-With": "XMLHttpRequest",
|
||||
"User-Agent": "Mozilla/5.0 (compatible: pyCruiseMapper; https://kumig.it/kumitterer/pycruisemapper)"}
|
Loading…
Reference in a new issue