-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrandomsongs.py
63 lines (52 loc) · 1.52 KB
/
randomsongs.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
#!/usr/bin/env python3
import os
import argparse
import json
from gmusicapi import Mobileclient
import codecs
import random
G = Mobileclient()
playlist = "51d6ea84-a1db-4202-aa5b-afb0fcb7b94d"
def login():
"""logs in to gmusic"""
mc = Mobileclient()
#mc.perform_oauth()
G.oauth_login(Mobileclient.FROM_MAC_ADDRESS)
def getlibrary():
"""get all library from library"""
library = G.get_all_songs()
return library
def generaterandom(mylibrary):
"""generates 150 random songs"""
randomsongs = random.sample(mylibrary, 200)
return randomsongs
def removesongs():
"""removes old music"""
playlists = G.get_all_user_playlist_contents()
for randomplaylist in playlists:
if playlist in randomplaylist.values():
for song in randomplaylist["tracks"]:
G.remove_entries_from_playlist(song["id"])
else:
print("Skipping playlist, {}".format(randomplaylist["name"]))
def addsongs(randomsong):
"""adding songs to playlist"""
songstoadd = []
for songs in randomsong:
song = songs["id"]
try:
if not songs["rating"] == "1":
G.add_songs_to_playlist(playlist, song)
except KeyError:
G.add_songs_to_playlist(playlist, song)
return randomsong
def main():
login()
mylibrary = getlibrary()
print("Removing old songs")
removesongs()
randomsong = generaterandom(mylibrary)
print("Adding new songs")
addsongs(randomsong)
if __name__ == '__main__':
main()