-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (45 loc) · 1.45 KB
/
main.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
66
import requests
from TwitterAPI import TwitterAPI # type: ignore
from settings import Settings
settings = Settings()
twitter = TwitterAPI(
settings.CONSUMER_KEY,
settings.CONSUMER_SECRET,
settings.ACCESS_TOKEN_KEY,
settings.ACCESS_TOKEN_SECRET,
)
def get_current_weather(location: str) -> dict:
"""
Args:
location (str): name of the location to get the current weather for
Returns:
current_weather (dict): current weather-data for the location
"""
url = settings.WEER_LIVE_BASE_URL
params = {"key": settings.WEER_LIVE_API_KEY, "locatie": location}
current_weather = requests.get(url=url, params=params).json()
return current_weather
def compose_tweet() -> str:
"""
Returns:
str: tweet-text
"""
current_weather = get_current_weather(settings.WEER_LIVE_LOCATION)
data = current_weather["liveweer"][0]
location = data.get("plaats")
condition = data.get("samenv").lower()
temperature = data.get("temp")
tweet_text = f"In {location} momenteel {condition} en {temperature} graden. #{location}"
return tweet_text
def send_tweet() -> str:
"""
Sends a tweet with the current weather
Returns:
status_code
"""
tweet_text = compose_tweet()
result = twitter.request("statuses/update", {"status": tweet_text})
return result.status_code
def app(event: str) -> str:
# pylint: disable=unused-argument
return send_tweet()