amphinicy / marine-traffic-client-api

A python client for Marine Traffic API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example code available? Strange runtime errors for API cost and casting to strings.

MSBGit opened this issue · comments

Are there examples available that show how to use the API? I am trying to use the PS06 API and receiving very strange error messages from MarineTraffic:

Traceback (most recent call last):
  File "mttest-1.py", line 36, in <module>
    time_span=10)
  File "mttest-1.py", line 23, in ps06_Vessel_Positions_in_a_Predefined_Area
    msg_type="simple")
  File "/usr/local/lib/python3.7/site-packages/marinetrafficapi/bind.py", line 204, in call
    return request.call()
  File "/usr/local/lib/python3.7/site-packages/marinetrafficapi/bind.py", line 188, in call
    return self._process_response(status_code, response)
  File "/usr/local/lib/python3.7/site-packages/marinetrafficapi/bind.py", line 173, in _process_response
    raise MarineTrafficRequestApiException(msg)
marinetrafficapi.exceptions.MarineTrafficRequestApiException: Request errors: code 5a2: INSUFFICIENT CREDITS. You currently have 13 credits while 84699 is/are required to fulfil this API call. You can purchase additional credits at: https://www.marinetraffic.com/en/online-services/marinetraffic-credits

The request I am making only costs a few credits with a simple query, and about 52 credits for an extended query, nowhere near 84699 credits! My code is pretty simple:

class MarineTraffic:

    def __init__(self, api_key):
        self.api_key = api_key

        self.api = MarineTrafficApi(api_key=self.api_key, debug=True)

        print(f"initialzied MarineTraffic API: api: {self.api}, api_key: {self.api_key}")

    def ps06_Vessel_Positions_in_a_Predefined_Area(self, min_latitude, max_latitude,
                                                   min_longitude, max_longitude,
                                                   time_span):

        vessels = self.api.fleet_vessel_positions(str(min_latitude),
                                                str(max_latitude),
                                                str(min_longitude),
                                                str(max_longitude),
                                                str(time_span),
                                                msg_type="simple")
        return vessels

and if I manually create the URL request string in the browser for PS06, I get back sensible data at a sensible cost. One other thing I had to do was to cast the latitude, longitude and time_span variables to strings; if I tried to pass them without casting to strings, I would get runtime exceptions like this:

Traceback (most recent call last):
  File "mttest-1.py", line 35, in <module>
    time_span=10)
  File "mttest-1.py", line 22, in ps06_Vessel_Positions_in_a_Predefined_Area
    time_span)
  File "/usr/local/lib/python3.7/site-packages/marinetrafficapi/bind.py", line 204, in call
    return request.call()
  File "/usr/local/lib/python3.7/site-packages/marinetrafficapi/bind.py", line 186, in call
    self.url = self._prepare_url()
  File "/usr/local/lib/python3.7/site-packages/marinetrafficapi/bind.py", line 106, in _prepare_url
    self.parameters[RequestConst.PATH]])
TypeError: sequence item 0: expected str instance, float found

I am running Python 3.7.7 on OS X Mojave (10.14.6).