コンテンツにスキップ

Yandex

Classes

Yandex

Bases: BaseSearchEngine[YandexResponse]

API client for the Yandex reverse image search engine.

This class provides an interface to perform reverse image searches using Yandex's service. It supports searching by both image URL and local image file upload.

Attributes:

Name Type Description
base_url str

The base URL for Yandex image search service.

Note
  • The service might be affected by regional restrictions.
  • Search results may vary based on the user's location and Yandex's algorithms.
Source code in PicImageSearch/engines/yandex.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Yandex(BaseSearchEngine[YandexResponse]):
    """API client for the Yandex reverse image search engine.

    This class provides an interface to perform reverse image searches using Yandex's service.
    It supports searching by both image URL and local image file upload.

    Attributes:
        base_url (str): The base URL for Yandex image search service.

    Note:
        - The service might be affected by regional restrictions.
        - Search results may vary based on the user's location and Yandex's algorithms.
    """

    def __init__(
        self,
        base_url: str = "https://yandex.com",
        **request_kwargs: Any,
    ):
        """Initializes a Yandex API client with specified configurations.

        Args:
            base_url (str): The base URL for Yandex searches.
            **request_kwargs (Any): Additional arguments for network requests.
        """
        base_url = f"{base_url}/images/search"
        super().__init__(base_url, **request_kwargs)

    async def search(
        self,
        url: Optional[str] = None,
        file: Union[str, bytes, Path, None] = None,
        **kwargs: Any,
    ) -> YandexResponse:
        """Performs a reverse image search on Yandex.

        This method supports two ways of searching:
            1. Search by image URL
            2. Search by uploading a local image file

        Args:
            url (Optional[str]): URL of the image to search.
            file (Union[str, bytes, Path, None]): Local image file, can be a path string, bytes data, or Path object.
            **kwargs (Any): Additional arguments passed to the parent class.

        Returns:
            YandexResponse: An object containing:
                - Search results and metadata
                - The final search URL used by Yandex

        Raises:
            ValueError: If neither `url` nor `file` is provided.

        Note:
            - Only one of `url` or `file` should be provided.
            - When using file upload, the image will be sent to Yandex's servers.
            - The search process involves standard Yandex parameters like `rpt` and `cbir_page`.
        """
        self._validate_args(url, file)

        params = {"rpt": "imageview", "cbir_page": "sites"}

        if url:
            params["url"] = url
            resp = await self._make_request(method="get", params=params)
        elif file:
            files = {"upfile": read_file(file)}
            resp = await self._make_request(
                method="post",
                params=params,
                data={"prg": 1},
                files=files,
            )

        return YandexResponse(resp.text, resp.url)

Functions

__init__(base_url='https://yandex.com', **request_kwargs)

Initializes a Yandex API client with specified configurations.

Parameters:

Name Type Description Default
base_url str

The base URL for Yandex searches.

'https://yandex.com'
**request_kwargs Any

Additional arguments for network requests.

{}
Source code in PicImageSearch/engines/yandex.py
23
24
25
26
27
28
29
30
31
32
33
34
35
def __init__(
    self,
    base_url: str = "https://yandex.com",
    **request_kwargs: Any,
):
    """Initializes a Yandex API client with specified configurations.

    Args:
        base_url (str): The base URL for Yandex searches.
        **request_kwargs (Any): Additional arguments for network requests.
    """
    base_url = f"{base_url}/images/search"
    super().__init__(base_url, **request_kwargs)
search(url=None, file=None, **kwargs) async

Performs a reverse image search on Yandex.

This method supports two ways of searching
  1. Search by image URL
  2. Search by uploading a local image file

Parameters:

Name Type Description Default
url Optional[str]

URL of the image to search.

None
file Union[str, bytes, Path, None]

Local image file, can be a path string, bytes data, or Path object.

None
**kwargs Any

Additional arguments passed to the parent class.

{}

Returns:

Name Type Description
YandexResponse YandexResponse

An object containing: - Search results and metadata - The final search URL used by Yandex

Raises:

Type Description
ValueError

If neither url nor file is provided.

Note
  • Only one of url or file should be provided.
  • When using file upload, the image will be sent to Yandex's servers.
  • The search process involves standard Yandex parameters like rpt and cbir_page.
Source code in PicImageSearch/engines/yandex.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
async def search(
    self,
    url: Optional[str] = None,
    file: Union[str, bytes, Path, None] = None,
    **kwargs: Any,
) -> YandexResponse:
    """Performs a reverse image search on Yandex.

    This method supports two ways of searching:
        1. Search by image URL
        2. Search by uploading a local image file

    Args:
        url (Optional[str]): URL of the image to search.
        file (Union[str, bytes, Path, None]): Local image file, can be a path string, bytes data, or Path object.
        **kwargs (Any): Additional arguments passed to the parent class.

    Returns:
        YandexResponse: An object containing:
            - Search results and metadata
            - The final search URL used by Yandex

    Raises:
        ValueError: If neither `url` nor `file` is provided.

    Note:
        - Only one of `url` or `file` should be provided.
        - When using file upload, the image will be sent to Yandex's servers.
        - The search process involves standard Yandex parameters like `rpt` and `cbir_page`.
    """
    self._validate_args(url, file)

    params = {"rpt": "imageview", "cbir_page": "sites"}

    if url:
        params["url"] = url
        resp = await self._make_request(method="get", params=params)
    elif file:
        files = {"upfile": read_file(file)}
        resp = await self._make_request(
            method="post",
            params=params,
            data={"prg": 1},
            files=files,
        )

    return YandexResponse(resp.text, resp.url)

Functions