Change weather forecast based on user's zipcode (yahoo weather module)

ejh3 - July 31, 2009 - 14:05

Hello everyone,

I'm basically trying to do what just what the subject says.

The yahoo weather module description says that it's a base for "easy adding functionality like work[ing] together with localisation modules to automaticaly display the weather for user location."

Has anyone ever done this or know of any localization modules that may be able to work together with this weather module to accomplish what I need?

Thank you,

Joe

Why Yahoo Weather?

Cybergarou - July 31, 2009 - 14:42

Is there a particular reason why you want to use the yahoo weather module? The Weather module has been developed and I'm sure it's much closer to having the capability that you are asking for, if it doesn't have it already.

Either way you'll need the Location module.

Based off zip code...

ejh3 - August 13, 2009 - 12:42

I began using the yahoo weather module because it pulls forecasts based off zip codes. The other weather module pulls information from airport codes only, I think.

I need to be able to pull forecasts from zip codes and also be able to change the forecast if a user logged in has a different zip code than the site wide default zip code.

hi2u

I think I got something to

ejh3 - August 13, 2009 - 15:07

I think I got something to work...

In the .module file for the yahoo weather module, I added the a yahoo_weather_forecast_update_weather_info() function call to the yahoo_weather_forecast_block() function in the if($op == 'view') statement.

Before doing that though, I made a custom field in the registration process that asks for a zip code from a user. Then in the .module file, within the _yahoo_weather_forecast_process_xml() function, I added the following above where the $units variable is set:

<?php
global $user;                          
$profile = profile_load_profile($user);
$zipcode = $user->profile_zipcode;

if(
user_is_logged_in()) {             
 
$code = $zipcode;
} else {                               
 
$code = variable_get('yahoo_weather_forecast_city_code', 'YOUR DEFAULT WEATHER CODE HERE');
}
?>

I also changed the default zip code in the .module file set in the yahoo_weather_forecast_settings_form() function in the $form['yahoo_weather_forecast_city_code'] array. This probably didn't need to be done here as you can set it in the site configuration form through the back end GUI, but I did it anyway.

Anyway, now if I'm logged in with a user that has a different zip code other than the default one, the weather module pulls that information instead and refreshes it accordingly. Bad thing is I think it pulls new information each page request/refresh.

There's probably another way of accomplishing this, but right now doing it this way seems to be working for me. If anyone knows of something better, share it here.

hi2u

I can certainly understand

Cybergarou - August 13, 2009 - 20:25

I can certainly understand working with the module that works in the data format you want. Generally I wouldn't modify a module to get it working the way you want it to, but in this case I think you're safe. Development on the yahoo weather module seems to be very dead.

What you have come up with here is good. Still it would be a good idea to address the issue of pulling new information with each page request. Many weather feeds will eventually block an address that makes the same request too many times. (They see it as a DOS attack.) You can also speed up the rendering of your pages if you aren't waiting for Yahoo to respond each time.

In your case I would make a table that stores the zipcode as the index and holds the time that zipcode was last accessed plus the update interval. (now()+3600 is a good value to store for weather feeds, unless it updates more frequently) Then you have to store a copy of the xml file Yahoo sends you locally. When a request for a zipcode is made you would check to see if the local data is still good and divert the module to the local copy if it is.

Of course it would be even better if you stored the end result for each zipcode instead of an intermediate step, but that's a more involved modification.

I would have to look at the module code to make any specific suggestions. Let me know if you want any help.

 
 

Drupal is a registered trademark of Dries Buytaert.