Skip to content

Instantly share code, notes, and snippets.

@milisarge
Last active August 24, 2017 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save milisarge/cc60d3cab7c70763f0d5b6780e9e677e to your computer and use it in GitHub Desktop.
Save milisarge/cc60d3cab7c70763f0d5b6780e9e677e to your computer and use it in GitHub Desktop.
import httpclient, asyncdispatch, strutils, tables, unicode, json, math, times
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
var key = "78b50ffaf45be011ccc5fccca4d836d8"
proc getData() {.async.} =
let client = newAsyncHttpClient()
var city = "Istanbul"
let resp = await client.get(ForecastUrlFormat % [key, city, $(1)])
let
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