forked from lloeki/py-skyjam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
download.py
70 lines (53 loc) · 1.94 KB
/
download.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
import sys
import os
import logging
from gmusicapi.clients import Musicmanager
logging.basicConfig(filename='dl.log', level=logging.DEBUG)
def fix(path):
# OSX: : -> FULLWIDTH COLON (U+FF1A)
# OSX: / -> : (translated as / in Cocoa)
# LINUX: / -> DIVISION SLASH (U+2215)
path.replace(':', "\uFF1A")
path.replace('/', ':')
# path.replace('/', "\u2215")
return path
if os.path.exists('oauth.cred'):
pass
else:
Musicmanager.perform_oauth('oauth.cred', True)
manager = Musicmanager()
manager.login('oauth.cred')
columns = int(subprocess.check_output(['stty', 'size']).split()[1])
logging.info('starting download')
count = 0
for songs in manager.get_uploaded_songs(True):
for song in songs:
count = count + 1
dartist = 'dl/%s' % fix(song['album_artist'] or song['artist'])
if not os.path.exists(dartist):
os.mkdir(dartist)
dalbum = '%s/%s' % (dartist, fix(song['album']))
if not os.path.exists(dalbum):
os.mkdir(dalbum)
fsong = '%s/%02d - %s.mp3' % (dalbum,
song['track_number'],
fix(song['title']))
sys.stdout.write('\r' + (' ' * (columns-1)) + '\r')
sys.stdout.write(("%05d %s" % (count, fsong))[0:columns-1])
sys.stdout.flush()
if not os.path.exists(fsong):
audio = None
try:
filename, audio = manager.download_song(song['id'])
logging.info('download success: "%s"' % fsong)
except KeyboardInterrupt:
sys.stdout.write('\n')
sys.exit(1) # move this up
except Exception, e:
logging.error('download failed: "%s" %s' % (fsong, e))
else:
with open(fsong, 'wb') as f:
f.write(audio)
else:
logging.info('download skipped: "%s"' % fsong)
sys.stdout.write('\n')