Xfce Forum

Sub domains
 

You are not logged in.

#1 2020-02-28 15:19:06

raspatan
Member
Registered: 2020-02-28
Posts: 5

Could Weather Plugin use API data from AirVisual?

I want to make a feature request regarding the Weather Plugin, xfce4-weather-plugin, (and use it in Bountysource to reward XFCE for their awesome effort). The request is to add data about thecurrent (and estimated) air quality of the location. This data is provided by AirVisual (https://www.airvisual.com/). They have an API (https://www.airvisual.com/air-pollution-data-api) but it's free only for 10,000 calls per month, obviously insufficient for a plug-in of this sort (unless the plugin has some sort of log-in involved). Currently, the plug-in gets the information from met.no, a Norwegian service, which free data does not include air quality (https://www.met.no/en/free-meteorologic … e-from-MET).

Question: would it be impossible for the weather plugin to add air quality data using AirVisual? Are you aware of another source that might perhaps achieve this? In other words, is my feature request actually possible to achieve?

Thanks smile

Last edited by raspatan (2020-02-28 15:19:58)

Offline

#2 2020-02-29 14:30:30

ToZ
Moderator
From: Canada
Registered: 2011-06-02
Posts: 6,381

Re: Could Weather Plugin use API data from AirVisual?

Hello and welcome.

raspatan wrote:

Question: would it be impossible for the weather plugin to add air quality data using AirVisual?

You can make the feature request at the bug tracker and support it via bountysource. Someone will need to be interested enough to add the functionality.

Are you aware of another source that might perhaps achieve this?

Yes! The xfce4-genmon-plugin can display outputs of bash scripts. For example, a script like this (thrown together over morning coffee - YMMV):

#!/bin/bash

# Air Quality Index - requires wget, jq & and airvisual api key
# get free Community API from www.airvisual.com
#	- create your own API key
#	- enter API key below

### CHANGE THESE VALUES TO SUIT
CITY="Oshawa"
STATE="Ontario"
COUNTRY="Canada"
APIKEY="XXXXXXX-XXXX-XXXX-XXXXXXXXXXX"

### make sure that only one instance of this script is running per user
lockfile=/tmp/.airquality.$USER.lockfile
if ( set -o noclobber; echo "locked" > "$lockfile") 2> /dev/null; then
  	trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
  	echo "airqualityDEBUG: Locking succeeded" >&2
  	
  	CACHEDIR="$HOME/.cache/aq"
  	
	### make sure network is up to prevent errors and screwy behaviour
	while ! ping -q -c 1 -W 1 8.8.8.8 >/dev/null; do sleep 1; done
	
	### make sure tmp cache dir exists or delete contents
	[[ -d "$CACHEDIR" ]] && rm -rf $CACHEDIR/* || mkdir -p $CACHEDIR

	### prepare the airquality api call 
	CALL="api.airvisual.com/v2/city?city=$CITY&state=$STATE&country=$COUNTRY&key=$APIKEY"
	
	### make the call
	wget -O $CACHEDIR/aq.json --no-check-certificate --quiet --method GET --timeout=0 --header '' $CALL
	
	# parse the data
	CITY=$(jq ".data.city" $CACHEDIR/aq.json | tr -d '"')
	STATE=$(jq ".data.state" $CACHEDIR/aq.json | tr -d '"')
	COUNTRY=$(jq ".data.country" $CACHEDIR/aq.json | tr -d '"')
	TIMESTAMP=$(jq ".data.current.weather.ts" $CACHEDIR/aq.json | tr -d '"')
	TEMPERATURE=$(jq ".data.current.weather.tp" $CACHEDIR/aq.json)
	PRESSURE=$(jq ".data.current.weather.pr" $CACHEDIR/aq.json)
	HUMIDITY=$(jq ".data.current.weather.hu" $CACHEDIR/aq.json)
	WINDSPEED=$(jq ".data.current.weather.ws" $CACHEDIR/aq.json)
	WINDDIR=$(jq ".data.current.weather.wd" $CACHEDIR/aq.json)
	AQINDEX=$(jq ".data.current.pollution.aqius" $CACHEDIR/aq.json)
	
	# generate color codes for air quality index
	COLOR="black"
	if (($AQINDEX >= 0 && $AQINDEX <= 50)); then COLOR="green"; fi
	if (($AQINDEX >= 51 && $AQINDEX <= 100)); then COLOR="yellow" ; fi
	if (($AQINDEX >= 100 && $AQINDEX <= 150)); then COLOR="orange"; fi
	if (($AQINDEX >= 151 && $AQINDEX <= 200)); then COLOR="red"; fi
	if (($AQINDEX >= 201 && $AQINDEX <= 300)); then COLOR="purple"; fi
		
	#debug
	#echo "CITY=$CITY"
	#echo "STATE=$STATE"
	#echo "COUNTRY=$COUNTRY"
	#echo "TIMESTAMP=$TIMESTAMP"
	#echo "TEMPERATURE=$TEMPERATURE"
	#echo "PRESSURE=$PRESSURE"
	#echo "HUMIDITY=$HUMIDITY"
	#echo "WINDSPEED=$WINDSPEED"
	#echo "WINDDIR=$WINDDIR"
	#echo "AQINDEX=$AQINDEX"
	#echo "COLOR=$COLOR"
	
	# do the genmon
	echo "<txt><span foreground='white' background='$COLOR'>$AQINDEX</span></txt><tool>Air Quality Index
$CITY $STATE $COUNTRY

TEMPERATURE=$TEMPERATURE
PRESSURE=$PRESSURE
HUMIDITY=$HUMIDITY
WINDSPEED=$WINDSPEED
WINDDIR=$WINDDIR

$TIMESTAMP</tool>"	


### can't create lockfile - notify user and quit
else
  	echo "airqualityDEBUG: Lock failed, check for existing process and/or lock file and delete - exiting." >&2
  	exit 1
fi			

exit 0

...would work.

To use it, do the following:

  1. Save this content to a file on your hard drive

  2. make the file executable

  3. change the CITY, STATE, COUNTRY, APIKEY values in the first section (you need to get an APIKEY from aqivisual.com as you noted)

  4. Add the genmon plugin to the panel. Set the command to point to this script, uncheck the label, and set the period to how often you would like it to check

aqi.png

Offline

#3 2020-02-29 15:36:37

raspatan
Member
Registered: 2020-02-28
Posts: 5

Re: Could Weather Plugin use API data from AirVisual?

Thank you very much! That is certainly one solution, but one that would only help me. It would be great if the air quality reading comes by default in the weather plug in too. Do you think it is feasible for such plug-in to incorporate this feature? I don't want to waste a bounty on something is not possible.

Offline

#4 2020-02-29 15:57:55

ToZ
Moderator
From: Canada
Registered: 2011-06-02
Posts: 6,381

Re: Could Weather Plugin use API data from AirVisual?

raspatan wrote:

Do you think it is feasible for such plug-in to incorporate this feature? I don't want to waste a bounty on something is not possible.

Is it feasible? Yes.

met.no does offer an air quality api. The challenge will be finding someone who is willing to program it in. Create the enhancement request and see what happens.

Offline

#5 2020-03-02 23:52:55

raspatan
Member
Registered: 2020-02-28
Posts: 5

Re: Could Weather Plugin use API data from AirVisual?

I created the feature request (https://bugzilla.xfce.org/show_bug.cgi?id=16482) and will do the bounty asap. Btw, I tried using your code in the meantime and doesn't work. I created my API, changed variables, installed genmon, added to panel, but when point command to file in home directory (~/air_quality), it says "No such file or directory". The script is exetuable. Running on terminal shows "airqualityDEBUG: Locking succeeded". Haven't found help online. It's probably a too basic error hmm Can you help me please?

Offline

#6 2020-03-03 00:15:28

ToZ
Moderator
From: Canada
Registered: 2011-06-02
Posts: 6,381

Re: Could Weather Plugin use API data from AirVisual?

raspatan wrote:

I created the feature request (https://bugzilla.xfce.org/show_bug.cgi?id=16482) and will do the bounty asap. Btw, I tried using your code in the meantime and doesn't work. I created my API, changed variables, installed genmon, added to panel, but when point command to file in home directory (~/air_quality), it says "No such file or directory". The script is exetuable. Running on terminal shows "airqualityDEBUG: Locking succeeded". Haven't found help online. It's probably a too basic error hmm Can you help me please?

Genmon doesn't understand the "~" indicator - it uses a very basic interpreter. Put the full pathname and file there. Something like:

/home/raspatan/air_quality

Offline

#7 2020-03-03 15:02:17

raspatan
Member
Registered: 2020-02-28
Posts: 5

Re: Could Weather Plugin use API data from AirVisual?

A trivial mistake indeed. Thanks. I had to install `jq` to make it work. Last question. Colours do not show up. Only black. Tried with different cities (to test different values). Any idea what might the the problem? I might be missing other packages. When run on terminal, it shows the code passed to the `echo` command (<txt><span foreground='white' background='green'>25</span></txt><tool>...) but that might be because the terminal doesn't handle HTML or whatever that is.

Last edited by raspatan (2020-03-03 15:02:45)

Offline

#8 2020-03-03 18:08:08

ToZ
Moderator
From: Canada
Registered: 2011-06-02
Posts: 6,381

Re: Could Weather Plugin use API data from AirVisual?

I can look into it. Which distro and which version of the genmon plugin are you using? And, which Appearance (GTK) theme are you using?

Offline

#9 2020-03-03 18:20:15

raspatan
Member
Registered: 2020-02-28
Posts: 5

Re: Could Weather Plugin use API data from AirVisual?

ToZ wrote:

I can look into it. Which distro and which version of the genmon plugin are you using? And, which Appearance (GTK) theme are you using?

That's the reason. I'm using Xubuntu 16.04 (genmon 3.4). CSS capabilities were introduced in version 4.0.1. I will switch next month to the newly 20.04, which uses 4.0.2 (https://launchpad.net/ubuntu/+source/xf … mon-plugin). Then it should work.

PS: I have usually struggled in the past installing newer version of packages. I can see genmon depends on a more updated version of xfce4-panel. It might mess up things if I try to upgrade xfce.

Last edited by raspatan (2020-03-03 18:20:57)

Offline

#10 2020-03-03 18:26:21

ToZ
Moderator
From: Canada
Registered: 2011-06-02
Posts: 6,381

Re: Could Weather Plugin use API data from AirVisual?

Best to follow the distro's packaging rules right now. Let me know if the issue persists after the upgrade.

Offline

Board footer

Powered by FluxBB