Drupal 7 Module

Open Weather is a module I created to parse out the weather response from openweathermap.org. The main reason I created this was because Google had killed it's weather API, thus rendering the Google Weather module useless. This module is a nice way for users to get weather in their site now.

This module has a few nice features. First, it can handle unlimited cities for weather output. Each city will have it's own block which can be customized to show whatever weather details you see fit. You can even theme this yourself by overriding the theme that comes with the module. Second, all data is incorporated into views. This gives the developer or user the ability to show forecast data in their site, or simply display the data anyway they want. Third (currently in progress), is the ability for users of a website to have their own weather in their profile.

git clone http://git.drupal.org/sandbox/kherda/1777890.git open_weather

Comments

jvdurme’s picture

Hi Kherda,

the demo looks very slick. Nice!
However, your git command you posted is wrong. It asks for a password.
Please write the correct command:

git clone http://git.drupal.org/sandbox/kherda/1777890.git open_weather

I ran CodeSniffer on your files and there are still some coding issues. They are below.
Please find the report as attachment in my next comment below. I couldn't attach something here while re-editing.
You can check with CodeSniffer yourself: http://drupal.org/project/drupalcs

It is better if these code styling issues are fixed first.
The guidelines for code writing can be found here: http://drupal.org/coding-standards

cheers,

Joost

ankitchauhan’s picture

@jvdurme

Please don't paste errors as comment. Attach them in a document if they are more than 20.

thanks

jvdurme’s picture

StatusFileSize
new38.49 KB

Ah, ok. Sorry about that.

Kherda, find the CodeSniffer report attached to this comment.

kherda’s picture

Thank you for the heads up on the Code Sniffer! I had never used that before... Everything should be up to Drupal Standards now (latest code committed).

Kevin

dj1999’s picture

StatusFileSize
new6.91 KB

Hi,

this is a very nice module, thanks!

Some challange was to start poperly working.
I do not know that just for me or not, but can't generate weather data on times what in admin form have:
2 am 5 am 8 am 11 am 2 pm 5 pm 8 pm 11 pm

I get these times :
3 am 6 am 9 am 12 pm 3 pm 6 pm 9 pm 12 am

After modify times with timezone differeces works fine.

Here a patch, maybe help somebody too.

Regards,
Joe

kherda’s picture

Thanks for the patch! I had implemented it locally and tested... everything worked nicely, however I figure that since all of the data is already stored in a table using views to extract we really didn't need to confusion of an interval selection, so I removed it. This will be easier for the site admin implementing in the future. This way no one will have any timezone issues.

weebpal’s picture

StatusFileSize
new15.96 KB

Hi Kevin,

Thanks for your really interesting project, I hope that will be approve early, so please update your project follow Drupal Coding Standard. And I also please to give some small note after take a look your code:

  $output->list[0]->id;

- should we add a litle validate here to skip some special cases

  $db = db_query("SELECT timestamp FROM {open_weather} LIMIT 1")->fetchObject();

- Could we we use fetchField here

function open_weather_cron() {

  $db = db_query("SELECT timestamp FROM {open_weather} LIMIT 1")->fetchObject();
  if (is_object($db)) {
    $expiretime = $db->timestamp + (variable_get('open_weather_refresh') * 60);
    if (time() > $expiretime) {
      open_weather_fill_weather();
    }
  }

  return;
}

/**
 * Clears old weather, stores new, logs it to watchdog.
 */
function open_weather_flush_caches() {

  open_weather_fill_weather();
  watchdog('open_weather', 'Cleared all saved weather and reloaded new weather', $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL);

  return;
}

- Maybe "return" is useless in these situations

Hope that your project will be approved early.
Regards,
WeebPal

DmitriyMakeev’s picture

Зlease specify the requirements for the module like CURL library for PHP. I had to install it to see the module ;)

And some bugs:
1. You need to remove a master, and set the correct default branch: Setting a default branch, Moving from a master to a major version branch.

2. Please fix coding style issues detected by automated tools:
http://ventral.org/pareview/httpgitdrupalorgsandboxkherda1777890git

3. open_weather.module
Line 89: use t() for the watchdog message.
open_weather.admin.inc
Line 23: use t() for "City", "Coords"...
Line 35: use t() for 'Fahrenheit'

4. Try to not use queries like db_query("DELETE FROM {variable} WHERE name LIKE 'open_weather_%'");. Use

db_delete('variable')
  ->condition('name', db_like('open_weather_') . '%', 'LIKE')
  ->execute();

5. In open_weather_uninstall() function use variable_del() for each variable in case if someone accidentally call its variable starting with 'open_weather_'.

DmitriyMakeev’s picture

Status: Needs review » Needs work
kherda’s picture

DmitriyMakeev (and others),

Thanks for the feedback. Aside from updating the code, I also implemented some additional logic to handle unlimited cities (each with their own settings). The weather for each city can be extracted using views (custom filter operator helps you select the city you entered), or you can implement the block created for each city, which will show the current weather (which can be altered in the theme layer).

I created a few custom handlers in views, one in particular will display the correct weather image (now included with the module).

I am using one main variable now for ease of use (JSON object as the contents).

Everything is a lot cleaner and more user friendly.

I will be sure to note the need for cURL as it is required for the API.

Kevin

klausi’s picture

Set this to "needs review" once you are ready.

We are currently quite busy with all the project applications and I can only review projects with a review bonus. Please help me reviewing and I'll take a look at your project right away :-)

mariusm’s picture

Hello,

I receive this errors

Notice: Undefined variable: blocks in open_weather_block_info() (line 536 of D:\wamp\www\testdr\sites\all\modules\openweather\open_weather.module).
Warning: Invalid argument supplied for foreach() in _block_rehash() (line 389 of D:\wamp\www\testdr\modules\block\block.module).

Thanks,
Marius

kherda’s picture

Thanks Marius,

I am working on that. After the initial implementation of the module it is seeking weather that has not been returned yet. It's on my radar :) I am looking to make an update this afternoon, when I do I will post again with the updates.

Kevin

kherda’s picture

Status: Needs work » Needs review

Hey Everyone,

I have made some major updates to this module. The only dependency now is on the views module and out of the box each cities weather will come with it's own customizable block. To take this further, you can theme out this block if you need by copying the file in the theme dir to your themes dir. Everything is working nicely... if there any any issues, please let me know.

Kevin

Update. I just added in the ability for users to view their own weather (weather data stored in the user data object). Need to refresh this based on interval on user profile page load, but it's indeed working.

dj1999’s picture

StatusFileSize
new3.22 KB

Here is a patch for localization.

kherda’s picture

Patch added, thanks for this!

I will be adding in metric vs imperial options sometime this week for other weather conversions (such as wind speed).

dj1999’s picture

Yes, it missed for me too. Wind data are international knot and I need them meters per second. If could set it in admin form is better than modify open-weather-block.tpl.php.

kherda’s picture

I agree. I currently have the weather format selection as an option in the admin section. I will change that to metric or imperial and the rest of the formatting will inherit the change. I will try to push this update out today. Be on the lookout.

Kevin

dj1999’s picture

Some secure and more localization.
Because t() say:
"You should never use t() to translate variables, such as calling
t($text);
, unless the text that the variable holds has been passed through t() elsewhere (e.g., $text is one of several translated literal strings in an array)."

I do not know this is the best way, but so could translate weather text and description. If it's other way I hope drupal experts will tell us.

Sorry that code and not patch but I hacked much more the module :)

  $city_data = array(
    "city id" => check_plain(@$output->list[0]->id),
    "coordinates" => array(
      "lat" => check_plain(@$output->list[0]->coord->lat),
      "long" => check_plain(@$output->list[0]->coord->lon),
    ),
    "station" => check_plain(@$output->list[0]->name),
    "current weather" => array(
      "temp" => check_plain(@$output->list[0]->main->temp),
      "pressure" => check_plain(@$output->list[0]->main->pressure),
      "humidity" => check_plain(@$output->list[0]->main->humidity),
      "temp_min" => check_plain(@$output->list[0]->main->temp_min),
      "temp_max" => check_plain(@$output->list[0]->main->temp_max),
      "wind" => check_plain(@$output->list[0]->wind->speed),
      "clouds" => check_plain(@$output->list[0]->clouds->all),
      "weather id" => check_plain(@$output->list[0]->weather[0]->id),
      "weather text" => t(@$output->list[0]->weather[0]->main, array(), array('context' => 'open_weather')),
      "weather desc" => t(@$output->list[0]->weather[0]->description, array(), array('context' => 'open_weather')),
      "weather icon" => check_plain(@$output->list[0]->weather[0]->icon),
    ),
    "block" => array(
      'weather icon' => 1,
      'current temp' => 1,
      'city name' => 1,
      'weather conditions' => 1,
      'weather description' => 1,
      'wind' => 1,
      'clouds' => 1,
    ),
  );
kherda’s picture

Thanks for your notes. I put them in place. I also updated the format to render as imperial vs metric, which will not only alter the temperature, but also the wind speed. I made some minor adjustments to the user weather option as well.

r2integrated’s picture

Status: Needs review » Needs work

Wow, what a great module! Views integration is hugely useful. Overall, I think this is quite worthy to be accepted into the community. There are a few issues I've noted below:

Automated Review:
There is still a master branch, make sure to set the correct default branch: http://drupal.org/node/1659588 . Then remove the master branch, see also step 6 and 7 in http://drupal.org/node/1127732
There is a git tag that has the same name as the branch 7.x-1.x. Make sure to remove this tag to avoid confusion.

Source: http://ventral.org/pareview - PAReview.sh online service

Manual Review:

  1. Consider having impertial/metric option on individual block instances AND in the field options in views.
  2. README.txt could use some formatting love
  3. Can you explain the necessity of hook_init() and the EXCLUDED_PAGES define?
  4. open_weather.module line 8 - SECUREPAGES_PAGES?
  5. open_weather.module lines 46 and 55 - both menu items point to the same form, maybe there should be only one?
  6. open_weather.module line 71, 97 - 'ow' should probably not be abbreviated - "open_weather" is more consistent
  7. open_weather.module line 90 - unsure of function naming
  8. Cloud/sun imagery - what is the origin? Did you design these or are they available somewhere and can we see the license?
  9. open_weather.module lines 606-641 and 787-822 - there has to be a less verbose way of doing this. Maybe a regex?
  10. open_weather.views_default.inc has an empty hook_views_default_views implementation
  11. open_weather.views_default.inc lines 20-55 - verbose, see above.
  12. open_weather.css has some empty selectors

Bottom Line
Aside from a few issues, this is a very well-done module and should make it to full project status.

klausi’s picture

Status: Needs work » Closed (won't fix)

Closing due to lack of activity. Feel free to reopen if you are still working on this application.

If you reopen this please keep in mind that we are currently quite busy with all the project applications and I can only review projects with a review bonus. Please help me reviewing and I'll take a look at your project right away :-)

pol’s picture

Sorry to dig up old thread, but here is a working module for OpenLayers and OpenWeatherMap: http://drupal.org/project/olowm

pol’s picture

Issue summary: View changes

Updated git url and specific module information that has changed or been updated as I have been developing.