This repository has been archived by the owner on Dec 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_genre_playlists.py
62 lines (49 loc) · 1.61 KB
/
generate_genre_playlists.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
import sys
import getpass
from gmusicapi import Mobileclient
email = raw_input("User (gmail): ")
pswd = getpass.getpass('Password:')
api = Mobileclient()
logged_in = api.login(email, pswd, Mobileclient.FROM_MAC_ADDRESS)
if not logged_in:
print "Authentification error"
sys.exit()
songs = api.get_all_songs()
for song in songs:
song['genre'] = song['genre'].replace(" ", "").lower()
if ';' in song['genre']:
song['genre'] = song['genre'].split(';')
elif ',' in song['genre']:
song['genre'] = song['genre'].split(',')
elif '/' in song['genre']:
song['genre'] = song['genre'].split('/')
else:
song['genre'] = [song['genre']]
genres_map = {}
for s in songs:
for g in s['genre']:
genres_map.setdefault(g, [])
genres_map[g].append(s['id'])
good_genres = []
print "Available genres:"
for g in genres_map.keys():
if len(g) == 0:
continue # empty genre tag
if len(genres_map[g]) >= 20:
print g + '\t' + str(len(genres_map[g])) + ' tracks --> generating playlist'
good_genres.append(g)
else:
print g + '\t' + str(len(genres_map[g])) + ' tracks --> too few tracks'
print
# remove all existing auto-playlists first
playlists = api.get_all_playlists()
for p in playlists:
if p['name'].startswith("genre auto-playlist"):
print "Removing playlist " + p['name']
api.delete_playlist(p['id'])
for genre in good_genres:
playlist = genres_map[genre]
playlist_name = "genre auto-playlist - " + genre
print "Creating playlist " + playlist_name
pid = api.create_playlist(playlist_name, description="Test " + genre + " playlist created automatically with a script", public=False)
api.add_songs_to_playlist(pid, playlist)