Skip to content

Instantly share code, notes, and snippets.

@denpatin
Created January 5, 2016 15:07
Show Gist options
  • Save denpatin/ea45b0e5f784bed2370d to your computer and use it in GitHub Desktop.
Save denpatin/ea45b0e5f784bed2370d to your computer and use it in GitHub Desktop.
Getting current weather from Yandex.Pogoda into Terminal
#!/usr/bin/ruby
require 'net/http'
require 'uri'
require 'rexml/document'
require 'date'
# TODO: Make city selectable, not hardcoded
def city
26063 # St Petersburg
end
uri = URI.parse "http://export.yandex.ru/weather-ng/forecasts/#{city}.xml"
response = Net::HTTP.get_response(uri)
doc = REXML::Document.new(response.body)
city_name = doc.root.attributes['exactname']
time = doc.root.elements['fact/observation_time'].text
temperature = doc.root.elements['fact/temperature'].text
pogoda = doc.root.elements['fact/weather_type'].text
wind = doc.root.elements['fact/wind_speed'].text
# TODO: Add automatic l10n for various languages
part_of_day = {
morning: 'утром',
day: 'днём',
evening: 'вечером',
night: 'ночью'
}
puts "На сегодня погода в #{city_name}е:"
doc.root.elements.each('day') do |day|
day.elements.each_with_index('day_part') do |part, i|
print "#{part_of_day[part.attributes['type'].to_sym]}: "
print "#{part.elements['temperature-data/avg'].text}, "
print "#{part.elements['weather_type'].text}, "
puts "ветер #{part.elements['wind_speed'].text} м/с"
break if i == 3
end
# TODO: Give the forecast only for today
break
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment