Skip to content

Instantly share code, notes, and snippets.

@jsanz
Created March 13, 2018 16:27
Show Gist options
  • Select an option

  • Save jsanz/08cb8191301a224cbfa4c621ef994609 to your computer and use it in GitHub Desktop.

Select an option

Save jsanz/08cb8191301a224cbfa4c621ef994609 to your computer and use it in GitHub Desktop.
Python: Get data from BlaBlaCar API
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import math\n",
"import os\n",
"import csv"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def getRow(trip):\n",
" '''\n",
" Flattens a BlaBlaCar trip object into the plain list we want to upload to CARTO\n",
" '''\n",
" return {\n",
" 'url' : trip['links']['_front'], \n",
" 'from_lat': trip['departure_place']['latitude'], \n",
" 'from_lon': trip['departure_place']['longitude'], \n",
" 'to_lat' : trip['arrival_place']['latitude'],\n",
" 'to_lon' : trip['arrival_place']['longitude'],\n",
" 'car_comfort' : trip['car']['comfort_nb_star'] if 'car' in trip else '' ,\n",
" 'car_maker' : trip['car']['make'] if 'car' in trip else '',\n",
" 'dep_date' : trip['departure_date'],\n",
" 'distance' : trip['distance']['value'],\n",
" 'duration' : trip['duration']['value'],\n",
" 'price' : trip['price_with_commission']['value'],\n",
" 'seats' : trip['seats']\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def getTrips(city):\n",
" '''\n",
" Iterates over the BlaBlaCar /trips/ endpoint to retrieve all the pages\n",
" '''\n",
" trips = []\n",
" page = 1\n",
" \n",
" URL = \"https://public-api.blablacar.com/api/v2/trips\"\n",
" KEY = os.getenv('BLABLACAR_API')\n",
" CUR = \"EUR\"\n",
" HEADERS = {\n",
" 'Content-Type': \"application/json\",\n",
" 'Cache-Control': \"no-cache\"\n",
" }\n",
" QS_BASE = {\"key\": KEY,\"cur\": CUR, \"limit\": 100} \n",
"\n",
" querystring = dict( { 'page' : page, \"fn\": city, }, **QS_BASE)\n",
" response = requests.request(\"GET\", URL, headers=HEADERS, params=querystring)\n",
" rj = response.json()\n",
"\n",
" while 'trips' in rj:\n",
" #print(' Got page {}'.format(page))\n",
" trips.extend(rj['trips'])\n",
" page = page + 1\n",
"\n",
" querystring = dict( { 'page' : page, \"fn\": city }, **QS_BASE)\n",
" response = requests.request(\"GET\", URL, headers=HEADERS, params=querystring)\n",
" rj = response.json()\n",
" \n",
" return trips\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Process cities\n",
"\n",
"Process a list of cities to get all the trips and generates as a result a CSV ready to be used at CARTO"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"cities = ['Valencia','Madrid','Barcelona','Bilbao','Sevilla']"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting the trips from BlaBlaCar API for Valencia...\n",
"722 trips retreived\n",
"\n",
"Getting the trips from BlaBlaCar API for Madrid...\n",
"838 trips retreived\n",
"\n",
"Getting the trips from BlaBlaCar API for Barcelona...\n",
"868 trips retreived\n",
"\n",
"Getting the trips from BlaBlaCar API for Bilbao...\n",
"1029 trips retreived\n",
"\n",
"Getting the trips from BlaBlaCar API for Sevilla...\n",
"997 trips retreived\n",
"\n"
]
}
],
"source": [
"all_trips = []\n",
"\n",
"for city in cities:\n",
" print('Getting the trips from BlaBlaCar API for {}...'.format(city))\n",
" trips = getTrips(city) \n",
" print('{} trips retreived'.format(len(trips))) \n",
" all_trips.append({ 'city':city, 'trips': trips})\n",
" print('')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once get all the trips, save them into a single CSV file"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generating blablatrips.csv...\n",
"Done!\n"
]
}
],
"source": [
"filename = 'blablatrips.csv'\n",
"print('Generating {}...'.format(filename))\n",
"with open(filename, 'w', newline='') as csvfile:\n",
" fieldnames = [\n",
" 'city', 'url', 'from_lat', 'from_lon', 'to_lat', 'to_lon', 'car_comfort', \n",
" 'car_maker', 'dep_date', 'distance', 'duration', 'price', 'seats'\n",
" ]\n",
" writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n",
" writer.writeheader()\n",
" for group in all_trips:\n",
" city = group['city']\n",
" trips = group['trips']\n",
" try:\n",
" for trip in trips:\n",
" row = getRow(trip)\n",
" writer.writerow(dict({'city':city},**row))\n",
" except KeyError as e:\n",
" print(e)\n",
" print(trip)\n",
"\n",
"print('Done!')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment