-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
executable file
·65 lines (52 loc) · 2.04 KB
/
demo.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
#!/usr/bin/env python3
import sys
import spotipy
import spotipy.util as util
import lyricsgenius
import pprint
genius = lyricsgenius.Genius("MGsI9MWlymRLnGmJ_HKdQlxxHXwWSGKWFGElUQUdT3q08gShQIE4x-6U527PlqQY")
genius.verbose = False # Turn off status messages
genius.remove_section_headers = True # Remove section headers (e.g. [Chorus]) from lyrics when searching
words = {}
def add_words(lyrics):
for word in lyrics:
word = word.lower().strip().replace('"','').replace(',','').replace('.','').strip('\'').strip('‘').replace('?','').strip('(').strip(')')
words[word] = words.get(word,0) + 1
def get_lyrics(artist, name):
song = genius.search_song(name, artist)
lyrics = song.lyrics
add_words(lyrics.split())
def get_track_info(tracks):
for item in tracks['items']:
track = item['track']
artist = track['artists'][0]['name']
name = track['name']
get_lyrics(artist, name)
if __name__ == '__main__':
if len(sys.argv) > 1:
username = sys.argv[1]
plist = sys.argv[2]
else:
print("Whoops, need your username and the name of the playlist!")
print("usage: python playlist.py [username] [playlist]")
sys.exit()
token = util.prompt_for_user_token(username)
if token:
sp = spotipy.Spotify(auth=token)
playlists = sp.current_user_playlists()
for playlist in playlists['items']:
if playlist['name'] == plist:
print()
print(f'The most used words in your playlist {plist}: ')
results = sp.playlist(playlist['id'], fields="tracks,next")
tracks = results['tracks']
get_track_info(tracks)
while tracks['next']:
tracks = sp.next(tracks)
get_track_info(tracks)
words = sorted(words.items(), key=lambda x: x[1], reverse=True)
for word in words:
output = word[0] + ': ' + str(word[1])
print(output)
else:
print("Can't get token for", username)