Files
supermarket/experiments/magnit-detail-endpoints/find-product-detail-endpoint-v1.ts
Mc Smog b8f170d83b fix: update import paths in debug scripts after reorganization
- Fix relative imports in experiments/ scripts (../ → ../../)
- Clean up tsconfig.json exclude list (remove non-existent paths)
- All debug scripts now work from their new location

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-22 02:02:52 +05:00

94 lines
3.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dotenv/config';
import { chromium } from 'playwright';
import axios from 'axios';
import { Logger } from '../../utils/logger.js';
async function main() {
Logger.info('=== Поиск endpoint для ДЕТАЛЕЙ товара ===\n');
// Получаем cookies
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://magnit.ru/', { waitUntil: 'domcontentloaded' });
const cookies = await context.cookies();
const cookieStr = cookies.map(c => `${c.name}=${c.value}`).join('; ');
const mgUdiCookie = cookies.find(c => c.name === 'mg_udi');
const deviceId = mgUdiCookie?.value || '';
const httpClient = axios.create({
baseURL: 'https://magnit.ru',
headers: {
'Content-Type': 'application/json',
'Accept': '*/*',
'Cookie': cookieStr,
'x-device-id': deviceId,
'x-client-name': 'magnit',
'x-device-platform': 'Web',
'x-new-magnit': 'true',
},
});
await browser.close();
const productId = '1000233138';
// Разные возможные endpoints для деталей товара
const endpoints = [
// v1 API (пользователь нашел reviews через v1)
`/webgate/v1/goods/${productId}?storeCode=992301&storeType=6`,
`/webgate/v1/products/${productId}?storeCode=992301`,
`/webgate/v1/catalog/product/${productId}?storeCode=992301`,
`/webgate/v1/listing/goods/${productId}?storeCode=992301`,
`/webgate/v1/listing/product/${productId}?storeCode=992301`,
// Другие варианты
`/webgate/v1/products/detail/${productId}?storeCode=992301`,
`/webgate/v1/object?productId=${productId}&storeCode=992301`,
`/webgate/v1/item/${productId}?storeCode=992301`,
];
for (const endpoint of endpoints) {
try {
Logger.info(`Пробую: ${endpoint}`);
const response = await httpClient.get(endpoint);
if (response.status === 200) {
Logger.info(`✅ Status: ${response.status}`);
const json = JSON.stringify(response.data);
if (json.length < 3000) {
Logger.info(`Response:\n${json}`);
} else {
// Проверяем, есть ли полезные поля
const data = response.data;
const hasDetails = data.brand || data.description || data.weight || data.unit;
if (hasDetails) {
Logger.info(`=== НАЙДЕНЫ ДЕТАЛИ ТОВАРА ===`);
Logger.info(`brand: ${data.brand}`);
Logger.info(`description: ${data.description?.substring(0, 100)}`);
Logger.info(`weight: ${data.weight}`);
Logger.info(`unit: ${data.unit}`);
break;
} else {
Logger.info(`Response без деталей товара (preview):`);
Logger.info(json.substring(0, 500) + '...');
}
}
}
} catch (error: any) {
if (error.response?.status === 404) {
Logger.info(` ❌ 404 Not Found`);
} else if (error.response?.status === 403) {
Logger.info(` ❌ 403 Forbidden`);
} else {
Logger.info(`${error.message}`);
}
}
}
}
main();