Skip to content

Instantly share code, notes, and snippets.

@Coro365
Last active March 7, 2020 17:08
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 Coro365/f6f7615418a35a1d180447587217571e to your computer and use it in GitHub Desktop.
Save Coro365/f6f7615418a35a1d180447587217571e to your computer and use it in GitHub Desktop.
Show current weather icon.

Usage example DarkSky and Skycon

Show current weather icon.

Data Flow

  1. DarkSky API
  2. post_darksky_to_influxdb.rb
  3. influxdb
  4. current_weather.html

Usage

  1. Install influxdb
  2. Create database
  3. Get DarkSky API key
  4. Edit key, latitude, longitude in post_darksky_to_influxdb.rb
  5. Edit addr var in current_weather.html
  6. Run post_darksky_to_influxdb.rb (Regular execution recommended by cron)
  7. Open current_weather.html

Reference

License

  • MIT
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.weather-icon {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
margin: auto;}
</style>
</head>
<body>
<figure class="icon">
<canvas class="weather-icon" id="icon1" width="170" height="170"></canvas>
</figure>
<script src="https://darkskyapp.github.io/skycons/skycons.js"></script>
<script>
var iconType = "",
skycons = new Skycons({"color": "blak"});
function getWeathrIcon() {
var addr = "http://hostname.local:8086/query?",
db = "db=darksky&",
query = "q=SELECT%20icon%20FROM%20%22current_hour%22%20order%20by%20desc%20limit%201",
url = addr + db + query;
var xlm = new XMLHttpRequest();
xlm.open("GET", url, true);
xlm.send();
xlm.onreadystatechange = function () {
if (xlm.readyState === 4 && xlm.status === 200) {
dbResponse = JSON.parse(xlm.responseText);
iconType = dbResponse.results[0].series[0].values[0][1];
iconPlay();
}
};}
function iconPlay() {
console.log(iconType);
if (iconType === "") {
console.log("ERROR");
} else {
skycons.set("icon1", iconType);
skycons.play();
}
}
getWeathrIcon();
var iconReloadID = window.setInterval(getWeathrIcon, 60000);
</script>
</body>
</html>
require 'json'
require 'open-uri'
INFLUXDB_ADRR = 'http://hostname.local:8086/write?db=darksky'.freeze
def darksky_download
domain = 'https://api.darksky.net'
type = 'forecast'
key = '<YOUR_KEY>'
latitude = '<YOUR_latitude>'
longitude = '<YOUR_longitude>'
location = [latitude, longitude].join(',')
language = 'lang=ja'
units = 'units=auto'
exclude = 'exclude=currently'
param = '?' + [language, units, exclude].join('&')
url = [domain, type, key, location].join('/') + param
JSON.parse(URI.open(url).read)
end
def influx_post(period, timezone, data)
data.store('timeZone', timezone)
data = data.transform_keys { |k| k == 'time' ? 'forecastTime' : k }
field = data.reject { |k, _v| k == 'forecastTime' || k == 'timeZone' }
tag = data.select { |k, _v| k == 'forecastTime' || k == 'timeZone' }
field = key_value_join(field)
tag = key_value_join(tag)
payload = [[period, tag].join(','), field.join(',')].join("\s")
cmd = ['curl', '-i', '-XPOST', INFLUXDB_ADRR, '--data-binary', payload]
system(*cmd)
end
def key_value_join(hash)
dq = '"'
hash.map { |k, v| [k, dq + v.to_s + dq].join('=') }
end
weather = darksky_download
timezone = weather['timezone']
influx_post('current_hour', timezone, weather['hourly']['data'][0])
influx_post('today', timezone, weather['daily']['data'][0])
this_week = { 'time' => weather['daily']['data'][0]['time'],
'summary' => weather['daily']['summary'],
'icon' => weather['daily']['icon'] }
influx_post('this_week', timezone, this_week)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment