Skip to main content

pySauceNao is an unofficial library for the SauceNao API.

Project description

pySauceNao

pySauceNao is an unofficial asynchronous library for the SauceNao API. It supports lookups via URL or from the local filesystem.

Installation

This library requires Python 3.9 or above.

You can install the library through pip as follows,

pip install pysaucenao

Usage

from pysaucenao import SauceNao
sauce = SauceNao(self, *, api_key: Optional[str] = None,
                 db_mask: Optional[int] = None,
                 db_mask_disable: Optional[int] = None,
                 db: int = 999,
                 results_limit: int = 6,
                 min_similarity: float = 50.0,
                 test_mode: int = 0,
                 strict_mode: bool = True,
                 loop: Optional[asyncio.AbstractEventLoop] = None)

# results = await sauce.from_file('/path/to/image.jpg')
results = await sauce.from_url('https://www.example.com/image.jpg')
repr(results)
<SauceNaoResults(count=2, short_avail=3, long_avail=87, results=[<GenericSourc...e='Danbooru')>])>

The library attempts to provide a developer friendly container format for all results. Meaning, no matter if SauceNao returns a Pixiv source result or a more obscure source, you'll be able to easily pull the title, author URL and other useful information.

from pysaucenao import SauceNao, PixivSource
sauce = SauceNao()
results = await sauce.from_url('https://www.example.com/image.jpg')

len(results)  # 4

# Find out how many API request limits you have remaining after a search query
results.short_remaining  # 3 (per 30 seconds limit)
results.long_remaining   # 86 (per day limit)

# You can determine whether the search result is a Pixiv, Booru, Video or Other/Generic result by the type property or type checking
results[0].type == 'pixiv'
isinstance(results[0], PixivSource)

results[0].similarity     # The similarity percentage
results[0].thumbnail      # Returns a temporary signed URL; not suitable for permanent hotlinking
results[0].title          # The original title of the image
results[0].author_name    # The authors name
results[0].author_url     # URL to the profile page of the sourced artist
results[0].url            # URL to sourced image
results[0].source_url     # Same as url for Pixiv results, but returns the linked original source URL for Booru entries

Video search results provide three additional properties containing the episode number, estimated timestamp, and release year

from pysaucenao import SauceNao, VideoSource
sauce = SauceNao()
results = await sauce.from_url('https://www.example.com/image.jpg')

if isinstance(results[0], VideoSource):
    results[0].episode    # The episode number
    results[0].year       # The release year
    results[0].timestamp  # The approximate timestamp of the scene (e.g. '00:07:53 / 00:23:40')

MangaSource search results, similarly, provide an additional chapter property.

Advanced usage

Additional source URL's

Thanks to yuna.moe, pysaucenao is no longer limited to just AniDB source URL's for anime results as of v1.3

To utilize this new feature, you should first verify you are working with an Anime source.

Once you have done that, you will need to preload the ID map by running the load_ids method.

If you attempt to access any of the additional AnimeSource properties without first doing this, you will get an IndexError.

from pysaucenao import SauceNao, AnimeSource
sauce = SauceNao()
results = await sauce.from_url('https://www.example.com/image.jpg')

if isinstance(results[0], AnimeSource):
    await results[0].load_ids()

This will map the AniDB ID SauceNao returns with several other anime databases,

results[0].title        # Fullmetal Alchemist: Brotherhood
results[0].anilist_id   # 5114
results[0].anilist_url  # https://anilist.co/anime/5114
results[0].mal_url      # https://myanimelist.net/anime/5114
results[0].kitsu_url    # https://kitsu.io/anime/3936

Priority

If you want to prioritize certain types of results, you can do so using the priority setting as of v1.2

The most useful case for this is to prioritize anime results, preventing anime screencaps hosted on DeviantArt and other indexes some taking priority.

To use this in your own code, just initialize SauceNao like so,

sauce = SauceNao(priority=[21, 22])

As long as the anime search results are reasonably close to the next best match SauceNao returns, this will make sure the library always returns the anime result first, and ideally never a reposted screen-grab.

Registering for an API key

If you are performing lots of API queries, you will eventually need to sign up and register for an API key (and possibly upgrade your account for very large request volumes)

You can register for an account on SauceNAO's website

Error handling

The SauceNao class will throw an exception if any of the following occur:

  • You have exceeded your 30-second search query limit (ShortLimitReachedException)
  • You have exceeded your 24-hour search query limit (DailyLimitReachedException)
  • You attempted to upload a file larger than SauceNAO allows (FileSizeLimitException)
  • You provided an invalid API key (InvalidOrWrongApiKeyException)
  • The image was too small for use (ImageSizeException)
  • Either the URL or file provided was not a valid image (InvalidImageException)
  • Too many failed requests made; try again later (TooManyFailedRequestsException)
  • Your account does not have API access; contact SauceNao support (BannedException)
  • Any other unknown error occurred / service may be down (UnknownStatusCodeException)

All of these exceptions extend a base SauceNaoException class for easy catching and handling.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysaucenao-1.6.3.tar.gz (61.7 kB view details)

Uploaded Source

Built Distribution

pysaucenao-1.6.3-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file pysaucenao-1.6.3.tar.gz.

File metadata

  • Download URL: pysaucenao-1.6.3.tar.gz
  • Upload date:
  • Size: 61.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for pysaucenao-1.6.3.tar.gz
Algorithm Hash digest
SHA256 549bf8a50a0e286da9fb5ae8f32a3f43bfc86e55effcc803316f1f490fe4b2e1
MD5 3f00dc6ae292ae08a8470344a180ef61
BLAKE2b-256 08102f21487dca92b5e5837f70b098af5df7eddf4950aaeedb492c9cc0ace47f

See more details on using hashes here.

File details

Details for the file pysaucenao-1.6.3-py3-none-any.whl.

File metadata

  • Download URL: pysaucenao-1.6.3-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for pysaucenao-1.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1f56f7edac91f95ed32f1555429d3cac1265f9d708aebdcfd76e0f12eed8de1c
MD5 515d7eddd94e1fcfbecc06550b4b004d
BLAKE2b-256 025bf2adcaa18ff207a1d928c3aaeb39e80d782a46bafa2f7e755ea31bc33918

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page