示例#1
0
def _find_item_in_lib(lib, track_name, artist_name):
    """Finds an Item in the library based on the track_name.

    The track_name is not guaranteed to be perfect (i.e. as soon on MB),
    so in that case we query MB and look for the track id and query our
    lib with that.
    """
    # Query the library based on the track name
    query = MatchQuery('title', track_name)
    lib_results = lib._fetch(Item, query=query)

    # Maybe the provided track name isn't all too good
    # Search for the track on MusicBrainz, and use that info to retry our lib
    if not lib_results:
        mb_candidate = _get_mb_candidate(track_name, artist_name)
        if mb_candidate:
            query = OrQuery((
                        AndQuery((
                            MatchQuery('title', mb_candidate.title),
                            MatchQuery('artist', mb_candidate.artist),
                        )),
                        MatchQuery('mb_trackid', mb_candidate.track_id)
                    ))
            lib_results = lib._fetch(Item, query=query)

    if not lib_results:
        return None

    # If we get multiple Item results from our library, choose best match
    # using the distance
    if len(lib_results) > 1:
        return _get_best_match(lib_results, track_name, artist_name)[0]

    return lib_results[0]
示例#2
0
    def retrieve_library_items(self):
        cmd_query = self.query
        parsed_cmd_query, parsed_ordering = parse_query_parts(cmd_query, Item)
        parsed_ordering = FixedFieldSort("albumartist", ascending=True)

        if self.cfg_force:
            full_query = parsed_cmd_query
        else:
            parsed_plg_query = OrQuery([
                RegexpQuery('genre', '^$'),
                RegexpQuery('genre', '[/,]'),
            ])
            full_query = AndQuery([parsed_cmd_query, parsed_plg_query])

        self._say("Selection query: {}".format(full_query))

        return self.lib.items(full_query, parsed_ordering)
示例#3
0
 def translate_filters(self):
     """Translate filters from request arguments to a beets Query."""
     # The format of each filter key in the request parameter is:
     # filter[<attribute>]. This regex extracts <attribute>.
     pattern = re.compile(r"filter\[(?P<attribute>[a-zA-Z0-9_-]+)\]")
     queries = []
     for key, value in request.args.items():
         match = pattern.match(key)
         if match:
             # Extract attribute name from key
             aura_attr = match.group("attribute")
             # Get the beets version of the attribute name
             beets_attr = self.attribute_map.get(aura_attr, aura_attr)
             converter = self.get_attribute_converter(beets_attr)
             value = converter(value)
             # Add exact match query to list
             # Use a slow query so it works with all fields
             queries.append(MatchQuery(beets_attr, value, fast=False))
     # NOTE: AURA doesn't officially support multiple queries
     return AndQuery(queries)
示例#4
0
    def retrieve_library_items(self):
        cmd_query = self.query
        parsed_cmd_query, parsed_ordering = parse_query_parts(cmd_query, Item)

        if self.cfg_force:
            full_query = parsed_cmd_query
        else:
            parsed_plg_query = OrQuery([
                NumericQuery('year', '0'),
                MatchQuery('year', ''),
                NoneQuery('year'),
                NumericQuery('original_year', '0'),
                MatchQuery('original_year', ''),
                NoneQuery('original_year'),
            ])
            full_query = AndQuery([parsed_cmd_query, parsed_plg_query])

        self._say("Selection query: {}".format(full_query))

        return self.lib.items(full_query, parsed_ordering)