-
Notifications
You must be signed in to change notification settings - Fork 0
/
gDownloader.py
186 lines (132 loc) · 5.26 KB
/
gDownloader.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from gmusicapi import Mobileclient
from tqdm import tqdm
import os , pickle , threading , requests , shutil
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
import mutagen.id3
class Downloader:
def __init__( self , username1=None , password1=None , baseDirectory=None , pickleLIBFilePath=None ):
self.api = Mobileclient()
if username1 is not None:
if password1 is not None:
self.login( username1 , password1 )
if self.isLoggedIn() == True:
print("Logged In")
else:
raise Exception
# Setup Default Save Location
if baseDirectory is not None:
self.homeDIR = baseDirectory
self.libDIR = os.path.join( self.homeDIR , 'GMusicLocalLibraryPOOL' )
else:
self.homeDIR = os.path.expanduser("~")
self.libDIR = os.path.join( self.homeDIR , 'GMusicLocalLibraryPOOL' )
if not os.path.exists(self.libDIR):
os.makedirs(self.libDIR)
self.stations = {}
self.workingPlaylistOBJ = {}
self.needToDownloadSongs = None
self.Full = True
#self.playlists = None
self.localLibrary = None
#self.initializePlaylists()
if pickleLIBFilePath is not None:
self.initializeLocalLibrary(pickleLIBFilePath)
else:
self.initializeLocalLibrary()
def isLoggedIn(self):
x = self.api.is_authenticated()
return x
def login(self , userN , passW ):
self.api.login( userN , passW , Mobileclient.FROM_MAC_ADDRESS )
def initializePlaylists(self):
try:
self.playlists = pickle.load( open( libDIR + "libPlaylists.p" , "rb" ) )
except:
print("Recreating Playlists Save File")
self.playlists = {}
playlists['EDM'] = []
playlists['Relaxing'] = []
playlists['EDM'].append('4b40425b-2e11-388f-aeed-ea736b88662c')
pickle.dump( playlists , open( libDIR + "libPlaylists.p" , "wb" ) )
def initializeLocalLibrary(self , pickleLIBFilePath=None):
defaultPath2 = os.path.join( self.libDIR , "libDatabasePOOL.p" )
if pickleLIBFilePath is not None:
defaultPath2 = pickleLIBFilePath
print("DefaultPath .p file = " + defaultPath2)
try:
self.localLibrary = pickle.load( open( defaultPath2 , "rb" ) )
print("Loaded libDatabasePOOL.p")
except:
self.localLibrary = {}
pickle.dump( self.localLibrary , open( defaultPath2 , "wb" ) )
print("Recreated LibraryPOOL Save File")
print( "LocalLibary Size = " + str( len( self.localLibrary ) ) )
def getMyStations(self):
stations = self.api.get_all_stations()
for x in stations:
self.stations[x['id']] = x['name']
def printAvailableStations(self):
for x in self.stations:
print( str(x) + " = " + self.stations[x] )
def downloadStationToPOOL( self , stationID ):
rawPlaylist = self.api.get_station_tracks( stationID , 25 )
self.needToDownloadSongs = {}
for x in rawPlaylist:
if x['nid'] in self.localLibrary:
print("Already in LibraryPOOL")
else:
self.Full = False
print( str(x['nid']) + " == Not in library ... need to download" )
self.needToDownloadSongs[x['nid']] = { 'stationID': stationID , 'trackName': x['title'] , 'artistName': x['artist'] , 'albumID': x['albumId'] , 'artURL': x['albumArtRef'][0]['url'] }
p1 = threading.Thread( target=self.getMP3FromSongIDS , args=( stationID , ) )
p1.start()
p1.join()
if self.Full == False:
self.Full = True
print( "LocalLibary Size = " + str( len( self.localLibrary ) ) )
self.downloadStationToPOOL( stationID )
def getMP3FromSongIDS( self , stationID ):
a1 = 1
for x in self.needToDownloadSongs:
self.saveMP3ToLibraryPOOL( x , self.needToDownloadSongs[x]['trackName'] , self.needToDownloadSongs[x]['artistName'] , stationID )
self.localLibrary[x] = self.needToDownloadSongs[x]
pickle.dump( self.localLibrary , open( os.path.join( self.libDIR , "libDatabasePOOL.p" ) , "wb" ) )
print("added [" + str(a1) + " of " + str(len(self.needToDownloadSongs)) + "] songs to localLibrary")
a1 = a1 + 1
def saveMP3ToLibraryPOOL( self , songID , name , artist , stationID ):
albumName = self.stations[stationID]
wURL = self.api.get_stream_url( songID , self.api.android_id , 'hi' )
fN = os.path.join( self.libDIR , songID + ".mp3" )
response1 = requests.get( wURL , stream=True )
with open( fN , 'wb' ) as f:
for data in tqdm( response1.iter_content(chunk_size=524288) ):
f.write(data)
m3 = MP3( fN , ID3=EasyID3 )
m3.add_tags( ID3=EasyID3 )
m3["title"] = name
m3['artist'] = artist
m3['album'] = albumName
m3['organization'] = stationID
m3.save()
def extractSinglePlaylistFromPOOL( self , stationID , destinationDIR , onlyCopyNotExtract=False ):
if not os.path.exists(destinationDIR):
os.makedirs(destinationDIR)
if onlyCopyNotExtract == True:
for key , value in self.localLibrary.items():
try:
if value['stationID'] == stationID:
#print( "found --> " + self.localLibrary[key]['trackName'] + " in ... " + str(stationID) )
fN = str(key) + ".mp3"
shutil.copy( os.path.join( self.libDIR , fN ) , os.path.join( destinationDIR , fN ) )
except:
pass
else:
for key , value in self.localLibrary.items():
try:
if value['stationID'] == stationID:
#print( "found --> " + self.localLibrary[key]['trackName'] + " in ... " + str(stationID) )
fN = str(key) + ".mp3"
shutil.move( os.path.join( self.libDIR , fN ) , os.path.join( destinationDIR , fN ) )
except:
pass