Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-08-27 08:19:43

ToK
Plugin Author
Registered: 2014-02-18
Posts: 23

tok_owm_current – new weather plugin

As I did not find any active weather plugin for textpattern and stumbled over OpenWeatherMap by chance, I wrote a little plugin to include some current weather data in a txp page or form.

The plugins output is widely configurable; a help page including some examples is included. Please note, that you need to get an OpenweatherMap API Key in addition.

Please take a look at my github repository.

Minor improvements are already planned, but maybe there are some more suggestions outside …

Offline

#2 2014-08-27 11:14:39

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: tok_owm_current – new weather plugin

Very nice, thank you for writing this. I managed to get weather data for a few places with minimum effort, which is great.

One thing they mention on their site is “Do not send requests more then 1 time per 10 minutes from one device. Normally the weather is not changing so frequently.” It seems that if I do refresh the page more frequently than that, I still get the information, but it might be kinder to their server if you could cache the results somehow (perhaps in the txp_prefs table?)

Although they specify ‘per device’, things might be simpler just to cache the tag’s output. I’d normally do this by taking an md5 of the tag’s serialized $atts array and using that as the key, e.g.:

set_pref('tok_owm_data_<md5string>', 'timestamp|<result_of_call>', 'tok_owm', PREF_HIDDEN);

You could then maybe introduce a cache attribute which defaults to 10, use get_pref() to fetch the cached JSON results for the tag, check if the timestamp you stored has exceeded the cache timeout value and only fetch+cache fresh data from the server if it has. It’s a shame that Textpattern’s prefs entries aren’t timestamped internally because that would save you a bit of hassle (I might consider that in a future version of Txp because it’s handy for a lot of applications). Alternatively you could store a second pref value which held timestamps if you preferred, which is perhaps a bit cleaner at the expense of an extre DB entry.

People who have elected to have a paid owm account can change the cache attribute value to request more frequent updates, or anybody could set cache="0" to turn off the cache feature if they preferred, on a tag-by-tag basis.

It does sound like a lot of work, so maybe it’s not worth it! But it would be more friendly to their service and, potentially, faster for users too. Just a thought.

All in all, a neat little plugin, thanks again.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2014-08-27 11:41:55

ToK
Plugin Author
Registered: 2014-02-18
Posts: 23

Re: tok_owm_current – new weather plugin

Bloke wrote #283263:

Very nice, thank you for writing this. I managed to get weather data for a few places with minimum effort, which is great.

Fine.

it might be kinder to their server if you could cache the results somehow (perhaps in the txp_prefs table?)

You’ re right. I realized that problem also before, but did not have an idea how to deal with it. I’ll try to implement your suggestions. Thanks a lot.

Offline

#4 2014-08-27 13:46:22

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: tok_owm_current – new weather plugin

ToK wrote #283258:

As I did not find any active weather plugin for textpattern …

I did :)

<txp:etc_query url="http://api.openweathermap.org/data/2.5/forecast/weather?id=524901&APPID=1111111111&units=metric&lang=ru"
	markup="json" query="list/*" wraptag="p" break="br"
>
	<img src="http://openweathermap.org/img/w/{weather/0/icon?}.png" alt="{weather/0/description?}" />
	{dt_txt?} : {main/temp?}°C
</txp:etc_query>

But your plugin is more convenient, of course. As cache, I’d rather suggest the general-purpose aks_cache.

Offline

#5 2014-08-27 14:03:46

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,134
GitHub

Re: tok_owm_current – new weather plugin

etc wrote #283267:

I did :)

Offline

#6 2014-08-27 14:09:46

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: tok_owm_current – new weather plugin

Not that anyone needed to be reminded, but this is why proper caching of weather apps can be important. :)

Android App Takes Down Heart of National Weather Service Website

Offline

#7 2014-08-28 11:27:06

ToK
Plugin Author
Registered: 2014-02-18
Posts: 23

Re: tok_owm_current – new weather plugin

Caching is now implemented. But as I did not want to mess up the txp prefs table, the cached data is saved in an own one — which seemed much cleaner to me. As Bloke suggested, the timeout for cached values is configurable via an attribute (default is ten minutes). I used aks_cache as documentation for the database functions. Thanks a lot for the hints.

Offline

#8 2014-08-28 16:06:33

ToK
Plugin Author
Registered: 2014-02-18
Posts: 23

Re: tok_owm_current – new weather plugin

tok_owm_current may be be used as container tag now also. The curly brackets around the variable names have been changed to square brackets as textile uses curly brackets as well. Sorry for the inconvenience, if someone uses this plugin already.

Offline

Board footer

Powered by FluxBB