| 1 |
|
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 29 |
|
| 30 |
local openweather_all = {} |
| 31 |
|
| 32 |
|
| 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 |
|
| 52 |
local function parse(stdout, stderr, exitreason, exitcode) |
| 53 |
|
| 54 |
if stdout == nil or exitcode ~= 0 then return _wdata end |
| 55 |
|
| 56 |
_wdata["{city}"] = |
| 57 |
string.match(stdout, '"name":"([%a%s%-]+)"') or _wdata["{city}"] |
| 58 |
_wdata["{wind deg}"] = |
| 59 |
string.match(stdout, '"deg":([%d]+)') or _wdata["{wind deg}"] |
| 60 |
_wdata["{wind mps}"] = |
| 61 |
string.match(stdout, '"speed":([%d%.]+)') or _wdata["{wind mps}"] |
| 62 |
_wdata["{sky}"] = |
| 63 |
string.match(stdout, '"main":"([%a]+)"') or _wdata["{sky}"] |
| 64 |
_wdata["{weather}"] = |
| 65 |
string.match(stdout, '"description":"([%a%s]+)"') or _wdata["{weather}"] |
| 66 |
_wdata["{temp c}"] = |
| 67 |
string.match(stdout, '"temp":([%-]?[%d%.]+)') or _wdata["{temp c}"] |
| 68 |
_wdata["{temp min c}"] = |
| 69 |
string.match(stdout, '"temp_min":([%-]?[%d%.]+)') or _wdata["{temp min c}"] |
| 70 |
_wdata["{temp max c}"] = |
| 71 |
string.match(stdout, '"temp_max":([%-]?[%d%.]+)') or _wdata["{temp max c}"] |
| 72 |
_wdata["{humid}"] = |
| 73 |
string.match(stdout, '"humidity":([%d]+)') or _wdata["{humid}"] |
| 74 |
_wdata["{sunrise}"] = |
| 75 |
tonumber(string.match(stdout, '"sunrise":([%d]+)')) or _wdata["{sunrise}"] |
| 76 |
_wdata["{sunset}"] = |
| 77 |
tonumber(string.match(stdout, '"sunset":([%d]+)')) or _wdata["{sunset}"] |
| 78 |
_wdata["{press}"] = |
| 79 |
string.match(stdout, '"pressure":([%d%.]+)') or _wdata["{press}"] |
| 80 |
|
| 81 |
|
| 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 C |
| 86 |
if _wdata["{temp c}"] ~= "N/A" then |
| 87 |
_wdata["{temp c}"] = math.floor(tonumber(_wdata["{temp c}"]) + .5) |
| 88 |
end |
| 89 |
if _wdata["{wind deg}"] ~= "N/A" then |
| 90 |
_wdata["{wind deg}"] = tonumber(_wdata["{wind deg}"]) |
| 91 |
|
| 92 |
|
| 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 |
|
| 111 |
|
| 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) |