forked from QuinnyPig/awswhatsnew
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.py
110 lines (90 loc) · 4.03 KB
/
handler.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import json
import logging
import os
import time
import boto3
import feedparser
import requests
import twitter
from botocore.client import Config
from html.parser import HTMLParser
from aws_lambda_powertools.utilities import parameters
from slack_sdk import WebClient
logger = logging.getLogger()
logger.setLevel(logging.INFO)
ssm_provider = parameters.SSMProvider()
stage = os.environ['stage']
logger.info(f"STAGE: " + stage)
consumer_key = ssm_provider.get("/whatsnew/twitter/"+stage+"/consumer_key", decrypt=True)
consumer_secret = ssm_provider.get("/whatsnew/twitter/"+stage+"/consumer_secret", decrypt=True)
access_token_key = ssm_provider.get("/whatsnew/twitter/"+stage+"/access_token_key", decrypt=True)
access_token_secret = ssm_provider.get("/whatsnew/twitter/"+stage+"/access_token_secret", decrypt=True)
api = twitter.Api(consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=access_token_key,
access_token_secret=access_token_secret)
bot_user_oauth_token = ssm_provider.get("/whatsnew/slack/"+stage+"/bot_user_oauth_token", decrypt=True)
bot_channel_id = ssm_provider.get("/whatsnew/slack/"+stage+"/channel_id", decrypt=True)
client = WebClient(token=bot_user_oauth_token)
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.strict = False
self.convert_charrefs = True
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return "".join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
posts_table = boto3.resource("dynamodb", region_name="af-south-1").Table(os.environ["PostsTableName"])
def within(t: time.struct_time, minutes: int) -> bool:
return abs(time.mktime(time.gmtime()) - time.mktime(t)) <= (minutes * 60)
def already_posted(guid: str) -> bool:
return "Item" in posts_table.get_item(Key={"guid": guid})
def post_to_twitter(payload, entry):
api.PostUpdate(
payload
+ "... "
+ entry.link,
verify_status_length=False,
)
def in_feed_body(link):
post_body = requests.get(link)
post_body_content = str(post_body.content).casefold()
if ("Cape Town".casefold() in post_body_content) or ("all commercial AWS Regions".casefold() in post_body_content) or ("all AWS Regions".casefold() in post_body_content):
return True
else:
return False
def post_to_slack(payload, entry):
response = client.chat_postMessage(
channel=bot_channel_id,
text=payload + "\n " + entry.link,
icon_emoji=":earth_africa:",
username="AWS Whats New in Africa"
)
def lambda_handler(event, context):
recency_threshold = int(os.environ['PostRecencyThreshold'])
payload = ""
for entry in feedparser.parse("http://aws.amazon.com/new/feed/").entries:
logger.info(f"Checking {entry.guid} - {entry.title}")
if ("Cape Town" in entry.title) or ("Cape Town" in entry.description) or (in_feed_body(entry.link)):
if within(entry.published_parsed, minutes=recency_threshold) and not already_posted(entry.guid):
logger.info(f"Posting {entry.guid} - {entry.title}")
payload = entry.title + "\n\n" #+ strip_tags(entry.description)
try:
#length = 300
while len(payload) > 249:
payload = entry.title + "\n\n" #+ strip_tags(entry.description)
logger.info(f"Posting with body length: " + str(len(payload)))
logger.info(f"Posting with body: " + payload + "... " + entry.link)
post_to_slack(payload, entry)
#post_to_twitter(payload, entry)
posts_table.put_item(
Item={"guid": entry.guid, "title": entry.title, "link": entry.link}
)
except Exception:
logger.exception(f"Failed to post")