the weather

######## get ## the ## weather ## data ########

require ‘rubygems’
require ‘selenium-webdriver’
require ‘capybara’
require ‘capybara/poltergeist’

require_relative ‘fileOperationsHelperFunctions’
require_relative ‘speakOutLoudHelperFunction’

# sudo gem install selenium-webdriver

# Fehmarn DE0002845013
# München DE0006515
# Madrid ES0MA0079
# Barcelona ES0BC0020
# Malaga ES0MG0067
# Murcia ES0MU0031
# new york US0NY0993
# Berlin DE0001020

def getCityWeatherUpdate (city, *setDate)
@city
# start tracking time
start = Time.now

# select the firefox driver
# driver = Selenium::WebDriver.for :firefox
# select the phantomJS driver
# driver = Capybara::Session.new(:poltergeist)
driver = Capybara::Session.new(:selenium)
# go to the website with country code
# driver.get(“http://www.wetter.com/wetter_aktuell/rueckblick/?id=#{city}”)
driver.visit(“http://www.wetter.com/wetter_aktuell/rueckblick/?id=#{city}”)

# switch the last character of the current date with a 4
# dateStart= Time.now.strftime(“%d.%m.%Y”).gsub!( /.{1}$/, ‘4’ )
if setDate.nil?
# if there is no beginning date parameter set
# use one year ago from current date as start date
dateStart= Time.now.strftime(“%d.%m.%Y”).gsub!( /.{1}$/, ‘4’ )
end

if setDate
# set the date from parameter given by function call
dateStart = setDate
end

# find the beginning date entry field
# element = driver.find_element(:id, “archive_dateFrom”)

puts “i click on dateForm”
driver.find(:id, “archive_dateFrom”).click
driver.fill_in(‘archive_dateFrom’, with: “”)
driver.fill_in(‘archive_dateFrom’, with: “#{dateStart}”)

# driver.find(‘#archive_dateFrom’).native.submit
# driver.find(‘#archive_dateFrom’).native.send_keys “14.09.2014”
# session.find(‘.link-internal’, :text => ‘JA’).click
# element.click
# empty the beginning date entry field
# element.clear

# fill in the the beginning date entry field
# element.send_keys dateStart
puts dateStart

# element.native.send_keys “14.09.2014”
# submit the beginning date entry field
# element.submit
# remove all these characters from the html source document into pageSourceCleaning
# puts driver.body

# pageSourceCleaning = driver.page_source.partition(“temperatureLineDiagram”)
pageSourceCleaning = driver.body.partition(“temperatureLineDiagram”)
pageSourceCleaning = pageSourceCleaning[2]
pageSourceCleaning = pageSourceCleaning.partition(“wtbox clearfix wtbox-tb”)
pageSourceCleaning = pageSourceCleaning[0]
pageSourceCleaning = pageSourceCleaning.gsub(/d{13}/ ,”empty”)
pageSourceCleaning = pageSourceCleaning.gsub(“(“,””)
pageSourceCleaning = pageSourceCleaning.gsub(“{“,””)
pageSourceCleaning = pageSourceCleaning.gsub(“}”,””)
pageSourceCleaning = pageSourceCleaning.gsub(“[“,””)
pageSourceCleaning = pageSourceCleaning.gsub(“]”,””)
pageSourceCleaning = pageSourceCleaning.gsub(“”,””)
pageSourceCleaning = pageSourceCleaning.gsub(‘<div class=’,””)
pageSourceCleaning = pageSourceCleaning.gsub(‘)’,””)
pageSourceCleaning = pageSourceCleaning.gsub(‘”‘,””)
pageSourceCleaning = pageSourceCleaning.gsub(‘;’,””)
pageSourceCleaning = pageSourceCleaning.gsub(‘°C’,””)
pageSourceCleaning = pageSourceCleaning.gsub(‘date:’,””)
# display the cleaned html source which is now a CSV file on terminal
puts pageSourceCleaning

# associate city handle with city name
if city == “ES0MG0067” then cityNameToWrite = “malaga”
elsif city == “ES0BC0020” then cityNameToWrite = “Barcelona”
elsif city == “DE0002845013” then cityNameToWrite = “Fehmarn”
elsif city == “US0NY0993” then cityNameToWrite = “new york”
elsif city == “ES0MA0079” then cityNameToWrite = “madrid”
elsif city == “DE0001020” then cityNameToWrite = “berlin”
end

# write know cities into a file
if cityNameToWrite
# write data into text file
File.open(“./seedFiles/#{cityNameToWrite}.txt”, ‘w’) {|f| f.write(pageSourceCleaning) }
# shut down the browser
# driver.quit
puts “wrote file #{cityNameToWrite}”
speakOutLoud(“wrote file #{cityNameToWrite}”)
end

# write unknown cities into a file
if cityNameToWrite.nil?
# write data into text file
File.open(“./seedFiles/#{city}.txt”, ‘w’) {|f| f.write(pageSourceCleaning) }
# shut down the browser
# driver.quit
puts “wrote file #{city}”
speakOutLoud(“wrote file #{city}”)
end

###################
finish = Time.now
diff = finish – start
puts “timer: #{diff}”

# headless
# timer: 6.465766
# selenium Firefox
# timer: 12.445004

end

getCityWeatherUpdate(“DE0002845013”, “14.09.2014”)

————-analyze-and-speak——————————————-
require ‘csv’
require ‘.//helperFunctions/fileOperationsHelperFunctions’
require ‘.//helperFunctions/speakOutLoudHelperFunction’

my_project_folder = File.dirname(__FILE__)

def how_was_the_weather (city_name, city_data, max_median, speaker_voice, reading_speed)
@city_name = “Berlin”
@city_data = “”
@max_median = 20
@speaker_voice = “Serena”
@reading_seed
above_max_median = 0
below_max_median = 0

# lets get the cvs data from a file
# for spain the files are momentarily from 29.6.2014 – 29.6.2015
CSV.foreach(city_data) do |row|

if ( row[0] =~ /max: ‘/)
# puts row

max = row[0].gsub( /.{1}$/, ” )

max.slice!(0, 19)

if (max.to_i > max_median)

above_max_median += 1

end

if (max.to_i < max_median)

below_max_median += 1

end

end

end
all_days = above_max_median + below_max_median

# the speaking voice decides what language to use
if speaker_voice == “Serena”

puts “how many times was the temperature above #{max_median} degrees in #{city_name}?”
speak_out_loud(“how many times was the temperature above #{max_median} degrees in #{city_name}?”,reading_speed, speaker_voice)

puts “#{above_max_median} times out of #{all_days}”
speak_out_loud(“#{above_max_median} times out of #{all_days}”,reading_speed , speaker_voice)

percent_value = above_max_median.to_f / all_days.to_f * 100
puts “Thats #{percent_value.to_i} perocent”
speak_out_loud(“Thats #{percent_value.to_i} percent”,reading_speed , speaker_voice)

elsif speaker_voice == “Daniel”

puts “how many times was the temperature above #{max_median} degrees in #{city_name}?”
speak_out_loud(“how many times was the temperature above #{max_median} degrees in #{city_name}?”,180 , speaker_voice)

puts “#{above_max_median} times out of #{all_days}”
speak_out_loud(“#{above_max_median} times out of #{all_days}”,reading_speed , speaker_voice)

percent_value = above_max_median.to_f / all_days.to_f * 100
puts “Thats #{percent_value.to_i} perocent”
speak_out_loud(“Thats #{percent_value.to_i} percent”,reading_speed , speaker_voice)

elsif speaker_voice == “Anna”

puts “Wie oft war die Temperatur in #{city_name} über #{max_median} Grad?”
speak_out_loud(“Wie oft war die Temperatur in #{city_name} über #{max_median} Grad?”,180 , speaker_voice)

puts “#{above_max_median} aus #{all_days} Tagen”
speak_out_loud(“#{above_max_median} aus #{all_days} Tagen”,reading_speed , speaker_voice)
percent_value = above_max_median.to_f / all_days.to_f * 100
puts “Das macht #{percent_value.to_i} prozent”
speak_out_loud(“Das macht #{percent_value.to_i} prozent”,reading_speed , speaker_voice)

elsif speaker_voice == “Yannick”

puts “Wie oft war die Temperatur in #{city_name} über #{max_median} Grad?”
speak_out_loud(“Wie oft war die Temperatur in #{city_name} über #{max_median} Grad?”,180 , speaker_voice)
puts “#{above_max_median} aus #{all_days} Tagen”
speak_out_loud(“#{above_max_median} aus #{all_days} Tagen”,reading_speed , speaker_voice)
percent_value = above_max_median.to_f / all_days.to_f * 100
puts “Das macht #{percent_value.to_i} prozent”
speak_out_loud(“Das macht #{percent_value.to_i} prozent”,reading_speed , speaker_voice)
end
end

max_median_temperature = 19
speaker_voice = “Anna”
reading_speed = “180”
# 120 is slow
# 180 is normal

# German = Anna, Yannick,
# English = Serena, Daniel

# how_was_the_weather( “madrid”, “madridwetter20152.txt”, max_median_temperature, speaker_voice)
# how_was_the_weather(“Femarn”, “fehmarnwetter2015.txt”, max_median_temperature, speaker_voice)
# how_was_the_weather(“Berlin”, “berlinwetter2015.txt”, max_median_temperature, speaker_voice)
# how_was_the_weather(“Hannover”, “hannoverwetter2015.txt”, max_median_temperature, speaker_voice)
# how_was_the_weather( “madrid”, “madridwetter2015.txt”, max_median_temperature, speaker_voice)
# how_was_the_weather( “Barcelona”, “barcelonawetter2015.txt”, max_median_temperature, speaker_voice)
# how_was_the_weather( “Malaga”, “malagawetter2015.txt”, max_median_temperature, speaker_voice)
# how_was_the_weather( “Murcia”, “malagawetter2015.txt”, max_median_temperature, speaker_voice)
# how_was_the_weather( “New York”, “newyorkwetter2015.txt”, max_median_temperature, speaker_voice)

# city(“DE0002845013”, “22.02.2015”)

how_was_the_weather( “Fehmarn”, “.//seedFiles/weather/Fehmarn.txt”, max_median_temperature, speaker_voice, reading_speed)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s