├── README.md ├── config.json ├── icons ├── .DS_Store ├── cloud-1.png ├── cloud.png ├── clouds.png ├── cloudy-1.png ├── cloudy-2.png ├── cloudy-3.png ├── cloudy-4.png ├── cloudy-5.png ├── cloudy.png ├── cold-1.png ├── cold.png ├── dawn-1.png ├── dawn.png ├── drizzle.png ├── hail.png ├── half-moon.png ├── haze.png ├── lightning-1.png ├── lightning.png ├── mist.png ├── moon-1.png ├── moon-2.png ├── moon.png ├── na.png ├── night-2.png ├── night-3.png ├── night.png ├── rain-1.png ├── rain-3.png ├── rain-4.png ├── rain-5.png ├── rain-6.png ├── rain-7.png ├── rain-8.png ├── rain.png ├── sleet.png ├── snowflake-1.png ├── snowflake-2.png ├── snowflake.png ├── storm-1.png ├── storm-2.png ├── storm-3.png ├── storm-4.png ├── storm-5.png ├── storm.png ├── sun-1.png ├── sun-2.png ├── sun-4.png ├── sun-5.png ├── sun.png ├── sunset.png ├── temperature-1.png ├── temperature-2.png ├── temperature-4.png ├── temperature.png ├── thermometer-1.png ├── thermometer-2.png ├── thermometer.png ├── tornado.png ├── warning.png ├── water-1.png ├── wind-1.png ├── wind.png ├── windy-1.png ├── windy-2.png ├── windy-3.png └── windy.png └── init.lua /README.md: -------------------------------------------------------------------------------- 1 | # hs-weather 2 | 3 | Weather menubar app for [hammerspoon](http://www.hammerspoon.org) users. 4 | 5 | :warning: **DEPRECATED** 6 | 7 | _Important EOL Notice: As of Thursday, Jan. 3, 2019, the weather.yahooapis.com and query.yahooapis.com for Yahoo Weather API will be retired. 8 | To continue using our free Yahoo Weather APIs, use https://weather-ydn-yql.media.yahoo.com/forecastrss._ 9 | 10 | ### Setup 11 | 12 | 1. Copy the repo to your hammerspoon folder. 13 | ```cp -r hs-weather ~/.hammerspoon``` 14 | 2. Add this to your init.lua 15 | ``` 16 | local weather = require("hs-weather") 17 | weather.start() 18 | ``` 19 | 3. Modify config.json to suit your needs 20 | - ```geolocation: true``` => if hammerspoon has location services enabled, will get weather for current location and the ```location``` parameter will be ignored 21 | 22 | Now the weather should be shown in the menu bar. 23 | 24 | [![Screen Shot 2016-08-21 at 2.37.41 PM.png](https://s10.postimg.org/e9djfzq6x/Screen_Shot_2016_08_21_at_2_37_41_PM.png)](https://postimg.org/image/rdj3soi8l/) 25 | 26 | **NOTES** 27 | - click updates weather 28 | - hover shows last yahoo update for the location 29 | - if right after opening hammerspoon it shows N/A, just click to retry (this happens because sometimes yahoo returns an empty response for the location) 30 | - Config can also be passed as an argument to weather.start() e.g. 31 | ``` 32 | local weather = require("hs-weather") 33 | weather.start({ 34 | geolocation = true, 35 | units = 'F' 36 | }) 37 | ``` 38 | - Default refresh is 5 minutes 39 | - Disable by calling ```weather.stop()``` 40 | 41 | ### Credits 42 | 43 | Weather data: [Yahoo Weather API](https://developer.yahoo.com/weather/). It does not require an api key. 44 | Icons by RNS, Freepik, Vectors Market, Yannick at http://www.flaticon.com 45 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "geolocation": false, 3 | "location": "Berlin, DE", 4 | "refresh": 300, 5 | "units": "C" 6 | } 7 | -------------------------------------------------------------------------------- /icons/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/.DS_Store -------------------------------------------------------------------------------- /icons/cloud-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cloud-1.png -------------------------------------------------------------------------------- /icons/cloud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cloud.png -------------------------------------------------------------------------------- /icons/clouds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/clouds.png -------------------------------------------------------------------------------- /icons/cloudy-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cloudy-1.png -------------------------------------------------------------------------------- /icons/cloudy-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cloudy-2.png -------------------------------------------------------------------------------- /icons/cloudy-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cloudy-3.png -------------------------------------------------------------------------------- /icons/cloudy-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cloudy-4.png -------------------------------------------------------------------------------- /icons/cloudy-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cloudy-5.png -------------------------------------------------------------------------------- /icons/cloudy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cloudy.png -------------------------------------------------------------------------------- /icons/cold-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cold-1.png -------------------------------------------------------------------------------- /icons/cold.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/cold.png -------------------------------------------------------------------------------- /icons/dawn-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/dawn-1.png -------------------------------------------------------------------------------- /icons/dawn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/dawn.png -------------------------------------------------------------------------------- /icons/drizzle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/drizzle.png -------------------------------------------------------------------------------- /icons/hail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/hail.png -------------------------------------------------------------------------------- /icons/half-moon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/half-moon.png -------------------------------------------------------------------------------- /icons/haze.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/haze.png -------------------------------------------------------------------------------- /icons/lightning-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/lightning-1.png -------------------------------------------------------------------------------- /icons/lightning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/lightning.png -------------------------------------------------------------------------------- /icons/mist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/mist.png -------------------------------------------------------------------------------- /icons/moon-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/moon-1.png -------------------------------------------------------------------------------- /icons/moon-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/moon-2.png -------------------------------------------------------------------------------- /icons/moon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/moon.png -------------------------------------------------------------------------------- /icons/na.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/na.png -------------------------------------------------------------------------------- /icons/night-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/night-2.png -------------------------------------------------------------------------------- /icons/night-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/night-3.png -------------------------------------------------------------------------------- /icons/night.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/night.png -------------------------------------------------------------------------------- /icons/rain-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/rain-1.png -------------------------------------------------------------------------------- /icons/rain-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/rain-3.png -------------------------------------------------------------------------------- /icons/rain-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/rain-4.png -------------------------------------------------------------------------------- /icons/rain-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/rain-5.png -------------------------------------------------------------------------------- /icons/rain-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/rain-6.png -------------------------------------------------------------------------------- /icons/rain-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/rain-7.png -------------------------------------------------------------------------------- /icons/rain-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/rain-8.png -------------------------------------------------------------------------------- /icons/rain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/rain.png -------------------------------------------------------------------------------- /icons/sleet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/sleet.png -------------------------------------------------------------------------------- /icons/snowflake-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/snowflake-1.png -------------------------------------------------------------------------------- /icons/snowflake-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/snowflake-2.png -------------------------------------------------------------------------------- /icons/snowflake.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/snowflake.png -------------------------------------------------------------------------------- /icons/storm-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/storm-1.png -------------------------------------------------------------------------------- /icons/storm-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/storm-2.png -------------------------------------------------------------------------------- /icons/storm-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/storm-3.png -------------------------------------------------------------------------------- /icons/storm-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/storm-4.png -------------------------------------------------------------------------------- /icons/storm-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/storm-5.png -------------------------------------------------------------------------------- /icons/storm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/storm.png -------------------------------------------------------------------------------- /icons/sun-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/sun-1.png -------------------------------------------------------------------------------- /icons/sun-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/sun-2.png -------------------------------------------------------------------------------- /icons/sun-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/sun-4.png -------------------------------------------------------------------------------- /icons/sun-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/sun-5.png -------------------------------------------------------------------------------- /icons/sun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/sun.png -------------------------------------------------------------------------------- /icons/sunset.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/sunset.png -------------------------------------------------------------------------------- /icons/temperature-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/temperature-1.png -------------------------------------------------------------------------------- /icons/temperature-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/temperature-2.png -------------------------------------------------------------------------------- /icons/temperature-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/temperature-4.png -------------------------------------------------------------------------------- /icons/temperature.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/temperature.png -------------------------------------------------------------------------------- /icons/thermometer-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/thermometer-1.png -------------------------------------------------------------------------------- /icons/thermometer-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/thermometer-2.png -------------------------------------------------------------------------------- /icons/thermometer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/thermometer.png -------------------------------------------------------------------------------- /icons/tornado.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/tornado.png -------------------------------------------------------------------------------- /icons/warning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/warning.png -------------------------------------------------------------------------------- /icons/water-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/water-1.png -------------------------------------------------------------------------------- /icons/wind-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/wind-1.png -------------------------------------------------------------------------------- /icons/wind.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/wind.png -------------------------------------------------------------------------------- /icons/windy-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/windy-1.png -------------------------------------------------------------------------------- /icons/windy-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/windy-2.png -------------------------------------------------------------------------------- /icons/windy-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/windy-3.png -------------------------------------------------------------------------------- /icons/windy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skamsie/hs-weather/8482d388a5d94149dd949d61338e0f34e853ef1f/icons/windy.png -------------------------------------------------------------------------------- /init.lua: -------------------------------------------------------------------------------- 1 | local m = {} 2 | 3 | local hammerDir = hs.fs.currentDir() 4 | local iconsDir = (hammerDir .. '/hs-weather/icons/') 5 | local configFile = (hammerDir .. '/hs-weather/config.json') 6 | local urlBase = 'https://query.yahooapis.com/v1/public/yql?q=' 7 | local query = 'select item.title, item.condition from weather.forecast where \ 8 | woeid in (select woeid from geo.places(1) where text="' 9 | 10 | -- https://developer.yahoo.com/weather/archive.html#codes 11 | -- icons by RNS, Freepik, Vectors Market, Yannick at http://www.flaticon.com 12 | local weatherSymbols = { 13 | [0] = (iconsDir .. 'tornado.png'), -- tornado 14 | [1] = (iconsDir .. 'storm.png'), -- tropical storm 15 | [2] = (iconsDir .. 'tornado.png'), -- hurricane 16 | [3] = (iconsDir .. 'storm-5.png'), -- severe thunderstorms 17 | [4] = (iconsDir .. 'storm-4.png'), -- thunderstorms 18 | [5] = (iconsDir .. 'sleet.png'), -- mixed rain and snow 19 | [6] = (iconsDir .. 'sleet.png'), -- mixed rain and sleet 20 | [7] = (iconsDir .. 'sleet.png'), -- mixed snow and sleet 21 | [8] = (iconsDir .. 'drizzle.png'), -- freezing drizzle 22 | [9] = (iconsDir .. 'drizzle.png'), -- drizzle 23 | [10] = (iconsDir .. 'drizzle.png'), -- freezing rain 24 | [11] = (iconsDir .. 'rain-1.png'), -- showers 25 | [12] = (iconsDir .. 'rain-1.png'), -- showers 26 | [13] = (iconsDir .. 'snowflake.png'), -- snow flurries 27 | [14] = (iconsDir .. 'snowflake.png'), -- light snow showers 28 | [15] = (iconsDir .. 'snowflake.png'), -- blowing snow 29 | [16] = (iconsDir .. 'snowflake.png'), -- snow 30 | [17] = (iconsDir .. 'hail.png'), -- hail 31 | [18] = (iconsDir .. 'sleet.png'), -- sleet 32 | [19] = (iconsDir .. 'haze.png'), -- dust 33 | [20] = (iconsDir .. 'mist.png'), -- foggy 34 | [21] = (iconsDir .. 'haze.png'), -- haze 35 | [22] = (iconsDir .. 'mist.png'), -- smoky 36 | [23] = (iconsDir .. 'wind-1.png'), -- blustery 37 | [24] = (iconsDir .. 'windy-1.png'), -- windy 38 | [25] = (iconsDir .. 'cold.png'), -- cold 39 | [26] = (iconsDir .. 'clouds.png'), -- cloudy 40 | [27] = (iconsDir .. 'night.png'), -- mostly cloudy (night) 41 | [28] = (iconsDir .. 'cloudy.png'), -- mostly cloudy (day) 42 | [29] = (iconsDir .. 'cloudy-4.png'), -- partly cloudy (night) 43 | [30] = (iconsDir .. 'cloudy-5.png'), -- partly cloudy (day) 44 | [31] = (iconsDir .. 'moon-2.png'), -- clear (night) 45 | [32] = (iconsDir .. 'sun-1.png'), -- sunny 46 | [33] = (iconsDir .. 'night-2.png'), -- fair (night) 47 | [34] = (iconsDir .. 'cloudy-1.png'), -- fair (day) 48 | [35] = (iconsDir .. 'hail.png'), -- mixed rain and hail 49 | [36] = (iconsDir .. 'temperature.png'), -- hot 50 | [37] = (iconsDir .. 'storm-4.png'), -- isolated thunderstorms 51 | [38] = (iconsDir .. 'storm-2.png'), -- scattered thunderstorms 52 | [39] = (iconsDir .. 'rain-3.png'), -- scattered thunderstorms 53 | [40] = (iconsDir .. 'rain-6.png'), -- scattered showers 54 | [41] = (iconsDir .. 'snowflake.png'), -- heavy snow 55 | [42] = (iconsDir .. 'snowflake.png'), -- scattered snow showers 56 | [43] = (iconsDir .. 'snowflake.png'), -- heavy snow 57 | [44] = (iconsDir .. 'cloudy.png'), -- party cloudy 58 | [45] = (iconsDir .. 'storm.png'), -- thundershowers 59 | [46] = (iconsDir .. 'snowflake.png'), -- snow showers 60 | [47] = (iconsDir .. 'lightning.png'), -- isolated thundershowers 61 | [3200] = (iconsDir .. 'na.png') -- not available 62 | } 63 | 64 | local function readConfig(file) 65 | local f = io.open(file, "rb") 66 | if not f then 67 | return {} 68 | end 69 | local content = f:read("*all") 70 | f:close() 71 | return hs.json.decode(content) 72 | end 73 | 74 | local function setWeatherIcon(app, code) 75 | local iconPath = weatherSymbols[code] 76 | local size = {w=16,h=16} 77 | if iconPath ~= nil then 78 | app:setIcon(hs.image.imageFromPath(iconPath):setSize(size)) 79 | else 80 | app:setIcon(hs.image.imageFromPath(weatherSymbols[3200]):setSize(size)) 81 | end 82 | end 83 | 84 | local function toCelsius(f) 85 | return (f - 32) * 5 / 9 86 | end 87 | 88 | local function setWeatherTitle(app, unitSys, temp) 89 | if unitSys == 'C' then 90 | local tempCelsius = toCelsius(temp) 91 | local tempRounded = math.floor(tempCelsius * 10 + 0.5) / 10 92 | app:setTitle(tempRounded .. ' °C ') 93 | else 94 | app:setTitle(temp .. ' °F ') 95 | end 96 | end 97 | 98 | local function urlencode(str) 99 | if (str) then 100 | str = string.gsub (str, "\n", "\r\n") 101 | str = string.gsub (str, "([^%w ])", 102 | function (c) return string.format ("%%%02X", string.byte(c)) end) 103 | str = string.gsub (str, " ", "+") 104 | end 105 | return str 106 | end 107 | 108 | local function getWeather(location) 109 | local weatherEndpoint = ( 110 | urlBase .. urlencode(query .. location .. '")') .. '&format=json') 111 | return hs.http.get(weatherEndpoint) 112 | end 113 | 114 | local function setWeatherForLocation(location, unitSys) 115 | local weatherEndpoint = ( 116 | urlBase .. urlencode(query .. location .. '")') .. '&format=json') 117 | hs.http.asyncGet(weatherEndpoint, nil, 118 | function(code, body, table) 119 | if code ~= 200 then 120 | print('-- hs-weather: Could not get weather. Response code: ' .. code) 121 | else 122 | print('-- hs-weather: Weather for ' .. location .. ': ' .. body) 123 | local response = hs.json.decode(body) 124 | if response.query.results == nil then 125 | if m.weatherApp:title() == '' then 126 | setWeatherIcon(m.weatherApp, 3200) 127 | end 128 | else 129 | local temp = response.query.results.channel.item.condition.temp 130 | local code = tonumber(response.query.results.channel.item.condition.code) 131 | local condition = response.query.results.channel.item.condition.text 132 | local title = response.query.results.channel.item.title 133 | setWeatherIcon(m.weatherApp, code) 134 | setWeatherTitle(m.weatherApp, unitSys, temp) 135 | m.weatherApp:setTooltip((title .. '\n' .. 'Condition: ' .. condition)) 136 | end 137 | end 138 | end 139 | ) 140 | end 141 | 142 | -- Get weather for current location 143 | -- Hammerspoon needs access to OS location services 144 | local function setWeatherForCurrentLocation(unitSys) 145 | if hs.location.servicesEnabled() then 146 | hs.location.start() 147 | hs.timer.doAfter(1, 148 | function () 149 | local loc = hs.location.get() 150 | hs.location.stop() 151 | setWeatherForLocation( 152 | '(' .. loc.latitude .. ',' .. loc.longitude .. ')', unitSys) 153 | end) 154 | else 155 | print('\n-- Location services disabled!\n') 156 | end 157 | end 158 | 159 | local function setWeather() 160 | if m.config.geolocation then 161 | setWeatherForCurrentLocation(m.config.units) 162 | else 163 | setWeatherForLocation(m.config.location, m.config.units) 164 | end 165 | end 166 | 167 | m.start = function(cfg) 168 | m.config = cfg or readConfig(configFile) 169 | 170 | -- defaults if not set 171 | m.config.refresh = m.config.refresh or 300 172 | m.config.units = m.config.units or 'C' 173 | m.config.location = m.config.location or 'Berlin, DE' 174 | 175 | m.weatherApp = hs.menubar.new() 176 | setWeather() 177 | 178 | -- refresh on click 179 | m.weatherApp:setClickCallback(function () setWeather() end) 180 | 181 | m.timer = hs.timer.doEvery( 182 | m.config.refresh, function () setWeather() end) 183 | end 184 | 185 | m.stop = function() 186 | m.timer:stop() 187 | end 188 | 189 | return m 190 | --------------------------------------------------------------------------------