Mercurial > public > tweet-analysis
annotate src/handlers/sentiment.py @ 4:cfd876570008
attach inline policy to function to access screts
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Thu, 16 Sep 2021 18:03:26 +0200 |
parents | 5c36f51105c2 |
children | db2ce7097ff3 |
rev | line source |
---|---|
1
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
1 import json |
3
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
2 import requests |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
3 import boto3 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
4 import base64 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
5 from botocore.exceptions import ClientError |
1
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
6 |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
7 |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
8 def get_tweet_sentiment(event, context): |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
9 """ |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
10 :param event: dict, required |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
11 API Gateway Lambda Proxy Input Format |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
12 :param context: object, required |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
13 Lambda Context runtime methods and attributes |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
14 :return: dict |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
15 API Gateway Lambda Proxy Output Format |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
16 """ |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
17 |
3
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
18 twitter_url = create_twitter_url(user='dennisconcep') |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
19 twitter_key = get_twitter_key() |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
20 bearer_token = twitter_key['BEARER'] |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
21 twitter_header = {"Authorization": "Bearer {}".format(bearer_token)} # Auth header |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
22 twitter_response = requests.request("GET", twitter_url, headers=twitter_header) |
1
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
23 |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
24 return { |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
25 "statusCode": 200, |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
26 "body": json.dumps({ |
3
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
27 "message": "hello world", |
4
cfd876570008
attach inline policy to function to access screts
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
3
diff
changeset
|
28 "tweets": twitter_response.json() |
1
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
29 }), |
0277e7fc0f0a
first successful sam deploy
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
diff
changeset
|
30 } |
3
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
31 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
32 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
33 def create_twitter_url(user, max_results=100): |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
34 """ |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
35 Create url to fetch `max_results` of tweets from `user` |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
36 :param user: string, required |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
37 :param max_results: int, optional, default 100 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
38 :return: string url |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
39 """ |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
40 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
41 formatted_max_results = 'max_results={}'.format(max_results) |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
42 formatted_user = 'query=from:{}'.format(user) |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
43 url = "https://api.twitter.com/2/tweets/search/recent?{}&{}".format(formatted_max_results, formatted_user) |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
44 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
45 return url |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
46 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
47 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
48 def get_twitter_key(): |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
49 """ |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
50 Get Twitter Api Key from AWS Secrets Manager |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
51 :return: |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
52 """ |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
53 secret_name = "tweet-analysis-keys" |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
54 region_name = "eu-west-2" |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
55 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
56 # Create a Secrets Manager client |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
57 session = boto3.session.Session() |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
58 client = session.client( |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
59 service_name='secretsmanager', |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
60 region_name=region_name |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
61 ) |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
62 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
63 try: |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
64 get_secret_value_response = client.get_secret_value(SecretId=secret_name) |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
65 except ClientError as e: |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
66 if e.response['Error']['Code'] == 'DecryptionFailureException': |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
67 # Secrets Manager can't decrypt the protected secret text using the provided KMS key. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
68 # Deal with the exception here, and/or rethrow at your discretion. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
69 raise e |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
70 elif e.response['Error']['Code'] == 'InternalServiceErrorException': |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
71 # An error occurred on the server side. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
72 # Deal with the exception here, and/or rethrow at your discretion. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
73 raise e |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
74 elif e.response['Error']['Code'] == 'InvalidParameterException': |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
75 # You provided an invalid value for a parameter. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
76 # Deal with the exception here, and/or rethrow at your discretion. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
77 raise e |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
78 elif e.response['Error']['Code'] == 'InvalidRequestException': |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
79 # You provided a parameter value that is not valid for the current state of the resource. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
80 # Deal with the exception here, and/or rethrow at your discretion. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
81 raise e |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
82 elif e.response['Error']['Code'] == 'ResourceNotFoundException': |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
83 # We can't find the resource that you asked for. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
84 # Deal with the exception here, and/or rethrow at your discretion. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
85 raise e |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
86 else: |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
87 # Decrypts secret using the associated KMS CMK. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
88 # Depending on whether the secret is a string or binary, one of these fields will be populated. |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
89 if 'SecretString' in get_secret_value_response: |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
90 secret = get_secret_value_response['SecretString'] |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
91 else: |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
92 secret = base64.b64decode(get_secret_value_response['SecretBinary']) |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
93 |
5c36f51105c2
fetch tweets working
Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
parents:
1
diff
changeset
|
94 return json.loads(secret) |