-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_weather_status.py
executable file
·65 lines (47 loc) · 1.7 KB
/
slack_weather_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
import json
import requests
SLACK_API_KEY = "get token from https://api.slack.com/custom-integrations/legacy-tokens"
OPENWEATHER_API_KEY = "get token from https://openweathermap.org/appid"
OPENWEATHER_CITY_ID = "find your city id https://openweathermap.org/current#cityid"
# https://openweathermap.org/weather-conditions
WEATHER_CODES = {
'2': ':lightning:',
'3': ':rain_cloud:',
'5': ':rain_cloud:',
'6': ':snowflake:',
'7': ':fog:',
'80': ':cloud:',
'800': ':sunny:',
'801': ':sun_small_cloud:',
}
def get_emoji(code):
code = str(code)
if code in WEATHER_CODES:
return WEATHER_CODES.get(code)
if code[:2] in WEATHER_CODES:
return WEATHER_CODES.get(code[:2])
if code[:1] in WEATHER_CODES:
return WEATHER_CODES.get(code[:1])
def to_celsius(kelvin):
return kelvin - 273.15
def build_status(text, emoji):
payload = {
'status_text': text,
'status_emoji': emoji
}
return json.dumps(payload)
if __name__ == '__main__':
r = requests.get('http://api.openweathermap.org/data/2.5/weather?id={}&appid={}'.format(OPENWEATHER_CITY_ID,
OPENWEATHER_API_KEY))
weather = r.json().get('weather')[0]
temp = to_celsius(r.json().get('main').get('temp'))
text = '{}°C and {}'.format(round(temp), weather.get('description'))
emoji = get_emoji(weather.get('id'))
r = requests.post('https://slack.com/api/users.profile.set', data={
'token': SLACK_API_KEY,
'profile': build_status(text, emoji)
})
if r.status_code != 200:
print(r.status_code)
print(r.json())