Created
March 6, 2019 06:16
-
-
Save fffonion/8b9a50e2c8f3d000659eb2be73311725 to your computer and use it in GitHub Desktop.
Hijack Phicomm K3 weather service to darksky weather API
This file contains hidden or 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
server { | |
listen 443 ssl; | |
listen [::]:443 ssl; | |
ssl_certificate certs/default.crt; | |
ssl_certificate_key certs/default.key; | |
server_name phiclouds.phicomm.com; | |
access_log /var/log/nginx/weather-access.log; | |
error_log /var/log/nginx/weather-error.log info; | |
location / { | |
content_by_lua_block { | |
local code_map = { | |
sunny = 0, | |
cloudy = 4, | |
["partly-cloudy-day"] = 5, | |
["partly-cloudy-night"] = 6, | |
rain = 13, | |
wind = 32 | |
} | |
local res = ngx.location.capture('/proxy') | |
local cjson = require("cjson") | |
local data = cjson.decode(res.body).hourly.data[1] | |
ret = { data = { | |
-- last_update = "2019-03-01T16:25:00+08:00", | |
now = { | |
code = tostring(code_map[data.icon]), | |
text = data.icon, | |
-- wind_scale = data.windSpeed, | |
temperature = math.floor((data.temperature - 32) * 5 / 9), | |
humidity = data.humidity * 100, | |
-- wind_direction = data.windBearing, | |
}, | |
-- name = "City", | |
} } | |
if not code_map[data.icon] then | |
ngx.log(ngx.ERR, data.icon, " not defined in code_map") | |
end | |
ngx.say(cjson.encode(ret)) | |
} | |
} | |
location = /proxy { | |
internal; | |
proxy_pass "https://api.darksky.net/forecast/API KEY/30.12345,120.123456"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment