Last active
June 4, 2017 04:29
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local curl = require "cURL" | |
local lom = require "lxp.lom" | |
local GISMETEO_CODES = { | |
['7495'] = '27612_1'; | |
['7496'] = '27612_1'; | |
['7499'] = '27612_1'; | |
} | |
local gismeteo_base_url = "http://informer.gismeteo.ru/xml/" | |
local function gismeteo(code) | |
local url = gismeteo_base_url .. GISMETEO_CODES[code] .. '.xml' | |
local result, response = {} | |
local h = curl.easy{url = url} | |
h:setopt_writefunction(table.insert, result) | |
local ok, err = h:perform() | |
if ok then response = h:getinfo_response_code() end | |
h:close() | |
if not ok then return nil, err end | |
return response, table.concat(result) | |
end | |
local function decode_t(t) | |
local n = {} | |
if t.attr then | |
for _, name in ipairs(t.attr) do | |
n[name] = t.attr[name] | |
end | |
end | |
for i = 1, #t do | |
local e = t[i] | |
if type(e) == 'table' then | |
local value, name = decode_t(e) | |
local a = n[name] or {} | |
a[#a + 1] = value | |
n[name] = a | |
end | |
end | |
return n, t.tag | |
end | |
local function decode_xml(msg) | |
local ok, err = lom.parse(msg) | |
if not ok then return nil, err end | |
return (decode_t{ok}) | |
end | |
local code, data = gismeteo'7495' | |
if code == 200 then | |
local response = decode_xml(data) | |
local week_day = os.date('*t').wday | |
local day = {'Today', 'Tomorrow'} | |
local tod = {'night', "morning", "afternoon", "evening"} | |
local cloudiness = {'Clear', 'Partly cloudy', 'Cloudy', 'Mainly cloudy'}; | |
local precipitation = { | |
[4] = 'Rain'; | |
[5] = 'Rainfall'; | |
[6] = 'Snow'; | |
[7] = 'Snow'; | |
[8] = 'Thunderstorm'; | |
[9] = ''; | |
[10] = 'No precipitation'; | |
}; | |
local town = response.MMWEATHER[1].REPORT[1].TOWN[1] | |
local town_name = string.gsub(town.sname, '%%(%x%x)', function(hex) | |
return string.char(tonumber(hex, 16)) | |
end) | |
local forecasts = town.FORECAST | |
print(town_name) | |
for i, forcast in ipairs(forecasts) do | |
local phenomena = forcast.PHENOMENA[1] | |
print( | |
day[ tonumber(forcast.weekday) == week_day and 1 or 2 ], | |
tod[ tonumber(forcast.tod) + 1], | |
forcast.TEMPERATURE[1].min, forcast.TEMPERATURE[1].max, | |
cloudiness[ tonumber(phenomena.cloudiness) + 1], | |
precipitation[ tonumber(phenomena.precipitation)] | |
) | |
end | |
else | |
print('not found') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment