Skip to content

Instantly share code, notes, and snippets.

@jbfarez
Last active October 27, 2022 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbfarez/8fd422d5e4e3d6614c72 to your computer and use it in GitHub Desktop.
Save jbfarez/8fd422d5e4e3d6614c72 to your computer and use it in GitHub Desktop.
Dashing Paris Velib widget

Preview

Screenshot

Description

Retrieve the real-time status of Paris Velib bike stations (Bikes and stands availability).

Requirements

Usage

Add this to your erb dashboard file (You have to replace {STATION_ALIAS} by the alias of your choice, this alias will be reused) :

<!-- Velib {STATION_ALIAS} station-->
<li data-row="1" data-col="1" data-sizex="4" data-sizey="3">
  <div data-id="velib-{STATION_ALIAS}" data-title="" data-view="Velib"></div>
</li>

You have to add this kind of block for each station you want to retrieve the status.

Settings

Job

You have to declare your API Key (In my case, I prefer to use an environment variable) :

API_KEY = ENV["VELIB_API_KEY"]

You can retrive multiple stations datas by adding them to the following array the key should be the STATION_ALIAS and the value should be the ID of the station(s) :

stations = {
  'victoire' => '9111', # Replace this by the station you want to retrieve
}

Widget

  • Just copy velib.coffee, velib.html and velib.scss to {DASHING_PATH}/widgets/velib/
  • Put the bike.svg to {DASHING_PATH}/assets/images/

NB :

Thanks to @stephenyeargin for the inspiration with his B-Cycle widget.

API Documentation here.

Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
class Dashing.Velib extends Dashing.Widget
@accessor 'stateClass', ->
if @get('state') == "OPEN"
"station-state-open"
else if @get('state') == "CLOSED"
"station-state-closed"
else
"station-state-unknown"
constructor: ->
super
ready: ->
$(@node).addClass(@get('stateClass'))
onData: (data) ->
$(@node).addClass(@get('stateClass'))
<h1 class="title" data-bind="station"></h1>
<h3>
<ul>
<li>
<span data-bind="bikes_available"></span> Bikes</br>
</li>
<li>
<span data-bind="stands_available"></span> Stands
</li>
</ul>
</h3>
<p class="more-info">State : <span data-bind="state"></span></p>
<p class="more-info" data-bind="moreinfo | raw"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
#!/usr/bin/env ruby
require 'httparty'
require 'json'
## Velib API key to get on JC Decaux dev website
## more infos : https://developer.jcdecaux.com/
## --
API_KEY = ENV["VELIB_API_KEY"]
## Stations to retrieve
## --
stations = {
'victoire' => '9111',
}
## Job itself
## --
SCHEDULER.every '2m', :first_in => 0 do |job|
# Define keys if not defined
API_KEY = '' unless defined?(API_KEY)
stations.each do |station,station_id|
begin
url = "https://api.jcdecaux.com/vls/v1/stations/#{station_id}?contract=Paris&apiKey=#{API_KEY}"
response = HTTParty.get(url)
datas = JSON.parse(response.body)
station_name = datas['name'].partition('- ').last
status = datas['status']
total_stands = datas['bike_stands']
available_stands = datas['available_bike_stands']
available_bikes = datas['available_bikes']
if not defined?(send_event)
puts "#------------------------------------------------------------#"
puts "Station name : #{station_name}"
puts "Status : #{status}"
puts "Total stands : #{total_stands}"
puts "Available stands : #{available_stands}"
puts "Available bikes : #{available_bikes}"
puts "#------------------------------------------------------------#"
else
title = 'velib-'+station.to_s
send_event(title, station: station_name, state: status, bikes_available: available_bikes, stands_available: available_stands)
end
rescue
puts response.body
puts "Something went wrong ... Could not fetch the informations."
end
end
end
.widget-velib {
background-image: url("/assets/bike.svg");
background-repeat: no-repeat;
background-position: center center;
background-size: 400px 400px;
background-color: #000000;
.updated-at {
color: rgba(0, 0, 0, 0.3);
}
}
.station-state-open {
background-color: #b0c964;
}
.station-state-closed {
background-color: #d04035;
}
.station-state-unknown {
background-color: #bfbdbe;
}
@JCluzet
Copy link

JCluzet commented Oct 26, 2022

Your widget don't work, unauthorized is write on launch.... :(

@jbfarez
Copy link
Author

jbfarez commented Oct 27, 2022

Hey @JCluzet, sorry to hear that but It has been written almost 8 years ago... It's not maintained anymore.
Moreover, if I remember correctly, the provider is not JCDecaux anymore.
You might want to adapt it to use the OpenData API ;)

Happy hacking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment