Debian Codegraph @

vicious/contrib/openweather_all.lua

raw symbols SCIP
1 -- contrib/openweather_all.lua
2 -- Copyright (C) 2013 NormalRa <normalrawr gmail com>
3 -- Copyright (C) 2017 Jörg Thalheim <[email protected]>
4 -- Copyright (C) 2020 Marcel Arpogaus <marcel.arpogaus gmail com>
5 --
6 -- This file is part of Vicious.
7 --
8 -- Vicious is free software: you can redistribute it and/or modify
9 -- it under the terms of the GNU General Public License as
10 -- published by the Free Software Foundation, either version 2 of the
11 -- License, or (at your option) any later version.
12 --
13 -- Vicious is distributed in the hope that it will be useful,
14 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
15 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 -- GNU General Public License for more details.
17 --
18 -- You should have received a copy of the GNU General Public License
19 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
20 -- {{{ Grab environment
21 local tonumber = tonumber
22 local string = {match = string.match}
23 local math = {ceil = math.ceil, floor = math.floor}
24 local helpers = require "vicious.helpers"
25 local spawn = require "vicious.spawn"
26 -- }}}
27
28 -- Openweather: provides weather information for a requested station
29 -- vicious.widgets.openweather
30 local openweather_all = {}
31
32 -- Initialize function tables
33 local _wdirs = {"N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"}
34 local _wdata = {
35 ["{city}"] = "N/A",
36 ["{wind deg}"] = "N/A",
37 ["{wind aim}"] = "N/A",
38 ["{wind mps}"] = "N/A",
39 ["{wind kmh}"] = "N/A",
40 ["{sky}"] = "N/A",
41 ["{weather}"] = "N/A",
42 ["{temp c}"] = "N/A",
43 ["{temp min c}"] = "N/A",
44 ["{temp max c}"] = "N/A",
45 ["{sunrise}"] = -1,
46 ["{sunset}"] = -1,
47 ["{humid}"] = "N/A",
48 ["{press}"] = "N/A"
49 }
50
51 -- {{{ Openweather widget type
52 local function parse(stdout, stderr, exitreason, exitcode)
53 -- Check if there was a timeout or a problem with the station
54 if stdout == nil or exitcode ~= 0 then return _wdata end
55
56 _wdata["{city}"] = -- City name
57 string.match(stdout, '"name":"([%a%s%-]+)"') or _wdata["{city}"]
58 _wdata["{wind deg}"] = -- Wind degrees
59 string.match(stdout, '"deg":([%d]+)') or _wdata["{wind deg}"]
60 _wdata["{wind mps}"] = -- Wind speed in meters per second
61 string.match(stdout, '"speed":([%d%.]+)') or _wdata["{wind mps}"]
62 _wdata["{sky}"] = -- Sky conditions
63 string.match(stdout, '"main":"([%a]+)"') or _wdata["{sky}"]
64 _wdata["{weather}"] = -- Weather description
65 string.match(stdout, '"description":"([%a%s]+)"') or _wdata["{weather}"]
66 _wdata["{temp c}"] = -- Temperature in celsius
67 string.match(stdout, '"temp":([%-]?[%d%.]+)') or _wdata["{temp c}"]
68 _wdata["{temp min c}"] = -- Minimal Temperature in celsius
69 string.match(stdout, '"temp_min":([%-]?[%d%.]+)') or _wdata["{temp min c}"]
70 _wdata["{temp max c}"] = -- Maximal Temperature in celsius
71 string.match(stdout, '"temp_max":([%-]?[%d%.]+)') or _wdata["{temp max c}"]
72 _wdata["{humid}"] = -- Relative humidity in percent
73 string.match(stdout, '"humidity":([%d]+)') or _wdata["{humid}"]
74 _wdata["{sunrise}"] = -- Sunrise
75 tonumber(string.match(stdout, '"sunrise":([%d]+)')) or _wdata["{sunrise}"]
76 _wdata["{sunset}"] = -- Sunset
77 tonumber(string.match(stdout, '"sunset":([%d]+)')) or _wdata["{sunset}"]
78 _wdata["{press}"] = -- Pressure in hPa
79 string.match(stdout, '"pressure":([%d%.]+)') or _wdata["{press}"]
80
81 -- Wind speed in km/h
82 if _wdata["{wind mps}"] ~= "N/A" then
83 _wdata["{wind mps}"] = math.floor(tonumber(_wdata["{wind mps}"]) + .5)
84 _wdata["{wind kmh}"] = math.ceil(_wdata["{wind mps}"] * 3.6)
85 end -- Temperature in °C
86 if _wdata["{temp c}"] ~= "N/A" then
87 _wdata["{temp c}"] = math.floor(tonumber(_wdata["{temp c}"]) + .5)
88 end -- Calculate wind direction
89 if _wdata["{wind deg}"] ~= "N/A" then
90 _wdata["{wind deg}"] = tonumber(_wdata["{wind deg}"])
91
92 -- Lua tables start at [1]
93 if (_wdata["{wind deg}"] / 45) % 1 == 0 then
94 _wdata["{wind aim}"] = _wdirs[_wdata["{wind deg}"] / 45 + 1]
95 else
96 _wdata["{wind aim}"] = _wdirs[math.ceil(_wdata["{wind deg}"] / 45) +
97 1] ..
98 _wdirs[math.floor(
99 _wdata["{wind deg}"] / 45) + 1]
100 end
101 end
102
103 return _wdata
104 end
105
106 function openweather_all.async(format, warg, callback)
107 if not warg then return callback {} end
108 if type(warg) ~= "table" then return callback {} end
109
110 -- Get weather forceast using the city ID code, from:
111 -- * OpenWeatherMap.org
112 local openweather = "http://api.openweathermap.org/data/2.5/weather?id=" ..
113 warg.city_id .. "&appid=" .. warg.app_id ..
114 "&mode=json&units=metric"
115
116 spawn.easy_async("curl --connect-timeout 1 -fsm 3 '" .. openweather .. "'",
117 function(...) callback(parse(...)) end)
118 end
119 -- }}}
120
121 return helpers.setasyncall(openweather_all)