Last active
January 15, 2022 02:52
-
-
Save johncalvinroberts/0f6b43039a8af3cdb6edc95a37f09eac to your computer and use it in GitHub Desktop.
XBar plugin to show the current AQI every hour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <xbar.title>AQI</xbar.title> | |
// <xbar.version>v1.0</xbar.version> | |
// <xbar.author>John Roberts</xbar.author> | |
// <xbar.author.github>johncalvinroberts</xbar.author.github> | |
// <xbar.desc>The script displays AQI in location</xbar.desc> | |
// <xbar.image>https://raw.githubusercontent.com/christophschlosser/bitbar-plugins/checkhosts/Network/checkhosts.png</xbar.image> | |
// <xbar.dependencies>go</xbar.dependencies> | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io" | |
"net/http" | |
) | |
type aqiResponse struct { | |
Data struct { | |
Iaqi struct { | |
Pm25 struct { | |
Val int `json:"v"` | |
} `json:"pm25"` | |
} `json:"iaqi"` | |
} `json:"data"` | |
} | |
/* | |
To get an API token, go here: https://aqicn.org/api | |
Or ask me for mine! Each token is rate limited to 1000 requests/second. That's a lot! | |
*/ | |
var token string = "YOUR_TOKEN_HERE" | |
var layout string = "3:04pm, Jan 2" | |
func main() { | |
resp, err := http.Get(fmt.Sprintf("https://api.waqi.info/feed/shanghai/?token=%s", token)) | |
if err != nil { | |
fmt.Println(":rotating_light:") | |
fmt.Println("---") | |
fmt.Println(err.Error()) | |
} | |
defer resp.Body.Close() | |
body, err := io.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println(err.Error()) | |
fmt.Println(":rotating_light:") | |
fmt.Println("---") | |
} | |
var currentReading aqiResponse | |
err = json.Unmarshal(body, ¤tReading) | |
if err != nil { | |
fmt.Println(":rotating_light:") | |
fmt.Println("---") | |
} | |
val := currentReading.Data.Iaqi.Pm25.Val | |
emoji := ":sunny:" | |
if val > 80 { | |
emoji = ":partly_sunny:" | |
} | |
if val > 115 { | |
emoji = ":foggy:" | |
} | |
if val > 140 { | |
emoji = ":mask:" | |
} | |
if val > 150 { | |
emoji = ":no_entry:" | |
} | |
if val > 160 { | |
emoji = ":red_circle:" | |
} | |
if val > 180 { | |
emoji = ":dizzy_face:" | |
} | |
if val > 200 { | |
emoji = ":skull:" | |
} | |
message := fmt.Sprintf("%s %d", emoji, val) | |
fmt.Println(message) | |
fmt.Println("---") | |
fmt.Println("Refresh | refresh=true") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment