-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_music_client.py
71 lines (60 loc) · 2.28 KB
/
google_music_client.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
71
import logging
import re
from gmusicapi import Mobileclient
from resources import Track
LOGGER = logging.getLogger(__name__)
class GoogleMusicClient(object):
def __init__(self, username, password):
self._username = username
self._password = password
self._apiClient = None
def initConnection(self):
self._apiClient = Mobileclient(debug_logging=False)
if not self._apiClient.login(self._username, self._password):
raise RuntimeError("Could not connect %s to Google Music." % \
self._username)
def addPlaylist(self, playlist):
plid = self._apiClient.create_playlist(playlist.name)
tids = []
for track in playlist.tracks:
aaid = self._getAllAccessTrackId(track)
if aaid:
try:
tid = self._apiClient.add_aa_track(aaid)
if tid:
tids.append(tid)
else:
LOGGER.warning( "Could not add track %s to library.", str(track))
except:
LOGGER.error("Could not add track %s to library.", str(track))
continue
else:
LOGGER.warning("Track %s not found.", str(track))
self._apiClient.add_songs_to_playlist(plid, tids)
def addAlbum(self, album):
aaid = self._getAllAccessAlbumId(album)
if aaid:
albumInfo = self._apiClient.get_album_info(aaid, include_tracks=True)
for track in albumInfo["tracks"]:
try:
self._apiClient.add_aa_track(track[Track.GM_ID_KEY])
except:
LOGGER.error("Could not add track %s to library.", str(track))
continue
else:
LOGGER.warning("Album %s not found.", str(album))
def _getAllAccessFirstResult(self, resource):
queryStr = re.sub("[-:\(\)\",]","", "%s %s" % (resource.name,
resource.artist))
queryStr = re.sub("\s+", "+", queryStr)
searchResults = self._apiClient.search_all_access(queryStr)
gmusicResources = searchResults.get(resource.GM_HITS_KEY)
if gmusicResources:
firstResult = gmusicResources[0][resource.GM_NAME]
return firstResult[resource.GM_ID_KEY]
else:
return None
def _getAllAccessTrackId(self, track):
return self._getAllAccessFirstResult(track)
def _getAllAccessAlbumId(self, album):
return self._getAllAccessFirstResult(album)