Skip to content

Instantly share code, notes, and snippets.

@njh
Created January 18, 2024 09:37
Show Gist options
  • Save njh/9a8020caab97b000f8401c5e194c1b56 to your computer and use it in GitHub Desktop.
Save njh/9a8020caab97b000f8401c5e194c1b56 to your computer and use it in GitHub Desktop.
Get one year of historical daily temperatures for a single location from Meteosource
#!/usr/bin/env ruby
require 'date'
require 'json'
today = Date.today
start = today - 365
(start...today).each do |date|
json = File.read("weather/#{date}.json")
data = JSON.parse(json, :symbolize_names => true)
puts [date, data[:daily][:temperature], data[:daily][:temperature_min], data[:daily][:temperature_max]].join("\t")
end
#!/usr/bin/env ruby
require 'date'
require 'httparty'
API_KEY = 'xxx'
PLACE_ID = 'london'
def fetch_date(place, date)
filename = "weather/#{date}.json"
return if File.exist?(filename)
puts "Fetching: #{date}"
url = "https://www.meteosource.com/api/v1/standard/time_machine?place_id=#{place}&date=#{date}&timezone=Europe%2FLondon&units=metric&key=#{API_KEY}"
res = HTTParty.get(url, :headers => {'Accept' => 'application/json'})
raise "Failed to fetch data: #{res.body}" if res.code != 200
File.open(filename, 'w') do |file|
file.write(res.body)
end
end
today = Date.today
start = today - 365
(start...today).each do |date|
fetch_date(PLACE_ID, date)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment