tak, zaraz podesle ci aktora ktoryt jest zlockowany na region pl. daj znac co myslisz o tym jak jest napisany oraz zmodyfikuj go tak (glownie plik main z racji ze struktura input schema mi sie podoba) aby dzialal na wszystkie regiony, tutaj masz main aktora zlockowanego na pl, poniewaz nie moge wkleic tutaj zipa:
import { Actor } from 'apify';
import { gotScraping } from 'got-scraping';
await Actor.init();
const input = await Actor.getInput();
const {
searchText,
proxyUrl,
totalItems = 96,
sortBy = 'relevance',
brand,
priceFrom,
priceTo
} = input;
if (!searchText || !proxyUrl) {
throw new Error('Musisz podać "searchText" i "proxyUrl" w input.');
}
console.log(🟢 [START] Używam proxy: ${proxyUrl});
const debugData = {
timestamp: new Date().toISOString(),
tokenFetch: {},
headers: {},
itemsCount: 0,
itemsPreview: [],
errors: [],
rawHtmlSample: '',
rawJsonSample: ''
};
// 1. Pobieranie tokenów
async function getVintedTokens() {
try {
console.log('🕵️♂️ Pobieram tokeny z https://www.vinted.pl...');
const response = await gotScraping({
url: 'https://www.vinted.pl/',
proxyUrl,
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'accept-language': 'pl-PL,pl;q=0.9',
}
});
debugData.rawHtmlSample = response.body.slice(0, 1000);
const cookies = response.headers['set-cookie'] || [];
const cookieHeader = cookies.map(c => c.split(';')[0]).join('; ');
const anonIdFromHeader = response.headers['x-anon-id'] || null;
const anonIdFromCookie = cookieHeader.match(/anon_id=([a-z0-9\-]+)/i)?.[1] || null;
const anonId = anonIdFromHeader || anonIdFromCookie;
console.log(`✅ Tokeny pobrane:`);
console.log(` • anonId: ${anonId}`);
console.log(` • cookieHeader: ${cookieHeader.slice(0, 80)}...`);
return { cookieHeader, anonId };
} catch (err) {
debugData.errors.push(`Token fetch error: ${err.message}`);
console.error('❌ Błąd podczas pobierania tokenów!');
throw new Error('Nie udało się pobrać wymaganych tokenów!');
}
}
const { cookieHeader, anonId } = await getVintedTokens();
debugData.tokenFetch = { cookieHeader, anonId };
// 2. Przygotowanie nagłówków
const headers = {
'accept': 'application/json',
'x-anonymous-id': anonId,
'x-money-object': 'true',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'cookie': cookieHeader,
'accept-language': 'pl-PL,pl;q=0.9',
'referer': 'https://www.vinted.pl/',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'sec-ch-ua': '"Chromium";v="114", "Google Chrome";v="114", "Not:A-Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
};
debugData.headers = headers;
// 3. Pobieranie ogłoszeń z API
let items = [];
let page = 1;
let done = false;
while (!done) {
const apiUrl = new URL('https://www.vinted.pl/api/v2/catalog/items');
apiUrl.searchParams.set('search_text', searchText);
apiUrl.searchParams.set('per_page', '96');
apiUrl.searchParams.set('page', page.toString());
if (sortBy) apiUrl.searchParams.set('order', sortBy);
if (brand) apiUrl.searchParams.set('brand', brand);
if (priceFrom) apiUrl.searchParams.set('price_from', priceFrom);
if (priceTo) apiUrl.searchParams.set('price_to', priceTo);
console.log(`🌐 Wysyłam zapytanie do API:\n ${apiUrl.toString()}`);
try {
const apiResponse = await gotScraping({
url: apiUrl.toString(),
method: 'GET',
headers,
proxyUrl,
responseType: 'json',
});
const data = apiResponse.body;
if (page === 1) debugData.rawJsonSample = JSON.stringify(data).slice(0, 1000);
if (data && data.items && Array.isArray(data.items)) {
for (const item of data.items) {
const thumbnail = item.photo?.url || item.photos?.[0]?.url || null;
items.push({
tytuł: item.title,
cena: item.price || '',
thumbnail,
liczba_lajków: item.favourite_count || 0,
liczba_wyświetleń: item.view_count || 0,
link: `https://www.vinted.pl${item.path}`,
});
if (items.length >= totalItems) {
done = true;
break;
}
}
} else {
debugData.errors.push('Brak danych "items" w odpowiedzi API.');
console.warn('⚠️ Brak danych "items" w odpowiedzi API.');
break;
}
} catch (err) {
debugData.errors.push(`API request error: ${err.message}`);
console.error(`❌ Błąd API Vinted: ${err.message}`);
break;
}
page++;
}
debugData.itemsCount = items.length;
debugData.itemsPreview = items.slice(0, 3);
console.log(✅ Pobrano ${items.length} ogłoszeń.);
// 4. Zapis danych
await Actor.pushData(items);
// 5. Zapis debug.json
await Actor.setValue('debug.json', debugData);
console.log('🏁 Zakończono działanie aktora.');
await Actor.exit();
oraz tutaj masz input schema:
{
"title": "Vinted API Scraper",
"description": "Scrapes Vinted listings using API with anonymous token handling",
"type": "object",
"schemaVersion": 1,
"properties": {
"searchText": {
"title": "Search term",
"type": "string",
"description": "e.g. oakley sunglasses",
"default": "oakley sunglasses",
"editor": "textfield"
},
"proxyUrl": {
"title": "Proxy URL",
"type": "string",
"description": "Proxy in format http://user:pass@host:port",
"editor": "textfield"
},
"totalItems": {
"title": "Number of items to scrape",
"type": "integer",
"description": "e.g. 96, 150, 200 (96 = one full page)",
"default": 96,
"editor": "number"
},
"sortBy": {
"title": "Sort by",
"type": "string",
"description": "How to sort results?",
"default": "relevance",
"editor": "select",
"enum": [
"relevance",
"newest_first",
"price_high_to_low",
"price_low_to_high"
]
},
"brand": {
"title": "Brand (optional)",
"type": "string",
"description": "e.g. adidas, nike (leave empty to skip)",
"editor": "textfield"
},
"priceFrom": {
"title": "Price from (optional)",
"type": "string",
"description": "Minimum price in local currency",
"editor": "textfield"
},
"priceTo": {
"title": "Price to (optional)",
"type": "string",
"description": "Maximum price in local currency",
"editor": "textfield"
},
"region": {
"title": "Region",
"type": "string",
"description": "Country/region of Vinted",
"enum": [
"Poland",
"France",
"Germany",
"Italy",
"Belgium",
"Spain",
"Netherlands",
"Austria",
"Czechia",
"Hungary",
"Lithuania",
"Slovakia",
"Luxembourg",
"Slovenia",
"Finland",
"Sweden",
"Denmark",
"Portugal",
"Ireland",
"Croatia"
],
"default": "Poland"
}
},
"required": [
"searchText",
"proxyUrl"
]
}