-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrdio-export.py
More file actions
executable file
·121 lines (104 loc) · 4.42 KB
/
rdio-export.py
File metadata and controls
executable file
·121 lines (104 loc) · 4.42 KB
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
#!/usr/bin/env python
# Copyright 2015 Ian McKellar <http://ian.mckellar.org/>
#
# This file is part of Rdio Export.
#
# Rdio Export is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rdio Export is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rdio Export. If not, see <http://www.gnu.org/licenses/>.
import argparse
import getpass
import re
import sys
from match import match_tracks
from playmusic import PlayMusic
from rdio import Rdio
from report import Report
parser = argparse.ArgumentParser(description='Export playlists and favorites from Rdio to Play Music')
parser.add_argument('rdio_username', help='username on Rdio')
parser.add_argument('google_email', help='Google account email address')
args = parser.parse_args()
google_password = getpass.getpass('%s password:' % args.google_email)
print 'Connecting to Rdio...'
rdio = Rdio()
print 'Connecting to Play Music...'
pm = PlayMusic(args.google_email, google_password)
if not pm.is_authenticated():
print 'Google login failed.'
sys.exit(1)
def migrate_playlist(user_key, playlist):
print u'\nMigrating playlist: %s' % playlist['name']
name = playlist['name']
if playlist['ownerKey'] != user_key:
name += u' (by %s)' % playlist['owner']
description = u'Imported from Rdio playlist %s\n' % playlist['shortUrl']
play_track_ids = []
failed = []
tracks = rdio.playlist_tracks(playlist)
if tracks is None:
print u'Can\'t get tracks for playlist "%s" - it might be private.' % playlist['name']
return
with Report('playlist-%s.html' % playlist['key'], name) as report:
for match in match_tracks(tracks, pm):
report.add_match(match)
if match.matched():
play_track_ids.append(match.play.id)
else:
failed.append(match)
if failed:
description += 'Failed to import: '
for match in failed:
description += u'%s (%s / %s / %s)' % (
match.rdio.url, match.rdio.name, match.rdio.artist, match.rdio.album)
playlist_id = pm.create_playlist(name, description, play_track_ids)
print u'Imported to %s' % pm.playlist_url(playlist_id)
def migrate_playlists(user):
print 'Finding playlists...'
user_key = user['key']
playlists = {}
for kind in ('owned', 'collab', 'favorites', 'subscribed'):
start = 0
while True:
pls = rdio.call('getUserPlaylists', user=user_key, kind=kind, extras='tracks,ownerKey', start=start)
if not pls:
break
start += len(pls)
for pl in pls:
playlists[pl['key']] = pl
# Find already imported playlists's Rdio URL.
imported = frozenset([m.groups()[0] for m in
[re.match(r'Imported from Rdio playlist (http://rd.io/x/.*?/)', p['description'])
for p in pm.get_all_playlists() if 'description' in p]
if m])
for playlist in playlists.values():
if playlist['shortUrl'] in imported:
print('Already imported playlist %s. Delete it from Play Music if you want to re-import it.' %
playlist['name'])
continue
migrate_playlist(user_key, playlist)
def migrate_favorites(user):
print 'Migrating favorites for %(firstName)s %(lastName)s' % user
print 'Scanning Rdio favorite...'
rdio_tracks = list(rdio.favorite_tracks(user['key']))
print 'Migrating %d favorites' % len(rdio_tracks)
with Report('favorites.html', "%(firstName)s %(lastName)s's Favorites" % user) as report:
matched_tracks = match_tracks(rdio_tracks, pm)
for match in matched_tracks:
report.add_match(match)
if match.matched():
pm.add_track(match.play.id)
if '@' in args.rdio_username:
rdio_user = rdio.call('findUser', email=args.rdio_username)
else:
rdio_user = rdio.call('findUser', vanityName=args.rdio_username)
migrate_playlists(rdio_user)
migrate_favorites(rdio_user)