public
Last active

Console weather forecast client, print weather for your city (from yandex pogoda)

  • Download Gist
yandex-weather.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#!/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 span.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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.