-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwttrin.lua
More file actions
90 lines (78 loc) · 2.82 KB
/
wttrin.lua
File metadata and controls
90 lines (78 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require "colorama"
require "http"
local preferences = inline:getDefaultSharedPreferences()
local DEFAULT_URL_PARAMS = "?m?M?0?q?T&lang=en"
local HEADERS = { ["User-Agent"] = "python-requests/2.32.3" }
local function weather(_, query)
query:answer("Loading...")
local city = query:getArgs()
if city == "" then
city = preferences:getString("wttr_in_city", "")
end
local params = preferences:getString("wttr_in_url_params", DEFAULT_URL_PARAMS)
local hideFirstLine = preferences:getBoolean("wttr_in_hide_first_line", false)
http.get({
url = "https://wttr.in/" .. city .. params,
headers = HEADERS
},
function(_, _, text)
if hideFirstLine then
text = text:gsub("^[^\n]*\n", "")
else
text = "Weather report: " .. text
end
local formatted = text:gsub("\n", "<br>"):gsub(" ", " ")
query:answer("<pre>" .. formatted .. "</pre>")
end,
function(_)
query:answer "Error occurred"
end)
end
return function(module)
module:setCategory("wttr.in")
module:registerCommand("weather", colorama.wrap(weather), "Display the current weather for a specified city")
module:registerPreferences(function(prefs)
local helpButton
helpButton = prefs.button("Help", function()
helpButton:setEnabled(false)
http.get({
url = "https://wttr.in/:help",
headers = HEADERS
},
function(_, _, text)
helpButton:setEnabled(true)
prefs:create("Help", function(prefs2)
return {
prefs.spacer(16),
text,
prefs.button("I'm done", function()
prefs2:cancel()
end),
prefs.spacer(16)
}
end)
end,
function(_)
helpButton:setEnabled(true)
inline:toast "Error occurred"
end)
end)
return {
prefs.textInput("wttr_in_url_params", "wttr.in URL parameters")
:setDefault(DEFAULT_URL_PARAMS),
prefs.spacer(8),
helpButton,
prefs.spacer(8),
prefs.textInput("wttr_in_city", "Default city"),
prefs.spacer(4),
prefs.checkBox("wttr_in_hide_first_line", "Hide city")
:setDefault(false),
prefs.spacer(4)
}
end)
local aliases = inline:getSharedPreferences "aliases"
if aliases:getString("w", "") == "" then
aliases:edit():putString("w", "weather"):apply()
end
module:saveLazyLoad()
end