|
20 | 20 | from beets import autotag, library, ui, util
|
21 | 21 | from beets.autotag import hooks
|
22 | 22 | from beets import config
|
| 23 | +from collections import defaultdict |
23 | 24 |
|
24 | 25 | log = logging.getLogger('beets')
|
25 | 26 |
|
@@ -64,30 +65,27 @@ def mbsync_albums(lib, query, move, pretend, write):
|
64 | 65 | log.info(u'Release ID not found: {0}'.format(a.mb_albumid))
|
65 | 66 | continue
|
66 | 67 |
|
67 |
| - # Construct an hash mapping recording MBIDs to their information. A |
68 |
| - # release can have recording MBIDs that appear multiple times in the |
69 |
| - # same release. |
70 |
| - track_index = {} |
| 68 | + # Map recording MBIDs to their information. Recordings can appear |
| 69 | + # multiple times on a release, so each MBID maps to a list of TrackInfo |
| 70 | + # objects. |
| 71 | + track_index = defaultdict(list) |
71 | 72 | for track_info in album_info.tracks:
|
72 |
| - if track_info.track_id in track_index: |
73 |
| - track_index[track_info.track_id].append(track_info) |
74 |
| - else: |
75 |
| - track_index[track_info.track_id] = [track_info] |
| 73 | + track_index[track_info.track_id].append(track_info) |
76 | 74 |
|
77 | 75 | # Construct a track mapping according to MBIDs. This should work
|
78 |
| - # for albums that have missing or extra tracks. If a mapping is |
79 |
| - # ambiguous, the items' disc and track number need to match in order |
80 |
| - # for an item to be mapped. |
| 76 | + # for albums that have missing or extra tracks. If there are multiple |
| 77 | + # copies of a recording, they are disambiguated using their disc and |
| 78 | + # track number. |
81 | 79 | mapping = {}
|
82 | 80 | for item in items:
|
83 |
| - candidates = track_index.get(item.mb_trackid, []) |
| 81 | + candidates = track_index[item.mb_trackid] |
84 | 82 | if len(candidates) == 1:
|
85 | 83 | mapping[item] = candidates[0]
|
86 |
| - continue |
87 |
| - for c in candidates: |
88 |
| - if c.medium_index == item.track and c.medium == item.disc: |
89 |
| - mapping[item] = c |
90 |
| - break |
| 84 | + else: |
| 85 | + for c in candidates: |
| 86 | + if c.medium_index == item.track and c.medium == item.disc: |
| 87 | + mapping[item] = c |
| 88 | + break |
91 | 89 |
|
92 | 90 | # Apply.
|
93 | 91 | with lib.transaction():
|
|
0 commit comments