Exemplo n.º 1
0
async def search_and_download(album, artist, format, size, out_filepath, *, size_tolerance_prct, amazon_tlds,
                              no_lq_sources, preserve_format=False):
  """ Search and download a cover, return True if success, False instead. """
  # register sources
  source_args = (size, size_tolerance_prct)
  cover_sources = [sources.LastFmCoverSource(*source_args),
                   sources.DeezerCoverSource(*source_args),
                   sources.AmazonCdCoverSource(*source_args),
                   sources.AmazonDigitalCoverSource(*source_args)]
  for tld in amazon_tlds:
    cover_sources.append(sources.AmazonCdCoverSource(*source_args, tld=tld))
  if not no_lq_sources:
    cover_sources.append(sources.GoogleImagesWebScrapeCoverSource(*source_args))

  # schedule search work
  search_futures = []
  for cover_source in cover_sources:
    coroutine = cover_source.search(album, artist)
    future = asyncio.ensure_future(coroutine)
    search_futures.append(future)

  # wait for it
  await asyncio.wait(search_futures)

  # get results
  results = []
  for future in search_futures:
    source_results = future.result()
    results.extend(source_results)

  # sort results
  results = await CoverSourceResult.preProcessForComparison(results, size, size_tolerance_prct)
  results.sort(reverse=True,
               key=functools.cmp_to_key(functools.partial(CoverSourceResult.compare,
                                                          target_size=size,
                                                          size_tolerance_prct=size_tolerance_prct)))
  if not results:
    logging.getLogger("Main").info("No results")

  # download
  done = False
  for result in results:
    try:
      await result.get(format, size, size_tolerance_prct, out_filepath, preserve_format=preserve_format)
    except Exception as e:
      logging.getLogger("Main").warning("Download of %s failed: %s %s" % (result,
                                                                          e.__class__.__qualname__,
                                                                          e))
      continue
    else:
      done = True
      break

  # cleanup sessions
  close_cr = []
  for cover_source in cover_sources:
    close_cr.append(cover_source.closeSession())
  await asyncio.gather(*close_cr)

  return done
Exemplo n.º 2
0
def search_and_download(album, artist, format, size, size_tolerance_prct,
                        amazon_tlds, no_lq_sources, out_filepath):
    """ Search and download a cover, return True if success, False instead. """
    # display warning if optipng or jpegoptim are missing
    if not HAS_JPEGOPTIM:
        logging.getLogger().warning(
            "jpegoptim could not be found, JPEG crunching will be disabled")
    if not HAS_OPTIPNG:
        logging.getLogger().warning(
            "optipng could not be found, PNG crunching will be disabled")

    # register sources
    source_args = (size, size_tolerance_prct)
    cover_sources = [
        sources.LastFmCoverSource(*source_args),
        sources.AmazonCdCoverSource(*source_args),
        sources.AmazonDigitalCoverSource(*source_args)
    ]
    for tld in amazon_tlds:
        cover_sources.append(sources.AmazonCdCoverSource(*source_args,
                                                         tld=tld))
    if not no_lq_sources:
        cover_sources.append(
            sources.GoogleImagesWebScrapeCoverSource(*source_args))

    # search
    results = []
    for cover_source in cover_sources:
        results.extend(cover_source.search(album, artist))

    # sort results
    results = CoverSourceResult.preProcessForComparison(
        results, size, size_tolerance_prct)
    results.sort(reverse=True,
                 key=functools.cmp_to_key(
                     functools.partial(
                         CoverSourceResult.compare,
                         target_size=size,
                         size_tolerance_prct=size_tolerance_prct)))
    if not results:
        logging.getLogger().info("No results")

    # download
    for result in results:
        try:
            result.get(format, size, size_tolerance_prct, out_filepath)
        except Exception as e:
            logging.getLogger().warning("Download of %s failed: %s %s" %
                                        (result, e.__class__.__qualname__, e))
            continue
        else:
            return True

    return False