-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenius_api.py
70 lines (52 loc) · 2.41 KB
/
genius_api.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
import config
import random
from lyricsgenius import Genius
from lyricsgenius.artist import Artist
# -------------------- GENIUS API SETUP -----------------
genius_client_id = config.genius_client_id
genius_client_secret_id = config.genius_client_secret_id
genius_client_access_token = config.genius_client_access_token
# -------------------------------------------------------
# -------------------- PARAMETERS -----------------------
# Dictionary for holding map from DOOM name / alias to Genius artist_id.
identities = {"MF DOOM": 70}
DOOM_ARTIST_ID = 70
# Number of annotations a song must have to "qualify"
MIN_ANNOTATIONS = 5
# Size of snippet to look for
MIN_LYRIC_LENGTH = 50
MAX_LYRIC_LENGTH = 150
# -------------------------------------------------------
def get_tweet_info():
# Create a new genius object
genius = Genius(genius_client_access_token)
# Pick a random song until we find one that has annotations (shouldn't take long)
print("Looking for annotations ...")
while True:
# Narrow selection by picking an album
albums = genius.artist_albums(DOOM_ARTIST_ID)
selected_album = random.choice(albums['albums'])
print("\tALBUM: " + selected_album['name'])
# Get the songs from the album and pick one
tracks = genius.album_tracks(selected_album['id'])
selected_track = random.choice(tracks['tracks'])
selected_song = selected_track['song']
print("\tSONG: " + selected_song['title_with_featured'])
# Check that the song has a "qualifying" number of annotations and they are of right length
annotation_count = selected_song['annotation_count']
print("\t\t" + str(annotation_count) + " annotations found...")
# If we found a song with sufficient annotations, continue; otherwise, try again
if selected_song['annotation_count'] >= MIN_ANNOTATIONS:
# Pick a random annotation for the selected song
selected_lyric = random.choice(genius.song_annotations(selected_song['id']))[0]
tweet_text = selected_lyric + "\n\n(" + selected_song['title_with_featured'] + ", " + selected_album[
'name'] + ")"
return tweet_text
else:
print("Trying again...")
# -------------------- FOR TESTING ----------------------
def main():
get_tweet_info()
if __name__ == '__main__':
main()
# -------------------------------------------------------