Guest User

Untitled

a guest
Feb 4th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. # encoding: utf-8
  2. # https://www.meteoservice.ru/content/export.html
  3. require 'net/http'
  4. require 'uri'
  5. require 'rexml/document'
  6. require 'mechanize'
  7.  
  8.  
  9. #require_relative "../../lib/decoder.rb"
  10.  
  11. CLOUDS = ['Ясно', 'Малооблачно', 'Облачно', 'Пасмурно']
  12.  
  13. DAY_TIME = {
  14. '03' => 'Ночь',
  15. '09' => 'Утро',
  16. '15' => 'День',
  17. '21' => 'Вечер'
  18. }
  19.  
  20. agent = Mechanize.new()
  21. town = 'Москва'
  22. page = agent.get("https://www.meteoservice.ru/content/export/gismeteo.html")
  23. list_tag = page.search("//select[starts-with(@id, 'city_list')]")
  24. # list_tag = page.search("#city_list")
  25. #.at("option:contains(town)").text
  26. town_tag = list_tag.search("option[text()=town]").to_a
  27. # town_tag = page.search("#city_list").at("option:contains('Москва')").parent.text
  28. # town_tag = list_tag.at('option:contains(town)')
  29. puts list_tag
  30.  
  31. puts town_tag[0].class
  32.  
  33. uri = URI.parse("https://www.meteoservice.ru/export/gismeteo/point/37.xml")
  34. response = Net::HTTP.get_response(uri)
  35.  
  36.  
  37.  
  38. # puts response.body
  39.  
  40. doc = REXML::Document.new(response.body)
  41. city_name = URI.unescape(doc.root.elements['REPORT/TOWN'].attributes['sname'])
  42. current_forecast = doc.root.elements['REPORT/TOWN'].elements.to_a
  43.  
  44. # puts current_forecast
  45.  
  46. min_t = []
  47. max_t = []
  48. min_w = []
  49. max_w = []
  50. clouds_index = []
  51. clouds = []
  52. time_ind = []
  53. time = []
  54. weekday = []
  55.  
  56. (0..3).each do |i|
  57.  
  58. min_t[i] = current_forecast[i].elements['TEMPERATURE'].attributes['min']
  59. max_t[i] = current_forecast[i].elements['TEMPERATURE'].attributes['max']
  60.  
  61. min_w[i] = current_forecast[i].elements['WIND'].attributes['min']
  62. max_w[i] = current_forecast[i].elements['WIND'].attributes['max']
  63.  
  64. clouds_index[i] = current_forecast[i].elements['PHENOMENA'].attributes['cloudiness'].to_i
  65.  
  66. time_ind[i] = current_forecast[i].attributes['hour']
  67. weekday[i] = current_forecast[i].attributes['weekday']
  68. time[i] = DAY_TIME[time_ind[i]]
  69.  
  70.  
  71.  
  72.  
  73. clouds[i] = CLOUDS[clouds_index[i]]
  74.  
  75. end
  76.  
  77. puts city_name
  78.  
  79. puts "\n\n"
  80. (0..3).each do |i|
  81.  
  82. if weekday[i] == weekday[0]
  83. day_string = "Сегодня"
  84. else
  85. day_string = "Завтра"
  86. end
  87.  
  88. puts "#{day_string} -- #{time[i]}\n"
  89.  
  90. puts "Температура от #{min_t[i]} до #{max_t[i]}"
  91. puts "Ветер до #{max_w[i]} м/с"
  92. puts clouds[i]
  93. puts "\n\n"
  94. end
Advertisement
Add Comment
Please, Sign In to add comment