diff --git a/src/boat.py b/src/boat.py new file mode 100644 index 0000000..70e8c5d --- /dev/null +++ b/src/boat.py @@ -0,0 +1,25 @@ +#! encode: utf8 + +class Boat: + def __init__(self, boatId, name, sismerId, url, avatar, temperatures, positions): + self.boatId = boatId + self.name = name + self.sismerId = sismerId + self.url = url + self.avatar = avatar + self.temperatures = temperatures + self.positions = positions + def __str__(self): + return f"[{self.name}]\nSISMER ID: {self.sismerId}\nURL: {self.url}\nAvatar: {self.avatar}" + + def getMaxTemp(self): + return max(self.temperatures) + def getNothietCoords(self): + if len(self.positions == 0): + raise Exception(f"{self.name} must have coords to compare") + best = slef.positions[0] + for position in self.positions: + if position.isNorthiestThan(best): + best = position + return best + diff --git a/src/boatsLoader.py b/src/boatsLoader.py new file mode 100644 index 0000000..297b9e0 --- /dev/null +++ b/src/boatsLoader.py @@ -0,0 +1,35 @@ +#! encode: utf8 + +from boat import Boat +from position import Position +from dataLoader import DataLoader + +class BoatsLoader(DataLoader): + def __init__(self): + self._url = 'https://localisation.flotteoceanographique.fr/api/v2/vessels' + self.searchBegin = '-01-01T00:00:00.000Z' + self.searchEnd = '-12-31T23:59:59.000Z' + + + def loadBoats(self, year): + rawData = self.loadFromURL(self._url) + boats = [] + for boat in rawData.json(): + boatData = self._loadBoatData(boat['id'], year) + tmpBoat = Boat(boat['id'], boat['name'], boat['sismerId'], boat['url'], boat['avatar'], boatData['temp'], boatData['pos']) + boats.append(tmpBoat) + return boats + def _loadBoatData(self, boatId, year): + jsonData = self.loadFromURL(self._url + f"/{boatId}/positions?startDate={year}{self.searchBegin}&endDate={year}{self.searchEnd}").json() + data = {} + data['pos'] = [] + data['temp'] = [] + for singleData in jsonData: + if len(singleData['data']) == 0: + continue + data['pos'].append(Position(singleData['lat'], singleData['lon'])) + + if 'airtemp' in singleData['data']: + data['temp'].append(singleData['data']['airtemp']) + return data + diff --git a/src/dataLoader.py b/src/dataLoader.py new file mode 100644 index 0000000..b71925e --- /dev/null +++ b/src/dataLoader.py @@ -0,0 +1,10 @@ +#! encode:utf8 + +import requests + +class DataLoader: + def loadFromURL(self, url): + data = requests.get(url) + if(data.status_code != 200): + raise Exception(f"Error {data.status_code}") + return data diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..54faccd --- /dev/null +++ b/src/main.py @@ -0,0 +1,14 @@ +#! encoding: utf-8 + +from boatsLoader import BoatsLoader +from sys import argv + +if len(argv) <= 1: + raise Exception("You must give the year you want to get") +year = int(argv[1]) + +boatsLoader = BoatsLoader() + +for boat in boatsLoader.loadBoats(year): + print(boat) + print() diff --git a/src/position.py b/src/position.py new file mode 100644 index 0000000..52c61e5 --- /dev/null +++ b/src/position.py @@ -0,0 +1,11 @@ +#! encoding: utf8 + +class Position: + def __init__(self, latitude, longitude): + self.latitude = latitude + self.longitude = longitude + + def __str__(self): + return f"[{self.latitude}; {self.latitude}] ({self.date})" + def isNorthiestThan(self, position): + return self.latitude >= position.latitude