-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
34 lines (29 loc) · 1.01 KB
/
lambda_function.py
File metadata and controls
34 lines (29 loc) · 1.01 KB
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
import json
import bs4 as bs
from botocore.vendored import requests
import random
import re
def getQuote(keyword):
try:
list_quotes = []
url = requests.get('https://www.brainyquote.com/search_results?q='+keyword).text
soup = bs.BeautifulSoup(url, 'html.parser')
for quote in soup.find_all('div', class_='clearfix'):
quote_text = quote.a.text
# only select quote shorter than 400 characters
if len(quote_text) < 400:
list_quotes.append(quote_text)
else: pass
random_quote = random.choice(list_quotes)
# remove the brackets
# random_quote = re.sub(r'^[\'\"]|[\'\"]$', '', random_quote)
return random_quote
except Exception as e:
return 'Keyword not found.'
def lambda_handler(event, context):
keyword = event['queryStringParameters']['k']
quote = getQuote(keyword)
return {
"statusCode": 200,
"body": json.dumps(quote)
}