-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_tweet.py
83 lines (61 loc) · 2.15 KB
/
create_tweet.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import random
import requests # for local development
import sys
import time
import tweepy
from credentials import * # use this one for testing
HAIKU_API = "https://haiku.kremer.dev"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
tweepy_api = tweepy.API(auth)
def respond(err, res=None):
print(res) # adds the generated Haiku to the CloudWatch logs
return {
"statusCode": 400 if err else 200,
"body": err.message if err else json.dumps(res),
"headers": {"Content-Type": "application/json",},
}
def lambda_handler(event, context):
pg = HaikuBot()
return respond(None, pg.run())
class HaikuBot:
def __init__(self, woeid: int = 23424977):
self.woeid = woeid
self.trends = self.get_twitter_trends(self.woeid)
def run(self):
keywords = self.trends
keyword = random.choice(
[keyword for keyword in keywords if keyword.isalpha() and keyword[1:].islower()]
)
generated_haiku = self.request_haiku(keyword)
full_haiku = self.format_haiku(keyword, generated_haiku)
tweepy_api.update_status(" \n".join(full_haiku))
print(" \n".join(full_haiku))
def request_haiku(self, keyword):
r = requests.get(HAIKU_API, {"keyword": keyword})
return r.json()
def parse_trends(self, full_trend):
results = []
if " " in full_trend:
return full_trend.split(" ")
# elif multiple capitals, split on those.
def get_twitter_trends(self, woeid: int = 23424977):
trends = tweepy_api.trends_place(woeid)
keywords = []
for trend in trends[0]["trends"]:
name = trend["name"]
if name[0] == "#":
name = name[1:]
keywords.append(name)
return keywords
def format_haiku(self, keyword, generated_haiku: list):
return ["#" + keyword, "-" * 2 * len(keyword)] + generated_haiku
def main():
""" To run this python script locally:
format: $ python3 create_tweet.py
"""
pg = HaikuBot()
print(pg.run())
if __name__ == "__main__":
main()