-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
47 lines (37 loc) · 1.59 KB
/
lambda_function.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
import requests
import os
from lyricsgenius import Genius
import random
import re
import tweepy
def get_lyric():
# getting lyrics from genius
GENIUS_ACCESS_TOKEN = os.environ.get("GENIUS_ACCESS_TOKEN")
genius = Genius(GENIUS_ACCESS_TOKEN)
album = genius.search_album('Midnights 3am Edition', "Taylor Swift")
random_track = random.choice(album.tracks)
track = random_track.to_dict()
lyrics = track['song']['lyrics']
# first we clean the lyrics and turn into a list so we can get a random lyric
lines = lyrics.split('\n')
cleaned = [line for line in lines if '[' not in line and line != '']
# some problems wiht ads at the last lines so we have to split to get lyrics
cleaned[-1] = cleaned[-1].split("See Taylor Swift")[0]
cleaned[-1] = re.split('(\d+)', cleaned[-1])[0]
# now after cleaning we get a random lyric, by getting a random index (except for the last line b/c possible index error)
index = random.randrange(0, len(cleaned) - 1)
# return the line and another line after it
return cleaned[index] + '\n' + cleaned[index + 1]
def lambda_handler(event, context):
# getting the tweet
random_lyric = get_lyric()
## authentication for twitter
client = tweepy.Client(
consumer_key=os.environ.get("CONSUMER_KEY"),
consumer_secret=os.environ.get("CONSUMER_SECRET"),
access_token=os.environ.get("TWEET_ACCESS_TOKEN"),
access_token_secret=os.environ.get("ACCESS_TOKEN_SECRET")
)
# posting the tweet
client.create_tweet(text=random_lyric)
return {"statusCode": 200, "tweet": random_lyric}