73 lines
2.5 KiB
TypeScript
73 lines
2.5 KiB
TypeScript
import axios from 'axios';
|
|
import * as cheerio from 'cheerio';
|
|
|
|
export interface CooldownData {
|
|
isActive: boolean;
|
|
expiresAt?: Date;
|
|
}
|
|
|
|
export const scrapeCooldown = async (steamId: string, steamLoginSecure: string): Promise<CooldownData> => {
|
|
const url = `https://steamcommunity.com/profiles/${steamId}/gcpd/730?tab=matchmaking`;
|
|
|
|
try {
|
|
const response = await axios.get(url, {
|
|
headers: {
|
|
'Cookie': steamLoginSecure,
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
|
},
|
|
timeout: 10000
|
|
});
|
|
|
|
const $ = cheerio.load(response.data);
|
|
|
|
if (response.data.includes('Sign In') || !response.data.includes('Personal Game Data')) {
|
|
throw new Error('Invalid or expired steamLoginSecure cookie');
|
|
}
|
|
|
|
// 1. Locate the specific table containing cooldown info
|
|
let expirationDate: Date | undefined = undefined;
|
|
|
|
$('table').each((_, table) => {
|
|
const headers = $(table).find('th').map((_, th) => $(th).text().trim()).get();
|
|
const expirationIndex = headers.findIndex(h => h.includes('Competitive Cooldown Expiration') || h.includes('Cooldown Expiration'));
|
|
|
|
if (expirationIndex !== -1) {
|
|
const rows = $(table).find('tr').not(':has(th)');
|
|
rows.each((_, row) => {
|
|
const dateText = $(row).find('td').eq(expirationIndex).text().trim();
|
|
if (dateText && dateText !== '') {
|
|
// Steam uses 'GMT' which some JS engines don't parse well, replace with 'UTC'
|
|
const cleanDateText = dateText.replace(' GMT', ' UTC');
|
|
const parsed = new Date(cleanDateText);
|
|
|
|
if (!isNaN(parsed.getTime())) {
|
|
// We want the newest expiration date found
|
|
if (!expirationDate || parsed > (expirationDate as Date)) {
|
|
expirationDate = parsed;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
if (expirationDate && (expirationDate as Date).getTime() > Date.now()) {
|
|
console.log(`[Scraper] Found active cooldown until: ${(expirationDate as Date).toISOString()}`);
|
|
return {
|
|
isActive: true,
|
|
expiresAt: expirationDate
|
|
};
|
|
}
|
|
|
|
const content = $('#personal_game_data_content').text();
|
|
if (content.includes('Competitive Cooldown') || content.includes('Your account is currently')) {
|
|
return { isActive: true };
|
|
}
|
|
|
|
return { isActive: false };
|
|
} catch (error: any) {
|
|
console.error(`[Scraper] Error for ${steamId}:`, error.message);
|
|
throw error;
|
|
}
|
|
};
|