electricitymaps / bloom-contrib

Making carbon footprint data available to everyone.
https://www.bloomclimate.com
MIT License
435 stars 104 forks source link

Add trainline integration #286

Closed liamgarrison closed 4 years ago

liamgarrison commented 4 years ago

Added an integration for Trainline, a very popular train/bus booking app in the UK and Europe.

liamgarrison commented 4 years ago

Great! Could you post an example output so we can double check?

Screenshot 2019-12-15 at 11 12 34 1

Also, do we have access to the trip distance in km?

Unfortunately not, just the duration and start and end points:

"outward": {
  "date": "2019-10-15T10:04:00+01:00",
  "duration": 24,
  "legs": [
    {
        "id": "6ec68a0d-2197-41d9-a3b9-5734bb14b42e",
        "destination": {
            "name": "Clapham Junction",
            "shortName": "CLJ",
            "time": "2019-10-15T10:08:00+01:00"
        },
        "origin": {
            "name": "Putney",
            "shortName": "PUT",
            "time": "2019-10-15T10:04:00+01:00"
        },
        "transportMode": "train",
        "carrierName": "South Western Railway",
        "carrierCode": "urn:trainline:atoc:carrier:SW",
        "carrierId": "78402b3e-16dc-440e-84c1-c8544911cb5e",
        "sleeper": false,
        "duration": 4,
        "transportDesignation": null,
        "seatReservation": {
            "data": null,
            "unallocatedInfoUrl": "https://www.thetrainline.com/en/help/question/4136/how-do-i-reserve-a-seat",
            "firstClass": false,
            "standardClass": true
        }
    },
    {
        "id": "6840f05e-b49b-4248-a925-6298600f1f72",
        "destination": {
            "name": "London Victoria",
            "shortName": "VIC",
            "time": "2019-10-15T10:28:00+01:00"
        },
        "origin": {
            "name": "Clapham Junction",
            "shortName": "CLJ",
            "time": "2019-10-15T10:20:00+01:00"
        },
        "transportMode": "train",
        "carrierName": "Southern",
        "carrierCode": "urn:trainline:atoc:carrier:SN",
        "carrierId": "7266db63-b854-4ae9-b885-4c3934146449",
        "sleeper": false,
        "duration": 8,
        "transportDesignation": null,
        "seatReservation": {
            "data": null,
            "unallocatedInfoUrl": "https://www.thetrainline.com/en/help/question/4136/how-do-i-reserve-a-seat",
            "firstClass": false,
            "standardClass": true
        }
    }
  ]
}

The duration of the outward/inward journey includes changes, so might be best to use the sum of all the legs for better accuracy? What do you reckon?

martincollignon commented 4 years ago

Hi @liamgarrison! Could you help out on the last touches? :)

liamgarrison commented 4 years ago

Ok ready for review, have added cookiejar now and it's up and running

liamgarrison commented 4 years ago

I've switched to using superagent, seems to work nicely:

Screenshot 2020-01-21 at 19 39 17