Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # encoding: utf-8
- # https://www.meteoservice.ru/content/export.html
- require 'net/http'
- require 'uri'
- require 'rexml/document'
- require 'mechanize'
- #require_relative "../../lib/decoder.rb"
- CLOUDS = ['Ясно', 'Малооблачно', 'Облачно', 'Пасмурно']
- DAY_TIME = {
- '03' => 'Ночь',
- '09' => 'Утро',
- '15' => 'День',
- '21' => 'Вечер'
- }
- agent = Mechanize.new()
- town = 'Москва'
- page = agent.get("https://www.meteoservice.ru/content/export/gismeteo.html")
- list_tag = page.search("//select[starts-with(@id, 'city_list')]")
- # list_tag = page.search("#city_list")
- #.at("option:contains(town)").text
- town_tag = list_tag.search("option[text()=town]").to_a
- # town_tag = page.search("#city_list").at("option:contains('Москва')").parent.text
- # town_tag = list_tag.at('option:contains(town)')
- puts list_tag
- puts town_tag[0].class
- uri = URI.parse("https://www.meteoservice.ru/export/gismeteo/point/37.xml")
- response = Net::HTTP.get_response(uri)
- # puts response.body
- doc = REXML::Document.new(response.body)
- city_name = URI.unescape(doc.root.elements['REPORT/TOWN'].attributes['sname'])
- current_forecast = doc.root.elements['REPORT/TOWN'].elements.to_a
- # puts current_forecast
- min_t = []
- max_t = []
- min_w = []
- max_w = []
- clouds_index = []
- clouds = []
- time_ind = []
- time = []
- weekday = []
- (0..3).each do |i|
- min_t[i] = current_forecast[i].elements['TEMPERATURE'].attributes['min']
- max_t[i] = current_forecast[i].elements['TEMPERATURE'].attributes['max']
- min_w[i] = current_forecast[i].elements['WIND'].attributes['min']
- max_w[i] = current_forecast[i].elements['WIND'].attributes['max']
- clouds_index[i] = current_forecast[i].elements['PHENOMENA'].attributes['cloudiness'].to_i
- time_ind[i] = current_forecast[i].attributes['hour']
- weekday[i] = current_forecast[i].attributes['weekday']
- time[i] = DAY_TIME[time_ind[i]]
- clouds[i] = CLOUDS[clouds_index[i]]
- end
- puts city_name
- puts "\n\n"
- (0..3).each do |i|
- if weekday[i] == weekday[0]
- day_string = "Сегодня"
- else
- day_string = "Завтра"
- end
- puts "#{day_string} -- #{time[i]}\n"
- puts "Температура от #{min_t[i]} до #{max_t[i]}"
- puts "Ветер до #{max_w[i]} м/с"
- puts clouds[i]
- puts "\n\n"
- end
Advertisement
Add Comment
Please, Sign In to add comment