- Created BatchGenerateWeatherArtsJob to process eligible cities and generate weather art. - Introduced GenerateWeatherArtJob for generating weather art and image attachment. - Added AiService for obtaining prompts and generating images with OpenAI API. - Implemented WeatherService to fetch current weather data from the QWeather API. - Updated Gemfile with necessary gems (whenever, ruby-openai, httparty, down, aws-sdk-s3). This commit introduces a system to create and store weather art images for various cities based on current weather conditions, leveraging external APIs for data and image generation.
38 lines
985 B
Ruby
38 lines
985 B
Ruby
# app/services/weather_service.rb
|
|
class WeatherService
|
|
include HTTParty
|
|
base_uri Rails.application.credentials.qweather.uri
|
|
|
|
def initialize
|
|
@api_key = Rails.application.credentials.qweather.token
|
|
end
|
|
|
|
def get_weather(latitude, longitude)
|
|
Rails.logger.debug "Get Weather for #{latitude},#{longitude}"
|
|
response = self.class.get(
|
|
"/weather/now",
|
|
headers: {
|
|
"X-QW-Api-Key" => "#{@api_key}"
|
|
},
|
|
query: {
|
|
location: "#{longitude},#{latitude}"
|
|
})
|
|
|
|
return nil unless response.success?
|
|
|
|
data = response["now"]
|
|
{
|
|
temperature: data["temp"].to_f,
|
|
feeling_temp: data["feelsLike"].to_f,
|
|
humidity: data["humidity"].to_f,
|
|
wind_scale: "#{data['windScale']}级",
|
|
wind_speed: data["windSpeed"].to_f,
|
|
precipitation: data["precip"].to_f,
|
|
pressure: data["pressure"].to_f,
|
|
visibility: data["vis"].to_f,
|
|
cloud: data["cloud"].to_f,
|
|
description: data["text"]
|
|
}
|
|
end
|
|
end
|