<commit_before>import collections
import json
import operator

from sacad.cover import CoverImageFormat, CoverImageMetadata, CoverSourceQuality, CoverSourceResult
from sacad.sources.base import CoverSource


class DeezerCoverSourceResult(CoverSourceResult):

  def __init__(self, *args, **kwargs):
    super().__init__(*args, source_quality=CoverSourceQuality.REFERENCE, **kwargs)


class DeezerCoverSource(CoverSource):

  """
  Cover source using the official Deezer API.

  https://developers.deezer.com/api/
  """

  BASE_URL = "https://api.deezer.com/search"
  COVER_SIZES = {"cover_small": (56, 56),
                 "cover": (120, 120),
                 "cover_medium": (250, 250),
                 "cover_big": (500, 500),
                 "cover_xl": (1000, 1000)}

  def __init__(self, *args, **kwargs):
    super().__init__(*args, min_delay_between_accesses=0.1, **kwargs)

  def getSearchUrl(self, album, artist):
    """ See CoverSource.getSearchUrl. """
    # build request url
    search_params = collections.OrderedDict()
    search_params["artist"] = artist
    search_params["album"] = album
    url_params = collections.OrderedDict()
    #url_params["strict"] = "on"
    url_params["order"] = "RANKING"
    url_params["q"] = " ".join("%s:\"%s\"" % (k, v) for k, v in search_params.items())
    return __class__.assembleUrl(__class__.BASE_URL, url_params)

  def processQueryString(self, s):
    """ See CoverSource.processQueryString. """
    # API search is fuzzy, not need to alter query
    return s

  async def parseResults(self, api_data):
    """ See CoverSource.parseResults. """
    results = []

    # get unique albums
    json_data = json.loads(api_data)
    albums = []
    for e in json_data["data"]:
      album = e["album"]
      album_id = album["id"]
      if album_id in map(operator.itemgetter("id"), albums):
        continue
      albums.append(album)

    for album in albums:
      for key, size in __class__.COVER_SIZES.items():
        img_url = album[key]
        thumbnail_url = album["cover_small"]
        results.append(DeezerCoverSourceResult(img_url,
                                               size,
                                               CoverImageFormat.JPEG,
                                               thumbnail_url=thumbnail_url,
                                               source=self,
                                               check_metadata=CoverImageMetadata.NONE))

    return results<commit_msg>Deezer: Add ranking<commit_after>import collections
import json
import operator

from sacad.cover import CoverImageFormat, CoverImageMetadata, CoverSourceQuality, CoverSourceResult
from sacad.sources.base import CoverSource


class DeezerCoverSourceResult(CoverSourceResult):

  def __init__(self, *args, **kwargs):
    super().__init__(*args, source_quality=CoverSourceQuality.REFERENCE, **kwargs)


class DeezerCoverSource(CoverSource):

  """
  Cover source using the official Deezer API.

  https://developers.deezer.com/api/
  """

  BASE_URL = "https://api.deezer.com/search"
  COVER_SIZES = {"cover_small": (56, 56),
                 "cover": (120, 120),
                 "cover_medium": (250, 250),
                 "cover_big": (500, 500),
                 "cover_xl": (1000, 1000)}

  def __init__(self, *args, **kwargs):
    super().__init__(*args, min_delay_between_accesses=0.1, **kwargs)

  def getSearchUrl(self, album, artist):
    """ See CoverSource.getSearchUrl. """
    # build request url
    search_params = collections.OrderedDict()
    search_params["artist"] = artist
    search_params["album"] = album
    url_params = collections.OrderedDict()
    #url_params["strict"] = "on"
    url_params["order"] = "RANKING"
    url_params["q"] = " ".join("%s:\"%s\"" % (k, v) for k, v in search_params.items())
    return __class__.assembleUrl(__class__.BASE_URL, url_params)

  def processQueryString(self, s):
    """ See CoverSource.processQueryString. """
    # API search is fuzzy, not need to alter query
    return s

  async def parseResults(self, api_data):
    """ See CoverSource.parseResults. """
    results = []

    # get unique albums
    json_data = json.loads(api_data)
    albums = []
    for e in json_data["data"]:
      album = e["album"]
      album_id = album["id"]
      if album_id in map(operator.itemgetter("id"), albums):
        continue
      albums.append(album)

    for rank, album in enumerate(albums, 1):
      for key, size in __class__.COVER_SIZES.items():
        img_url = album[key]
        thumbnail_url = album["cover_small"]
        results.append(DeezerCoverSourceResult(img_url,
                                               size,
                                               CoverImageFormat.JPEG,
                                               thumbnail_url=thumbnail_url,
                                               source=self,
                                               rank=rank,
                                               check_metadata=CoverImageMetadata.NONE))

    return results
{'fraction_non_alphanumeric': 0.11553705905667856, 'fraction_numerical': 0.012881490289338089, 'mean_word_length': 2.357950765136394, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3065103', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1401, 'n_words': 322}