Skip to content

Instantly share code, notes, and snippets.

@davefp
Last active April 1, 2021 04:30
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save davefp/4990174 to your computer and use it in GitHub Desktop.
Save davefp/4990174 to your computer and use it in GitHub Desktop.
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";
}
@LostCoder11
Copy link

The site listed for getting WOEID doesn't works anymore, I have found an amazing alternative. Find Your WOEID here: https://nations24.com. Make change in the gist if possible.

@LostCoder11
Copy link

Yahoo discontinued their APIs. Switch to another Widget for weather.

Check out https://weathercodes.nations24.com/

@aymanebarka
Copy link

Here is an up-to-date version of this widget working with OpenWeatherMap API.
I made also some customisations concerning the icons : https://github.com/aymanebarka/Weather-Dashing

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