0.2.0 • Published 6 years ago

polisen-api v0.2.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

PolisenAPI

Simple JavaScript API client for Polisen API.

Getting started

Requires node >= 8.9.0

npm install --save polisen-api

Usage

Initialize

const PolisenAPI = require('polisen-api')

Get police stations

api.fetchPoliceStations()
  .then((stations) => {
    console.log(stations.map(s => s.name))
  })

Get latest events

const api = new PolisenAPI()

api.fetchEvents()
  .then((events) => {
    console.log(events.map(e => e.name))
  })

:warning: The API does not let you filter further back than the last ~500 events or so. See this note on filters for more info.

Filter events on location

api.fetchEvents({ locations: ['Stockholm', 'Järfälla'] })

Filter events on type

api.fetchEvents({ types: ['Djur skadat/omhändertaget', 'Trafikbrott'] })

Filter on year/month/day/hour

api.fetchEvents({ date: { year: 2018 } })
api.fetchEvents({ date: { year: 2018, month: 8 } })
api.fetchEvents({ date: { year: 2018, month: 8, day: 16 } })
api.fetchEvents({ date: { year: 2018, month: 8, day: 16, hour: 8 } })

Multiple filters

api.fetchEvents({
  date: { year: 2018 },
  locations: ['Järfälla'],
  types: ['Djur skadat/omhändertaget'],
})

Throws errors for invalid arguments

api.fetchEvents({ date: { year: 2018, month: '-1' } })
//

If you only want to get build the URL

api.getEventsURL({ locations: ['Stockholm'], date: { year: 2018, month: '3' } })
// supports the same params as fetchEvents

api.getPoliceStationsURL()

You can require all available event types

const policeEventTypes = require('polisen-api/event_types_data')

Add latitude and longitude to location data

const apiLatLong = require('polisen-api/api-lat-long')

api.fetchEvents()
  .then(events => {
    events.map(event => apiLatLong(event.location))
    // { latitude: 63.176683, longitude: 14.636068, ...event.location }
  })

api.fetchPoliceStations()
  .then(stations => {
    stations.map(station => apiLatLong(station.location))
    // { latitude: 63.176683, longitude: 14.636068, ...station.location }
  })

Filter note

When given no parameters the API returns last 500 events. The API does not let you filter more than a week back (during my very limited testing), so unfortunately you can't do historical analysis.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/buren/polisen-api.

Development

Setup

$ git clone https://github.com/buren/polisen-api
$ cd polisen-api
$ npm install

Run the example

$ node example.js

License

The library is available as open source under the terms of the MIT License.