Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Console weather forecast client, print weather for your city (from yandex pogoda)
#!/usr/bin/env coffee
### -----------------------------------
Console weather client
https://gist.github.com/msoap/2332117
usage:
yandex-weather.coffee
yandex-weather.coffee london
----------------------------------- ###
Jsdom = require 'jsdom'
Table = require 'cli-table'
city = process.argv[2]
address = 'http://pogoda.yandex.ru/' + if city then "#{city}/" else ''
selectors =
city: 'div.b-navigation-city h1.b-navigation-city__city'
term_now: 'div.b-widget-current-weather div.b-thermometer div.b-thermometer__now'
term_night: 'div.b-widget-current-weather div.b-thermometer div.b-thermometer__small-temp'
desc_now: 'div.b-widget-current-weather div.b-thermometer div.b-info-item'
pressure: 'div.b-thermometer-info__line:nth-child(1)'
wind: 'div.b-thermometer-info__line:nth-child(2)'
humidity: 'div.b-thermometer-info__line:nth-child(3)'
selectors_next_days =
date: 'div.b-forecast div.b-forecast__day'
desc: 'div.b-forecast div.b-forecast__desc'
term: 'div.b-forecast div.b-forecast__tday'
term_night: 'div.b-forecast div.b-forecast__tnight'
Jsdom.env
url: address
scripts: ['http://code.jquery.com/jquery-2.1.0.min.js']
done: (errors, window) ->
$ = window.$
result = {}
for item_key of selectors
result[item_key] = $(selectors[item_key]).text().replace(/\s+/g, " ")
result.forecast = []
for item_key of selectors_next_days
$(selectors_next_days[item_key]).each (i, el) ->
result.forecast[i] = {} unless result.forecast[i]?
result.forecast[i][item_key] = el.textContent
render result
# -------------------------------------------------------------------
render = (weather) ->
console.log "#{weather.city.yellow} (#{address})"
term_night = if weather.term_night then ", ночью: #{weather.term_night.green}" else ""
console.log "Сейчас: #{weather.term_now.green}, #{weather.desc_now.green}#{term_night}"
console.log "#{weather.pressure}"
console.log "#{weather.humidity}"
console.log "#{weather.wind}"
table = new Table
chars:
'mid': ''
'left-mid': ''
'mid-mid': ''
'right-mid': ''
head: ['дата', '°C', 'погода', '°C ночью']
colWidths: [12, 5, 45, 10]
weather.forecast.map (item) ->
table.push [item.date, item.term, item.desc, item.term_night]
table_string = table.toString()
console.log table_string
@msoap
Owner

Install

# install node.js
brew install node

# install node libs
npm -g install coffee-script jsdom cli-table colors

# install script
git clone https://gist.github.com/2332117.git yandex-weather
chmod a+x yandex-weather/yandex-weather.coffee

Usage

yandex-weather.coffee
yandex-weather.coffee moscow
yandex-weather.coffee kiev

Preview

yandex-weather.coffee.screenshot.png

@msoap
Owner

After change pogoda.yandex.ru html, script dont work. Use Go program instead - https://github.com/msoap/yandex-weather-cli.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.