Compare commits
2
Commits
f5b3ce997f
...
4dd4494435
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4dd4494435 | ||
|
|
c6c606b504 |
@@ -4,7 +4,7 @@ from urllib.parse import urlencode
|
||||
|
||||
from aioweb_tls import TLSSession
|
||||
from monitor_utils.config import DevelopmentMode
|
||||
from monitor_utils.constants import CYCLE_DELAY, EXPECTED_TASK_ERRORS
|
||||
from monitor_utils.constants import CYCLE_DELAY
|
||||
from monitor_utils.database import add_products_to_database
|
||||
from monitor_utils.models import AvailabilityInfo, PricingInfo, Product, ProductMetadata
|
||||
from monitor_utils.runner import (
|
||||
@@ -14,7 +14,7 @@ from monitor_utils.runner import (
|
||||
run_bounded_cycle,
|
||||
setup_monitor_logging,
|
||||
)
|
||||
from monitor_utils.sessions import send_request
|
||||
from monitor_utils.sessions import get_response_json, send_request
|
||||
from motor.motor_asyncio import AsyncIOMotorCollection
|
||||
|
||||
setup_monitor_logging(monitor="samsung_shop")
|
||||
@@ -53,34 +53,32 @@ async def get_category_products(
|
||||
offset=0,
|
||||
):
|
||||
LOGGER.debug("getting products", extra={"category": category_id, "offset": offset})
|
||||
try:
|
||||
response = await send_request(
|
||||
session,
|
||||
url="https://sribsrch.ecom.samsung.com/estoresearch-api/v1/scom/us/pf_search",
|
||||
method="POST",
|
||||
data=urlencode(
|
||||
{
|
||||
"clientCode": "b2c",
|
||||
"clientName": "scom_pf",
|
||||
"firstSearchYN": "true",
|
||||
"countryCode": "us",
|
||||
"storeID": "us_education",
|
||||
"startIndex": str(offset),
|
||||
"requestCount": str(PRODUCTS_PER_PAGE),
|
||||
"category_code": category_id,
|
||||
"sort": "newest",
|
||||
}
|
||||
),
|
||||
headers={"content-type": "application/x-www-form-urlencoded"},
|
||||
)
|
||||
except EXPECTED_TASK_ERRORS:
|
||||
LOGGER.warning(
|
||||
"failed to get products",
|
||||
extra={"category": category_id, "offset": offset},
|
||||
)
|
||||
raise
|
||||
response = await send_request(
|
||||
session,
|
||||
url="https://sribsrch.ecom.samsung.com/estoresearch-api/v1/scom/us/pf_search",
|
||||
method="POST",
|
||||
data=urlencode(
|
||||
{
|
||||
"clientCode": "b2c",
|
||||
"clientName": "scom_pf",
|
||||
"firstSearchYN": "true",
|
||||
"countryCode": "us",
|
||||
"storeID": "us_education",
|
||||
"startIndex": str(offset),
|
||||
"requestCount": str(PRODUCTS_PER_PAGE),
|
||||
"category_code": category_id,
|
||||
"sort": "newest",
|
||||
}
|
||||
),
|
||||
headers={"content-type": "application/x-www-form-urlencoded"},
|
||||
)
|
||||
|
||||
data = get_response_json(
|
||||
response, logger=LOGGER, category=category_id, offset=offset
|
||||
)
|
||||
if data is None:
|
||||
return
|
||||
|
||||
data = response.json()
|
||||
if offset != 0:
|
||||
await process_page(data, category_id, offset, collection)
|
||||
return
|
||||
@@ -114,34 +112,27 @@ async def process_page(
|
||||
):
|
||||
"""Extract and persist one page of category search results."""
|
||||
|
||||
try:
|
||||
products = extract_products(response)
|
||||
if products:
|
||||
await add_products_to_database(
|
||||
products,
|
||||
collection,
|
||||
category_identifier=category_id,
|
||||
monitor_name="samsung_shop",
|
||||
)
|
||||
LOGGER.info(
|
||||
"added products",
|
||||
extra={
|
||||
"category": category_id,
|
||||
"offset": offset,
|
||||
"count": len(products),
|
||||
},
|
||||
)
|
||||
else:
|
||||
LOGGER.debug(
|
||||
"no products found",
|
||||
extra={"category": category_id, "offset": offset},
|
||||
)
|
||||
except EXPECTED_TASK_ERRORS:
|
||||
LOGGER.warning(
|
||||
"failed to build products",
|
||||
products = extract_products(response)
|
||||
if products:
|
||||
await add_products_to_database(
|
||||
products,
|
||||
collection,
|
||||
category_identifier=category_id,
|
||||
monitor="samsung_shop",
|
||||
)
|
||||
LOGGER.info(
|
||||
"added products",
|
||||
extra={
|
||||
"category": category_id,
|
||||
"offset": offset,
|
||||
"count": len(products),
|
||||
},
|
||||
)
|
||||
else:
|
||||
LOGGER.debug(
|
||||
"no products found",
|
||||
extra={"category": category_id, "offset": offset},
|
||||
)
|
||||
raise
|
||||
|
||||
|
||||
def extract_products(response: dict) -> list[Product]:
|
||||
|
||||
Reference in new issue
Block a user