Created
August 23, 2017 13:26
-
-
Save RokkuCode/a0ef9882e34669cb2b6f32836285ad50 to your computer and use it in GitHub Desktop.
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
const | |
ForecastUrlFormat = "http://api.openweathermap.org/data/2.5/forecast/daily?APPID=$1&lang=en&q=$2&cnt=$3" | |
ResultFormat = """$1: | |
$2 | |
Temperature: $3 °C | |
Humidity: $4% | |
Clouds: $5% | |
Wind speed: $6 m/s""".unindent | |
Key = "78b50ffaf45be011ccc5fccca4d836d8" | |
proc getData() {.async.} = | |
let client = newAsyncHttpClient() | |
let city = "Moscow" | |
let resp = await client.get(ForecastUrlFormat % [Key, city, $(1)]) | |
# First "await" of resp.body. | |
# If you comment this - gc.nim disappears from stacktrace | |
echo await resp.body | |
let | |
# Second "await" of resp.body | |
bd = await resp.body | |
day = parseJson(bd)["list"].getElems()[^1] | |
temp = int round day["temp"]["day"].getFNum() - 273 | |
humidity = int round day["humidity"].getFNum() | |
desc = unicode.capitalize day["weather"].getElems()[0]["description"].getStr() | |
wind = int round day["speed"].getFNum() | |
cloud = int(round(day["clouds"].getFNum())) | |
date = int64 day["dt"].getNum() | |
time = fromSeconds(date).getLocalTime().format("d'.'MM'.'yyyy") | |
echo ResultFormat % [time, desc, $temp, $humidity, $cloud, $wind] | |
waitFor getData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment