import urllib2 import json import os import datetime import pandas as pd with open(os.path.expanduser('~/.velib'), 'r') as f: key = f.read() def geturl(path): delim = '&' if '?' in path else '?' return "https://api.jcdecaux.com/vls/v1/{0:s}{1:s}apiKey={2:s}".format(path, delim, key) def get(path): url = geturl(path) return json.loads(urllib2.urlopen(url).read()) filter(lambda d: d['name'] == 'Paris', get('contracts')) stations = get('stations?contract=Paris') stations_df = pd.DataFrame(stations) stands = stations_df.bike_stands print("""There are {0:d} stations with a total of {1:d} bike stands near Paris. Each station has between {2:d} and {3:d} stands, with a mean of {4:.1f} stands. """.format( stands.count(), stands.sum(), stands.min(), stands.max(), stands.mean(), )) stands.hist(); title("Number of bike stands per station."); timestamp = stations_df.last_update.max() date = datetime.datetime.fromtimestamp(timestamp / 1000.).strftime('%Y-%m-%d %H:%M:%S') print(date) available_bike_stands = stations_df.available_bike_stands available_bikes = stations_df.available_bikes print("""There are {0:d} stations with no bikes out of {1:d} stations on {2:s}.""".format( np.sum(available_bikes == 0), available_bikes.count(), date,)) positions = np.array([(d['position']['lng'], d['position']['lat']) for d in stations]) indices = positions.min(axis=1) != 0.0 positions = positions[indices,:] x, y = positions.T sizes = stations_df.bike_stands[indices] available_stands = stations_df.available_bike_stands[indices] figure(figsize=(12,8)); scatter(x, y, c=available_stands, s=sizes, edgecolors='none', cmap=get_cmap('RdYlGn')); xticks([]); yticks([]); title("Available bike stands in Paris Velib' stations, {0:s}".format(date));