Update itunes plugin
This commit is contained in:
@@ -94,8 +94,7 @@ class SearchAPIPlugin(BeetsPlugin):
|
|||||||
|
|
||||||
def scan_track_ids(self, session, task):
|
def scan_track_ids(self, session, task):
|
||||||
self._log.debug("Scanning track IDs")
|
self._log.debug("Scanning track IDs")
|
||||||
items = task.items if task.is_album else [ task.item ]
|
for item in task.items:
|
||||||
for item in items:
|
|
||||||
with open(item.path, 'rb') as file:
|
with open(item.path, 'rb') as file:
|
||||||
data = file.read(1000) # Song ID always seems to be around 600-700 bytes in
|
data = file.read(1000) # Song ID always seems to be around 600-700 bytes in
|
||||||
location = data.find(b'song')
|
location = data.find(b'song')
|
||||||
@@ -110,47 +109,45 @@ class SearchAPIPlugin(BeetsPlugin):
|
|||||||
"""Returns a list of AlbumInfo objects for iTunes search results
|
"""Returns a list of AlbumInfo objects for iTunes search results
|
||||||
matching an album and artist.
|
matching an album and artist.
|
||||||
"""
|
"""
|
||||||
candidates = []
|
try:
|
||||||
# Lookup the track ids assigned by iTunes Match
|
candidates = []
|
||||||
if self.method in ['lookup', 'lookup (search fallback)', 'both']:
|
# Lookup the track ids assigned by iTunes Match
|
||||||
candidates += self.lookup_albums(items)
|
if self.method in ['lookup', 'lookup (search fallback)', 'both']:
|
||||||
|
candidates += self.lookup_albums(items)
|
||||||
|
|
||||||
# If the lookup was not successful ignore the ID for distance evaluation
|
# If the lookup was not successful ignore the ID for distance evaluation
|
||||||
if self.method == 'lookup (search fallback)' and len(candidates) == 0:
|
if self.method == 'lookup (search fallback)' and len(candidates) == 0:
|
||||||
for item in items:
|
for item in items:
|
||||||
_track_ids.pop(item, None)
|
_track_ids.pop(item, None)
|
||||||
|
|
||||||
if self.method in ['search', 'both'] or (self.method == 'lookup (search fallback)' and len(candidates) == 0):
|
if self.method in ['search', 'both'] or (self.method == 'lookup (search fallback)' and len(candidates) == 0):
|
||||||
candidates += self.search_albums(artist, album, va_likely)
|
candidates += self.search_albums(artist, album, va_likely)
|
||||||
return candidates
|
return candidates
|
||||||
|
except ConnectionError:
|
||||||
|
self._log.debug(u'Cannot search iTunes (no network connection)')
|
||||||
|
return []
|
||||||
|
|
||||||
def lookup_albums(self, items):
|
def lookup_albums(self, items):
|
||||||
ids = [ str(_track_ids[item]) for item in items if item in _track_ids ]
|
ids = [ str(_track_ids[item]) for item in items if item in _track_ids ]
|
||||||
def remove_found_tracks(album):
|
def remove_found_tracks(album):
|
||||||
for track in album.get_tracks():
|
for track in album.get_tracks():
|
||||||
try:
|
if track.track_id in ids:
|
||||||
ids.remove(track.track_id)
|
ids.remove(track.track_id)
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
albums = []
|
albums = []
|
||||||
try:
|
for country in self.lookup_countries:
|
||||||
for country in self.lookup_countries:
|
self._log.debug("Searching iTunes Store: {0}", country)
|
||||||
self._log.debug("Searching iTunes Store: {0}", country)
|
try:
|
||||||
try:
|
results = itunespy.lookup_album(','.join(ids), country=country)
|
||||||
results = itunespy.lookup_album(','.join(ids), country=country)
|
album_results = [ album for album in results if isinstance(album, itunespy.music_album.MusicAlbum) ]
|
||||||
album_results = [ album for album in results if isinstance(album, itunespy.music_album.MusicAlbum) ]
|
albums += [ self.make_album_info(album, True) for album in album_results ]
|
||||||
albums += [ self.make_album_info(album, True) for album in album_results ]
|
|
||||||
|
|
||||||
# Remove IDs that have been found. If an album has been found for all tracks stop searching.
|
# Remove IDs that have been found. If an album has been found for all tracks stop searching.
|
||||||
for album in album_results:
|
for album in album_results:
|
||||||
remove_found_tracks(album)
|
remove_found_tracks(album)
|
||||||
if not ids:
|
if not ids:
|
||||||
break
|
break
|
||||||
except LookupError:
|
except LookupError:
|
||||||
pass
|
pass
|
||||||
except ConnectionError:
|
|
||||||
self._log.debug(u'Cannot search iTunes (no network connection)')
|
|
||||||
return []
|
|
||||||
return albums
|
return albums
|
||||||
|
|
||||||
def search_albums(self, artist, album, va_likely):
|
def search_albums(self, artist, album, va_likely):
|
||||||
@@ -167,9 +164,6 @@ class SearchAPIPlugin(BeetsPlugin):
|
|||||||
try:
|
try:
|
||||||
results = itunespy.search_album(query, country=country, limit=self.search_limit)
|
results = itunespy.search_album(query, country=country, limit=self.search_limit)
|
||||||
albums += [self.make_album_info(result) for result in results]
|
albums += [self.make_album_info(result) for result in results]
|
||||||
except ConnectionError:
|
|
||||||
self._log.debug(u'Cannot search iTunes (no network connection)')
|
|
||||||
return []
|
|
||||||
except LookupError:
|
except LookupError:
|
||||||
self._log.debug(u'Search for {0} did return no results', query)
|
self._log.debug(u'Search for {0} did return no results', query)
|
||||||
return []
|
return []
|
||||||
|
|||||||
Reference in New Issue
Block a user