Create a gist now

Instantly share code, notes, and snippets.

@davefp /LICENSE.md
Last active Jun 17, 2017

What would you like to do?
Weather Widget for Dashing

##Preview

Description

Simple Dashing widget (and associated job) to display weather info. Uses Yahoo's weather API.

##Dependencies

xml-simple

Add it to dashing's gemfile:

gem 'xml-simple'

and run bundle install. Everything should work now :)

##Usage

To use this widget, copy weather.html, weather.coffee, and weather.scss into the /widgets/weather directory. Put the weather.rb file in your /jobs folder.

You'll also need the Climacons Webfont. Download it, and put the .eot, .ttf, and .woff files in your /assets/fonts folder

To include the widget in a dashboard, add the following snippet to the dashboard layout file:

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="weather" data-view="Weather"></div>
</li>

##Settings

You'll need to add the WOEID for your desired location to the job (you'll get Ottawa's weather by default). Find your WOEID here.

Temperatures are fetched and displayed in Centigrade, but you can change it to Fahrenheit by replacing format = 'c' to format = 'f' at the top of the job file.

Weather is fetched every 15 minutes, but you can change that by editing the job schedule.

The MIT License (MIT)

Copyright (c) 2014 David Underwood

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class Dashing.Weather extends Dashing.Widget
ready: ->
# This is fired when the widget is done being rendered
onData: (data) ->
if data.climacon
# reset classes
$('i.climacon').attr 'class', "climacon icon-background #{data.climacon}"
<i class="climacon icon-background"></i>
<h1 class="title" data-bind="title"></h1>
<h2 class="temp" data-bind="temp | raw"></h2>
<p class="condition" data-bind="condition"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
require 'net/http'
require 'xmlsimple'
# Get a WOEID (Where On Earth ID)
# for your location from here:
# http://woeid.rosselliot.co.nz/
woe_id = 3369
# Temerature format:
# 'c' for Celcius
# 'f' for Fahrenheit
format = 'c'
SCHEDULER.every '1s', :first_in => 0 do |job|
http = Net::HTTP.new('weather.yahooapis.com')
response = http.request(Net::HTTP::Get.new("/forecastrss?w=#{woe_id}&u=#{format}"))
weather_data = XmlSimple.xml_in(response.body, { 'ForceArray' => false })['channel']['item']['condition']
weather_location = XmlSimple.xml_in(response.body, { 'ForceArray' => false })['channel']['location']
send_event('weather', { :temp => "#{weather_data['temp']}&deg;#{format.upcase}",
:condition => weather_data['text'],
:title => "#{weather_location['city']} Weather",
:climacon => climacon_class(weather_data['code'])})
end
def climacon_class(weather_code)
case weather_code.to_i
when 0
'tornado'
when 1
'tornado'
when 2
'tornado'
when 3
'lightning'
when 4
'lightning'
when 5
'snow'
when 6
'sleet'
when 7
'snow'
when 8
'drizzle'
when 9
'drizzle'
when 10
'sleet'
when 11
'rain'
when 12
'rain'
when 13
'snow'
when 14
'snow'
when 15
'snow'
when 16
'snow'
when 17
'hail'
when 18
'sleet'
when 19
'haze'
when 20
'fog'
when 21
'haze'
when 22
'haze'
when 23
'wind'
when 24
'wind'
when 25
'thermometer low'
when 26
'cloud'
when 27
'cloud moon'
when 28
'cloud sun'
when 29
'cloud moon'
when 30
'cloud sun'
when 31
'moon'
when 32
'sun'
when 33
'moon'
when 34
'sun'
when 35
'hail'
when 36
'thermometer full'
when 37
'lightning'
when 38
'lightning'
when 39
'lightning'
when 40
'rain'
when 41
'snow'
when 42
'snow'
when 43
'snow'
when 44
'cloud'
when 45
'lightning'
when 46
'snow'
when 47
'lightning'
end
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #47bbb3;
$value-color: #fff;
$title-color: rgba(255, 255, 255, 0.7);;
$moreinfo-color: rgba(255, 255, 255, 0.7);;
// ----------------------------------------------------------------------------
// Widget-weather styles
// ----------------------------------------------------------------------------
.widget-weather {
background-color: $background-color;
.title {
color: $title-color;
}
.temp {
color: $value-color;
}
.condition {
font-weight: 500;
font-size: 30px;
color: $value-color;
}
.updated-at {
color: rgba(0, 0, 0, 0.3);
}
}
@font-face {
font-family: 'Climacons-Font';
src:url('climacons-webfont.eot');
src:url('climacons-webfont.eot?#iefix') format('embedded-opentype'),
url('climacons-webfont.svg#Climacons-Font') format('svg'),
url('climacons-webfont.woff') format('woff'),
url('climacons-webfont.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
.climacon:before{
font-family: 'Climacons-Font';
speak: none;
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased;
}
.climacon.cloud:before {
content: "\e000";
}
.climacon.cloud.sun:before {
content: "\e001";
}
.climacon.cloud.moon:before {
content: "\e002";
}
.climacon.rain:before,
.climacon.rain.cloud:before {
content: "\e003";
}
.climacon.rain.sun:before,
.climacon.rain.cloud.sun:before {
content: "\e004";
}
.climacon.rain.moon:before,
.climacon.rain.cloud.moon:before {
content: "\e005";
}
.climacon.showers:before,
.climacon.showers.cloud:before {
content: "\e006";
}
.climacon.showers.sun:before,
.climacon.showers.cloud.sun:before {
content: "\e007";
}
.climacon.showers.moon:before,
.climacon.showers.cloud.moon:before {
content: "\e008";
}
.climacon.downpour:before,
.climacon.downpour.cloud:before {
content: "\e009";
}
.climacon.downpour.sun:before,
.climacon.downpour.cloud.sun:before {
content: "\e00a";
}
.climacon.downpour.moon:before,
.climacon.downpour.cloud.moon:before {
content: "\e00b";
}
.climacon.drizzle:before,
.climacon.drizzle.cloud:before {
content: "\e00c";
}
.climacon.drizzle.sun:before,
.climacon.drizzle.cloud.sun:before {
content: "\e00d";
}
.climacon.drizzle.moon:before,
.climacon.drizzle.cloud.moon:before {
content: "\e00e";
}
.climacon.sleet:before,
.climacon.sleet.cloud:before {
content: "\e00f";
}
.climacon.sleet.sun:before,
.climacon.sleet.cloud.sun:before {
content: "\e010";
}
.climacon.sleet.moon:before,
.climacon.sleet.cloud.moon:before {
content: "\e011";
}
.climacon.hail:before,
.climacon.hail.cloud:before {
content: "\e012";
}
.climacon.hail.sun:before,
.climacon.hail.cloud.sun:before {
content: "\e013";
}
.climacon.hail.moon:before,
.climacon.hail.cloud.moon:before {
content: "\e014";
}
.climacon.flurries:before,
.climacon.flurries.cloud:before {
content: "\e015";
}
.climacon.flurries.sun:before,
.climacon.flurries.cloud.sun:before {
content: "\e016";
}
.climacon.flurries.moon:before,
.climacon.flurries.cloud.moon:before {
content: "\e017";
}
.climacon.snow:before,
.climacon.snow.cloud:before {
content: "\e018";
}
.climacon.snow.sun:before,
.climacon.snow.cloud.sun:before {
content: "\e019";
}
.climacon.snow.moon:before,
.climacon.snow.cloud.moon:before {
content: "\e01a";
}
.climacon.fog:before,
.climacon.fog.cloud:before {
content: "\e01b";
}
.climacon.fog.sun:before,
.climacon.fog.cloud.sun:before {
content: "\e01c";
}
.climacon.fog.moon:before,
.climacon.fog.cloud.moon:before {
content: "\e01d";
}
.climacon.haze:before {
content: "\e01e";
}
.climacon.haze.sun:before {
content: "\e01f";
}
.climacon.haze.moon:before {
content: "\e020";
}
.climacon.wind:before {
content: "\e021";
}
.climacon.wind.cloud:before {
content: "\e022";
}
.climacon.wind.sun:before,
.climacon.wind.cloud.sun:before {
content: "\e023";
}
.climacon.wind.moon:before,
.climacon.wind.cloud.moon:before {
content: "\e024";
}
.climacon.lightning:before,
.climacon.lightning.cloud:before {
content: "\e025";
}
.climacon.lightning.sun:before,
.climacon.lightning.cloud.sun:before {
content: "\e026";
}
.climacon.lightning.moon:before,
.climacon.lightning.cloud.moon:before {
content: "\e027";
}
.climacon.sun:before {
content: "\e028";
}
.climacon.sun.set:before,
.climacon.sunset:before {
content: "\e029";
}
.climacon.sun.rise:before,
.climacon.sunrise:before {
content: "\e02a";
}
.climacon.sun.low:before,
.climacon.sun-low:before,
.climacon.low-sun:before {
content: "\e02b";
}
.climacon.sun.lower:before,
.climacon.sun-lower:before,
.climacon.lower-sun:before {
content: "\e02c";
}
.climacon.moon:before {
content: "\e02d";
}
.climacon.moon.new:before {
content: "\e02e";
}
.climacon.moon.waxing.crescent:before,
.climacon.moon.first-crescent:before {
content: "\e02f";
}
.climacon.moon.waxing.quarter:before,
.climacon.moon.first-quarter:before,
.climacon.moon.waxing.half:before,
.climacon.moon.first-half:before{
content: "\e030";
}
.climacon.moon.waxing.gibbous:before,
.climacon.moon.first-gibbous:before,
.climacon.moon.waxing.three-quarter:before,
.climacon.moon.first-three-quarter:before {
content: "\e031";
}
.climacon.moon.full:before {
content: "\e032";
}
.climacon.moon.waning.gibbous:before,
.climacon.moon.last-gibbous:before,
.climacon.moon.waning.three-quarter:before,
.climacon.moon.last-three-quarter:before {
content: "\e033";
}
.climacon.moon.waning.quarter:before,
.climacon.moon.last-quarter:before,
.climacon.moon.waning.half:before,
.climacon.moon.last-half:before {
content: "\e034";
}
.climacon.moon.waning.crescent:before,
.climacon.moon.last-crescent:before {
content: "\e035";
}
.climacon.snowflake:before {
content: "\e036";
}
.climacon.tornado:before {
content: "\e037";
}
.climacon.thermometer.empty:before,
.climacon.thermometer:before {
content: "\e038";
}
.climacon.thermometer.low:before {
content: "\e039";
}
.climacon.thermometer.medium-low:before {
content: "\e03a";
}
.climacon.thermometer.medium-high:before {
content: "\e03b";
}
.climacon.thermometer.high:before {
content: "\e03c";
}
.climacon.thermometer.full:before {
content: "\e03d";
}
.climacon.celcius:before {
content: "\e03e";
}
.climacon.farenheit:before {
content: "\e03f";
}
.climacon.compass:before {
content: "\e040";
}
.climacon.compass.north:before {
content: "\e041";
}
.climacon.compass.east:before {
content: "\e042";
}
.climacon.compass.south:before {
content: "\e043";
}
.climacon.compass.west:before {
content: "\e044";
}
.climacon.umbrella:before {
content: "\e045";
}
.climacon.sunglasses:before {
content: "\e046";
}
.climacon.cloud.cycle:before,
.climacon.cloud.refresh:before {
content: "\e047";
}
.climacon.cloud.down:before,
.climacon.cloud.download:before {
content: "\e048";
}
.climacon.cloud.up:before,
.climacon.cloud.upload:before {
content: "\e049";
}

jbfink commented Feb 20, 2013

I have the feeling I'm missing something completely terribly obvious, but when I try to run this widget on my Dashing, I get the following error in dashing start:

/home/vagrant/.rvm/gems/ruby-1.9.3-p362@dashing/gems/backports-2.8.2/lib/backports/tools.rb:318:in `require': cannot load such file -- xmlsimple (LoadError)

xml-simple gem is installed, and doing irb and then require 'xmlsimple' gives the correct results. Any ideas where it's munging up in dashing?

Same error reporting here after first pass. Will have a look into it further to see if anything obvious has been missed.

I've installed the xml-simple gem as I presume this is what it is referencing but no joy.

You will need to add "gem 'xml-simple'" without the surrounding double quotes into the Gemfile and then rerun "bundle" command. This will include the xml-simple gem. Simply installing the gem did not work for me either. I also suggest that you reduce the 15m delay to 1m to aide in your troubleshooting.

hope that helps :)

harishbsrinivas - that worked. Had to install the xml-simple gem then add it to the gemlock file and run bundle of course but the widget now works.

The only thing that's not being pulled through is the city / title but I can look into that later.

TheGraycat - I was able to add the Location to the weather widget using these changes: http://pastebin.com/WdpX8nw5

Owner

davefp commented Mar 7, 2013

Oh wow, apparently I don't get notified when people comment on this gist, I'll have to see if I can fix that.

@jbfink @thegreycat sorry about the confusion. I've only ever run the job in the context of a dashboard project that has xmlsimple in the gemfile. Weird that it's not working for you in irb.

@scubes13 mind if I add that code to the job? I didn't consider dynamically setting the location as I always just hardcoded it but your method is definitely more flexible (e.g. when dealing with WOEIDs from other sources).

@davefp - by all means - do with it as you please. ;)

Thanks again for the assistance in #dashing IRC channel! (On Freenode for any one else interested in joining us.)

You may not have noticed if this worked straight off the bat for you, but the job is set to run every 1s - you might want to change that to save any web proxies you sit behind being smashed...

Also related to the comment by @davestephens, the Yahoo APIs generally have a limit for public usage of 2000 requests per hour, which would be exceeded at 1 request per second (3600 requests per hour). I'm not 100% sure if the weather API is included in that limit or not, but it wouldn't be insane to have a default of once every 10 seconds:

SCHEDULER.every '10s', :first_in => 0 do |job|

the icon for the weather doesnty show up doesnt show up

sash85 commented Feb 2, 2015

Great Widget :-). Is it possible to change the language or add an extra language file?

Sometimes gives this error:

scheduler caught exception:
Connection reset by peer
/usr/lib/ruby/1.9.1/net/protocol.rb:141:in read_nonblock' /usr/lib/ruby/1.9.1/net/protocol.rb:141:inrbuf_fill'
/usr/lib/ruby/1.9.1/net/protocol.rb:122:in readuntil' /usr/lib/ruby/1.9.1/net/protocol.rb:132:inreadline'
/usr/lib/ruby/1.9.1/net/http.rb:2562:in read_status_line' /usr/lib/ruby/1.9.1/net/http.rb:2551:inread_new'
/usr/lib/ruby/1.9.1/net/http.rb:1319:in block in transport_request' /usr/lib/ruby/1.9.1/net/http.rb:1316:incatch'
/usr/lib/ruby/1.9.1/net/http.rb:1316:in transport_request' /usr/lib/ruby/1.9.1/net/http.rb:1293:inrequest'
/usr/lib/ruby/1.9.1/net/http.rb:1286:in block in request' /usr/lib/ruby/1.9.1/net/http.rb:745:instart'
/usr/lib/ruby/1.9.1/net/http.rb:1284:in request' /home/hornenj/sweet_dashboard_project/jobs/weather.rb:17:inblock in <top (required)>'
/home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in call' /home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:intrigger_block'
/home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:in block in trigger' /home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:incall'
/home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in `block in trigger_job'

Yahoo is now requiring authentication for querying weather data. Interesting to watch them circle the drain...

<yahoo:error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xml:lang="en-US" yahoo:uri="http://yahoo.com">
<yahoo:description>
Please provide valid credentials. OAuth oauth_problem="OST_OAUTH_PARAMETER_ABSENT_ERROR", realm="yahooapis.com"
</yahoo:description>
<yahoo:detail>
Please provide valid credentials. OAuth oauth_problem="OST_OAUTH_PARAMETER_ABSENT_ERROR", realm="yahooapis.com"
</yahoo:detail>
</yahoo:error>

I keep getting these. Any ideas?

scheduler caught exception:
undefined method []' for nil:NilClass /home/hornenj/sweet_dashboard_project/jobs/weather.rb:18:inblock in <top (required)>'
/home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in call' /home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:intrigger_block'
/home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:in block in trigger' /home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:incall'
/home/hornenj/.gems/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in `block in trigger_job'

Actually looking around I think that's a symptom of your post 4 days ago and the need to get some sort of credential. Now to work out how to do that and where to plug it in.

Hi!
I get the following error since a few days. The widget worked wonderful before:

scheduler caught exception:
undefined method []' for nil:NilClass C:/svv/jobs/weather.rb:17:inblock in <top (required)>'
C:/Ruby/lib/ruby/gems/2.2.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230
:in call' C:/Ruby/lib/ruby/gems/2.2.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230 :intrigger_block'
C:/Ruby/lib/ruby/gems/2.2.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204
:in block in trigger' C:/Ruby/lib/ruby/gems/2.2.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.r b:430:incall'
C:/Ruby/lib/ruby/gems/2.2.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.r
b:430:in `block in trigger_job'

Looks like the same problem as nigelhorne has,

Any solutions?

Hi,

Couldnt plug in the authentication yet, but have a small workaround which works for me, may be it helps.
Please adjust the variables (city, widget url, authentication token etc)

URL='http://www.accuweather.com/en/se/stockholm/314929/weather-forecast/314929'
city="Stockholm"

result=( $(wget -q -O- "$URL" | grep acm_RecentLocationsCarousel.push | grep ${city} | awk -F\' '{printf("%s %s %s",$(NF-5),$(NF-3),$(NF-1));}') )

dStamp=$(date +'%s')
state=${result[@]:2}
temp=${result[0]}
feel=${result[1]}

cmd="curl -d '{ \"auth_token\": \"YOUR_AUTH_TOKEN\", \"temp\":\"${temp}&deg;C (${feel}&deg;C)\",\"condition\":\"${state}\",\"title\":\"${city}\",\"climacon\":\"$( echo ${state} | tr '[:upper:]' '[:lower:]')\",\"id\":\"weather\",\"updatedAt\":${dStamp} }' http://localhost:3030/widgets/weather"

eval $cmd

hkraji commented Apr 6, 2016

As noted above Yahoo API is not working properly, on top of that they've added Oauth so it requires more complex implementation. I've made changes to weather.rb now it uses Open Weather API and also repackaged this widget in my own solution here https://github.com/hkraji/weather-widget .

uninittialized constant SCHEDULER (NameError) helpme

@hkraji: Any idea how to add weather forecast (over n days) to yours?

Montaxx commented Jan 24, 2017

here's a fixed and improved version: https://github.com/Montaxx/weather-widget

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