1

Add Search Prompt

This commit is contained in:
Kim Wittenburg
2018-09-04 00:00:33 +02:00
parent 474f3da3a2
commit 2dc716622e

View File

@@ -7,11 +7,13 @@ import datetime
import urllib.request import urllib.request
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
import beets.mediafile as mediafile
from beets import config from beets import config
from beets.autotag.hooks import AlbumInfo, TrackInfo, Distance from beets.autotag.hooks import AlbumInfo, TrackInfo, Distance
from beets.autotag.match import tag_album
from beets.plugins import BeetsPlugin, sanitize_choices from beets.plugins import BeetsPlugin, sanitize_choices
from beets.dbcore import types from beets.dbcore import types
import beets.mediafile as mediafile from beets.ui.commands import PromptChoice
AVAILABLE_COUNTRIES = ['DE', 'US', 'GB', 'FR', 'AU', 'CA', 'IT', 'JP', 'DZ', 'AO', 'AI', 'AG', 'AR', 'AM', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BM', 'BO', 'BW', 'BR', 'BN', 'BG', 'CM', 'KY', 'CL', 'CN', 'CO', 'CR', 'CI', 'HR', 'CY', 'CZ', 'DK', 'DM', 'DO', 'EC', 'EG', 'SV', 'EE', 'ET', 'FI', 'GH', 'GR', 'GD', 'GT', 'GY', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IE', 'IL', 'JM', 'JO', 'KZ', 'KE', 'KR', 'KW', 'LV', 'LB', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MY', 'MV', 'ML', 'MT', 'MU', 'MX', 'MD', 'MS', 'MM', 'NP', 'NL', 'NZ', 'NI', 'NE', 'NG', 'NO', 'OM', 'PK', 'PA', 'PY', 'PE', 'PH', 'PL', 'PT', 'QA', 'RO', 'RU', 'KN', 'LC', 'VC', 'SA', 'SN', 'RS', 'SG', 'SK', 'SI', 'ZA', 'ES', 'LK', 'SR', 'SE', 'CH', 'TW', 'TZ', 'TH', 'TT', 'TN', 'TR', 'TC', 'UG', 'UA', 'AE', 'UY', 'UZ', 'VE', 'VN', 'VG', 'YE'] AVAILABLE_COUNTRIES = ['DE', 'US', 'GB', 'FR', 'AU', 'CA', 'IT', 'JP', 'DZ', 'AO', 'AI', 'AG', 'AR', 'AM', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BM', 'BO', 'BW', 'BR', 'BN', 'BG', 'CM', 'KY', 'CL', 'CN', 'CO', 'CR', 'CI', 'HR', 'CY', 'CZ', 'DK', 'DM', 'DO', 'EC', 'EG', 'SV', 'EE', 'ET', 'FI', 'GH', 'GR', 'GD', 'GT', 'GY', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IE', 'IL', 'JM', 'JO', 'KZ', 'KE', 'KR', 'KW', 'LV', 'LB', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MY', 'MV', 'ML', 'MT', 'MU', 'MX', 'MD', 'MS', 'MM', 'NP', 'NL', 'NZ', 'NI', 'NE', 'NG', 'NO', 'OM', 'PK', 'PA', 'PY', 'PE', 'PH', 'PL', 'PT', 'QA', 'RO', 'RU', 'KN', 'LC', 'VC', 'SA', 'SN', 'RS', 'SG', 'SK', 'SI', 'ZA', 'ES', 'LK', 'SR', 'SE', 'CH', 'TW', 'TZ', 'TH', 'TT', 'TN', 'TR', 'TC', 'UG', 'UA', 'AE', 'UY', 'UZ', 'VE', 'VN', 'VG', 'YE']
VA_ARTIST_ID = 120644327 VA_ARTIST_ID = 120644327
@@ -73,8 +75,11 @@ class iTunesPlugin(BeetsPlugin):
'artwork': False, # Whether to also download artwork from iTunes. Does not work well with fetchart 'artwork': False, # Whether to also download artwork from iTunes. Does not work well with fetchart
'method': 'lookup', # Valid values: 'search', 'lookup', 'both', 'lookup (search fallback)' 'method': 'lookup', # Valid values: 'search', 'lookup', 'both', 'lookup (search fallback)'
}) })
self.force_search = False
if self.method in ['lookup', 'lookup (search fallback)', 'both']: if self.method in ['lookup', 'lookup (search fallback)', 'both']:
self.register_listener('import_task_start', self.scan_track_ids) self.register_listener('import_task_start', self.scan_track_ids)
if self.method in ['lookup', 'lookup (search fallback)']:
self.register_listener('before_choose_candidate', self.add_prompt_choices)
self.register_listener('import_task_apply', self.apply_itunes_metadata) self.register_listener('import_task_apply', self.apply_itunes_metadata)
if self.use_itunes_artwork: if self.use_itunes_artwork:
self.import_stages += [ self.fetch_artwork ] self.import_stages += [ self.fetch_artwork ]
@@ -119,6 +124,8 @@ class iTunesPlugin(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.
""" """
force_search = self.force_search
self.force_search = False
try: try:
candidates = [] candidates = []
# Lookup the track ids assigned by iTunes Match # Lookup the track ids assigned by iTunes Match
@@ -130,7 +137,7 @@ class iTunesPlugin(BeetsPlugin):
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 force_search or 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: except ConnectionError:
@@ -319,6 +326,14 @@ class iTunesPlugin(BeetsPlugin):
info.itunes_advisory = decode_explicitness(track.track_explicitness) info.itunes_advisory = decode_explicitness(track.track_explicitness)
info.itunes_artwork_url = decode_artwork_url(track.artwork_url_100) info.itunes_artwork_url = decode_artwork_url(track.artwork_url_100)
return info return info
def add_prompt_choices(self, session, task):
return [PromptChoice('c', 'searCh iTunes', self.prompt_search)]
def prompt_search(self, session, task):
self.force_search = True
_, _, proposal = tag_album(task.items)
return proposal
def decode_explicitness(advisory_string): def decode_explicitness(advisory_string):
return ['notExplicit', 'explicit', 'cleaned'].index(advisory_string) return ['notExplicit', 'explicit', 'cleaned'].index(advisory_string)