-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (38 loc) · 1.34 KB
/
main.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 lyricsgenius
import random
import tweepy
keys = ["have been defined in environment variables"]
genius = lyricsgenius.Genius('genius_client_access_token')
artist = genius.search_artist("Fleetwood Mac")
all_songs = [song for song in artist.songs]
def get_raw_lyrics():
genius_client_access_token = "genius_client_access_token"
genius = lyricsgenius.Genius(genius_client_access_token)
random_song_title = random.choice(all_songs)
lyrics = genius.search_song(random_song_title, "Fleetwood Mac").lyrics
song = random_song_title.upper()
return lyrics, song
def get_tweet_from(lyrics):
lines = lyrics.split('\n')
for index in range(len(lines)):
if lines[index] == "" or "[" in lines[index]:
lines[index] = "XXX"
lines = [i for i in lines if i != "XXX"]
random_num = random.randrange(0, len(lines)-1)
tweet = lines[random_num] + "\n" + lines[random_num+1]
tweet = tweet.replace("\\", "")
return tweet
def handler(event, context):
auth = tweepy.OAuthHandler(
keys['CONSUMER_API_KEY'],
keys['CONSUMER_API_SECRET_KEY']
)
auth.set_access_token(
keys['ACCESS_TOKEN'],
keys['ACCESS_TOKEN_SECRET']
)
api = tweepy.API(auth)
lyrics, song = get_raw_lyrics()
tweet = get_tweet_from(lyrics)
status = api.update_status(tweet)
return tweet