Skip to main content

Yandex business API client

Project description

Yandex business (sprav) API client codecov

Installation

# Sync only mode
pip install ya_business_api[sync]
# Async only mode
pip install ya_business_api[async]
# All modes
pip install ya_business_api[all]

Instantiating

There are several ways to work with the API (synchronous and asynchronous). Both interfaces have the same signatures, the only difference is the need to use async/await keywords.

from ya_business_api.sync_api import SyncAPI		# Sync mode
from ya_business_api.async_api import AsyncAPI		# Async mode


def main() -> None:
	api = SyncAPI.build(
		session_id=...,
		session_id2=...,
		csrf_token=...,		# Optional
	)

	# Do things here...


async def main() -> None:
	api = await AsyncAPI.build(
		session_id=...,
		session_id2=...,
		csrf_token=...,		# Optional
	)

	# Do things here...

	await api.session.close()

Where can I get the data for the client?

On the reviews page (https://yandex.ru/sprav/.../edit/reviews), open the developer console (usually F12) from the first request, copy values of cookies (Session_id and sessionid2).

In the console, run the following script:

function getData() {
	console.info({
		"CSRFToken": window?.__PRELOAD_DATA?.initialState?.env?.csrf,
		"PermanentId": window?.__PRELOAD_DATA?.initialState?.edit?.company?.permanent_id,
	})
}

getData()

/**
 * {CSRFToken: "...", PermanentId: 00000000000}
*/

⚠️WARNING⚠️

  1. The PermanentId belong to certain companies and cannot be used to respond to reviews from another company.
  2. The CSRFToken can be fetched automatically if it is not explicitly specified when calling the build method.

Reviews

Reviews fetching

  • Async mode support: ✅;
  • Validation disabling: ✅.
# Sync mode
from ya_business_api.sync_api import SyncAPI
from ya_business_api.reviews.dataclasses.requests import ReviewsRequest


api = SyncAPI.build(...)
request = ReviewsRequest(
	permanent_id=<permanent_id>,
	ranking=Ranking.BY_RATING_DESC,		# Optional
	unread=True,						# Optional
	page=5,								# Optional
)
response = api.reviews.get_reviews(
	request,
	raw=False,		# Optional
)

Answering to reviews

  • Async mode support: ✅;
  • Validation disabling: ❌.
from ya_business_api.sync_api import SyncAPI
from ya_business_api.reviews.dataclasses.requests import AnswerRequest


api = SyncAPI.build(...)
reviews = api.reviews.get_reviews()
request = AnswerRequest(
	review_id=reviews.list.items[0].id,
	text="Thank you!",
	reviews_csrf_token=reviews.list.csrf_token,
	answer_csrf_token=reviews.list.items[0].business_answer_csrf_token,
)
response = api.reviews.send_answer(request)

Companies

Receiving companies

  • Async mode support: ✅;
  • Validation disabling: ✅.
from ya_business_api.sync_api import SyncAPI
from ya_business_api.companies.dataclasses.requests import CompaniesRequest


api = SyncAPI.build(...)
request = CompaniesRequest(filter="My Company", page=5)
response = api.companies.get_companies(
	request,		# Optional
	raw=False,		# Optional
)

Receiving company chains

Some companies have several branches, in such cases the company will have the "chain" type. This method will allow you to get a list of all branches.

  • Async mode support: ✅;
  • Validation disabling: ✅.
from ya_business_api.sync_api import SyncAPI
from ya_business_api.companies.dataclasses.request import ChainListRequest


api = SyncAPI.build(...)
request = ChainListRequest(
	tycoon_id=<tycoon_id>,		# Can be found in the Company dataclass.
	geo_id=0,					# Optional.
	page=1,						# Optional
)
response = api.companies.get_chain_list(
	request,		# Optional
	raw=False,		# Optional
)

⚠️WARNING⚠️

Raw response of this method is HTML text.

Service

Receiving CSRF token

  • Async mode support: ✅;
  • Validation disabling: ❌.
from ya_business_api.sync_api import SyncAPI


api = SyncAPI.build(...)
csrf_token = api.service.get_csrf_token()

Shortcuts

Answers deleting

api.reviews.send_answer(AnswerRequest(text="", ...))

Automatic closing of the session (async mode)

async with await AsyncAPI.make_session(session_id=..., session_id2=...) as session:
	api = AsyncAPI(permanent_id=..., csrf_token=..., session=session)
	...

Examples

Getting the number of reviews asynchronously

from ya_business_api.async_api import AsyncAPI
from ya_business_api.companies.dataclasses.requests import ChainListRequest
from ya_business_api.companies.dataclasses.chain_list import CompanyCardWithPhoto
from ya_business_api.reviews.dataclasses.requests import ReviewsRequest


api = await AsyncAPI.build(...)

try:
	companies_response = await api.companies.get_companies()
	open_companies = filter(lambda x: x.publishing_status != "closed", companies_response.list_companies)

	for company in open_companies:
		page = 1
		chain_list_request = ChainListRequest(tycoon_id=company.tycoon_id, page=page)
		chain_list_response = await api.companies.get_chain_list(chain_list_request)

		while len(chain_list_response.company_cards):
			company_cards = [i for i in chain_list_response.company_cards if isinstance(i, CompanyCardWithPhoto)]
			# Only company cards with photo contains permalink
			# It is possible that the branch also contains branches, but this is not taken into account in this guide

			for card in company_cards:
				reviews_request = ReviewsRequest(permanent_id=card.permalink)
				reviews_response = await api.reviews.get_reviews(reviews_request)
				print(company.permanent_id, card.permalink, reviews_response.list.pager.total)

			page += 1
			chain_list_request = ChainListRequest(tycoon_id=company.tycoon_id, page=page)
			chain_list_response = await api.companies.get_chain_list(chain_list_request)

finally:
	await api.session.close()

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

ya_business_api-1.3.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

ya_business_api-1.3.0-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file ya_business_api-1.3.0.tar.gz.

File metadata

  • Download URL: ya_business_api-1.3.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for ya_business_api-1.3.0.tar.gz
Algorithm Hash digest
SHA256 f405219539e03ba6f102d1e52da41a1bda490e7668de87ee3f060e628bc5a15b
MD5 a0959b4a82ed37764451ae4dbede016e
BLAKE2b-256 493402813d5b8f5382de79b57ab66e9e0ef5e775078d5fbd399d5420fb7f6857

See more details on using hashes here.

File details

Details for the file ya_business_api-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ya_business_api-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a91287dd0a48471d159690e8043ed5a7ca95e1c26cfc1faab16e63bdb8737d89
MD5 06d2db1f8ae36946ee2a11a2fa2cd3af
BLAKE2b-256 8b73ee5d0ecaca24c0a04981d4e06b4fe64cd2387145b0a0f65ade9c59cd81b8

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 Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page