Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xyb994 committed Feb 2, 2017
0 parents commit 2eb41c2
Show file tree
Hide file tree
Showing 15 changed files with 444 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

#stations cache database
.sqlite
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# AQI Alfred Workflow

## Setup
- Get your own aqicn.org token at (http://aqicn.org/data-platform/token/)[http://aqicn.org/data-platform/token/]
- In AQI Alfred Workflow's script filter, replace ```API_KEY``` with the token you got

## Use
- ```aqi```: get current city's AQI

- ```aqistation <name>``` to get AQI of all stations containing <name>

- While focused on the item
- ```Shift```: preview the AQI page
- ```Enter```: go to the AQI page
Binary file added src/2EE2D13E-E479-467D-99DE-44CBBC0F286A.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/696B119B-EF74-41B2-B41A-8F3C29EE8311.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions src/aqi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import sys
import json
import requests
import requests_cache

API_KEY = sys.argv[1]

items = []

def get_aqi_by_ip():
url_format = 'https://api.waqi.info/feed/here/?token={0}'
url = url_format.format(API_KEY)

with requests_cache.disabled():
response = requests.get(url)

if response.json()['status'] == 'ok':
data = response.json()['data']

aqi_index = data['aqi']
update_time = data['time']['s']
timezone = data['time']['tz']
url = data['city']['url']
city_name = data['city']['name']

if aqi_index != '-':
aqi_result = [aqi_index, update_time, timezone, url, city_name]
else:
aqi_result = [0, update_time, timezone, url, city_name]
return aqi_result
else:
print(json.dumps({"items": [{"title": "Error", "subtitle": data}]}))

def main():
requests_cache.install_cache(
'stations_cache',
backend='sqlite',
expire_after=1296000
)

aqi_result = get_aqi_by_ip()

aqi_index = aqi_result[0]
aqi_description = get_aqi_description(aqi_index)

update_time = aqi_result[1]
timezone = aqi_result[2]
url = aqi_result[3]
city_name = aqi_result[4]

#To-DO: UnicodeEncodeError station_name
# title = "[{0}] {1}".format(str(aqi_index), station_name)

title = str(aqi_index) + " @ " + city_name

subtitle = "{0}, {1} GMT{2}"\
.format(aqi_description, update_time, timezone)

icon = {"type": "png", "path": "aqi_index_scale_color/"\
+ aqi_description + ".png"}

add_item(title, subtitle, icon, url)

alfred_item_string = {"items": items}
print(json.dumps(alfred_item_string))


def get_aqi_description(aqi_index):
if aqi_index < 51:
return 'Good'
elif aqi_index < 101 and aqi_index > 50:
return 'Moderate'
elif aqi_index < 151 and aqi_index > 100:
return 'Unhealthy For Sensitive Group'
elif aqi_index < 201 and aqi_index > 150:
return 'Unhealthy'
elif aqi_index < 251 and aqi_index > 200:
return 'Very Unhealthy'
elif aqi_index < 300 and aqi_index > 250:
return 'Hazardous'


def add_item(title, subtitle, icon, url):
item = {
"uid": 'aqi',
"title": title,
"subtitle": subtitle,
"icon": icon,
"quicklookurl": url,
"valid": True,
"arg": url
}
items.append(item)
return item


if __name__ == '__main__':
main()
Binary file added src/aqi_index_scale_color/Good.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/aqi_index_scale_color/Hazardous.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/aqi_index_scale_color/Moderate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/aqi_index_scale_color/Unhealthy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/aqi_index_scale_color/Very Unhealthy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
117 changes: 117 additions & 0 deletions src/aqistation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import sys
import json
import requests
import requests_cache

SEARCH_KEYWORD = sys.argv[1]
API_KEY = sys.argv[2]

items = []


def main():
requests_cache.install_cache(
'stations_cache',
backend='sqlite',
expire_after=1296000
)

search_results = get_search_result()

for search_result in search_results:
station_id = search_result['uid']

aqi_result = get_aqi_info(station_id)

aqi_index = aqi_result[0]
aqi_description = get_aqi_description(aqi_index)

station_name = search_result['station']['name']

update_time = aqi_result[1]
timezone = aqi_result[2]
url = aqi_result[3]

#To-DO: UnicodeEncodeError station_name
# title = "[{0}] {1}".format(str(aqi_index), station_name)

title = str(aqi_index) + " @ " + station_name
subtitle = "{0}, {1} GMT {2}"\
.format(aqi_description, update_time, timezone)

icon = {"type": "png", "path": "aqi_index_scale_color/"\
+ aqi_description + ".png"}

add_item(title, subtitle, icon, url)

alfred_item_string = {"items": items}
print(json.dumps(alfred_item_string))


def get_search_result():
url_format = 'https://api.waqi.info/search/?token={0}&keyword={1}'
url = url_format.format(API_KEY, SEARCH_KEYWORD)
response = requests.get(url)
data = response.json()['data']

if response.json()['status'] == 'ok':
if data != []:
return data
else:
print(json.dumps({"items": [{"title": "Error", "subtitle": "No matching station found"}]}))
else:
print(json.dumps({"items": [{"title": "Error", "subtitle": data}]}))


def get_aqi_info(station_id):
url_format = 'https://api.waqi.info/feed/@{0}/?token={1}'
url = url_format.format(station_id, API_KEY)
with requests_cache.disabled():
response = requests.get(url)

if response.json()['status'] == 'ok':
data = response.json()['data']
aqi_index = data['aqi']

if aqi_index != '-':
update_time = data['time']['s']
timezone = data['time']['tz']
url = data['city']['url']
aqi_result = [aqi_index, update_time, timezone, url]
else:
aqi_result = [0, "-", "-", ""]
else:
aqi_result = [0, "-", "-", ""]

return aqi_result

def get_aqi_description(aqi_index):
if aqi_index < 51:
return 'Good'
elif aqi_index < 101 and aqi_index > 50:
return 'Moderate'
elif aqi_index < 151 and aqi_index > 100:
return 'Unhealthy For Sensitive Group'
elif aqi_index < 201 and aqi_index > 150:
return 'Unhealthy'
elif aqi_index < 251 and aqi_index > 200:
return 'Very Unhealthy'
elif aqi_index < 300 and aqi_index > 250:
return 'Hazardous'


def add_item(title, subtitle, icon, url):
item = {
"title": title,
"subtitle": subtitle,
"icon": icon,
"quicklookurl": url,
"valid": True,
"arg": url
}
items.append(item)
return item


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_KEY = 'eca2ecd094bbe2b5dabfa2ea201ea9cd577f0403'
Binary file added src/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2eb41c2

Please sign in to comment.