6ac32457a9
Replace all console.log/error with Pino logger (info/warn/error/debug/fatal) for structured JSON logging in production and pretty-print in development. Remove redundant header dumps and consolidate rate-limit logging. Add GitHub Actions workflow with release-please for automated semver releases and Docker build/push to GHCR on new releases.
211 lines
6.5 KiB
JavaScript
211 lines
6.5 KiB
JavaScript
const https = require('https');
|
|
const logger = require('../logger');
|
|
|
|
async function fetchPlayerDataHTTP(pdgaNumber) {
|
|
return new Promise((resolve, reject) => {
|
|
const options = {
|
|
hostname: 'www.pdga.com',
|
|
port: 443,
|
|
path: `/player/${pdgaNumber}`,
|
|
method: 'GET',
|
|
headers: {
|
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
|
},
|
|
timeout: 30000
|
|
};
|
|
|
|
const req = https.request(options, (res) => {
|
|
let data = '';
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
if (res.statusCode === 200) {
|
|
resolve(data);
|
|
} else {
|
|
const rateLimitInfo = {
|
|
statusCode: res.statusCode,
|
|
headers: res.headers
|
|
};
|
|
|
|
logger.info(`PDGA Response Status for #${pdgaNumber}: ${res.statusCode}`);
|
|
|
|
logger.debug({
|
|
retryAfter: res.headers['retry-after'],
|
|
rateLimit: res.headers['x-ratelimit-limit'],
|
|
rateLimitRemaining: res.headers['x-ratelimit-remaining'],
|
|
rateLimitReset: res.headers['x-ratelimit-reset']
|
|
}, `Rate limit details for #${pdgaNumber}`);
|
|
|
|
const error = new Error(`HTTP ${res.statusCode}`);
|
|
error.rateLimitInfo = rateLimitInfo;
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', (error) => {
|
|
logger.error(`Request error for PDGA #${pdgaNumber}: ${error.code} ${error.message}`);
|
|
reject(error);
|
|
});
|
|
|
|
req.on('timeout', () => {
|
|
req.destroy();
|
|
reject(new Error('Request timeout'));
|
|
});
|
|
|
|
req.setTimeout(30000);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
function parsePlayerData(html, pdgaNumber) {
|
|
try {
|
|
const nameMatch = html.match(/<title>([^<]+?)\s*\|\s*Professional Disc Golf Association/i);
|
|
const name = nameMatch ? nameMatch[1].trim() : 'Unknown';
|
|
|
|
const ratingMatch = html.match(/Current Rating:[^>]*>\s*(\d+)/i);
|
|
const rating = ratingMatch ? parseInt(ratingMatch[1]) : 0;
|
|
|
|
const changeMatch = html.match(/Current Rating:[\s\S]*?([+\-]\d+)[\s\S]*?\(as of/i);
|
|
const ratingChange = changeMatch ? parseInt(changeMatch[1]) : null;
|
|
|
|
return {
|
|
pdgaNumber,
|
|
name: name.replace(/\s*#\d+$/, ''),
|
|
rating,
|
|
ratingChange,
|
|
predictedRating: null
|
|
};
|
|
} catch (error) {
|
|
logger.error(`Error parsing data for PDGA ${pdgaNumber}: ${error.message}`);
|
|
return {
|
|
pdgaNumber,
|
|
name: 'Error',
|
|
rating: 0,
|
|
ratingChange: null,
|
|
predictedRating: null
|
|
};
|
|
}
|
|
}
|
|
|
|
async function fetchRatingHistory(pdgaNumber) {
|
|
return new Promise((resolve, reject) => {
|
|
const options = {
|
|
hostname: 'www.pdga.com',
|
|
port: 443,
|
|
path: `/player/${pdgaNumber}/history`,
|
|
method: 'GET',
|
|
headers: {
|
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
|
},
|
|
timeout: 30000
|
|
};
|
|
|
|
logger.info(`Fetching rating history for PDGA #${pdgaNumber} from: https://www.pdga.com/player/${pdgaNumber}/history`);
|
|
|
|
const req = https.request(options, (res) => {
|
|
let data = '';
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
if (res.statusCode === 200) {
|
|
logger.info(`Rating history request successful for PDGA #${pdgaNumber}`);
|
|
resolve(data);
|
|
} else {
|
|
logger.error(`Rating History Error for PDGA #${pdgaNumber}:`);
|
|
logger.error(`Status: ${res.statusCode}`);
|
|
|
|
logger.debug({
|
|
retryAfter: res.headers['retry-after'],
|
|
rateLimit: res.headers['x-ratelimit-limit'],
|
|
rateLimitRemaining: res.headers['x-ratelimit-remaining']
|
|
}, `Rate limit details for history #${pdgaNumber}`);
|
|
|
|
if (data.length > 0) {
|
|
logger.debug(`Partial response received (${data.length} bytes): ${data.substring(0, 200)}`);
|
|
}
|
|
|
|
const error = new Error(`HTTP ${res.statusCode} for rating history`);
|
|
error.statusCode = res.statusCode;
|
|
error.headers = res.headers;
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', (error) => {
|
|
logger.error({
|
|
code: error.code,
|
|
message: error.message,
|
|
errno: error.errno,
|
|
syscall: error.syscall
|
|
}, `Rating history request error for PDGA #${pdgaNumber}`);
|
|
|
|
if (error.code === 'ECONNRESET') {
|
|
logger.debug('Connection reset on rating history - likely rate limited by PDGA');
|
|
}
|
|
if (error.code === 'ECONNREFUSED') {
|
|
logger.debug('Connection refused - PDGA server may be blocking requests');
|
|
}
|
|
if (error.code === 'ETIMEDOUT') {
|
|
logger.debug('Request timed out - server may be overloaded');
|
|
}
|
|
|
|
reject(error);
|
|
});
|
|
|
|
req.on('timeout', () => {
|
|
logger.info(`Rating history request timeout for PDGA #${pdgaNumber} after 30s`);
|
|
req.destroy();
|
|
reject(new Error('Request timeout'));
|
|
});
|
|
|
|
req.setTimeout(30000);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
function parseRatingHistory(html) {
|
|
const history = [];
|
|
|
|
const rowMatches = html.match(/<tr[^>]*>[\s\S]*?<\/tr>/gi);
|
|
|
|
if (rowMatches) {
|
|
for (const row of rowMatches) {
|
|
if (row.includes('<th') || !row.includes('<td')) continue;
|
|
|
|
const cellMatches = row.match(/<td[^>]*>(.*?)<\/td>/gi);
|
|
|
|
if (cellMatches && cellMatches.length >= 2) {
|
|
const dateText = cellMatches[0].replace(/<[^>]*>/g, '').trim();
|
|
const ratingText = cellMatches[1].replace(/<[^>]*>/g, '').trim();
|
|
|
|
const dateMatch = dateText.match(/(\d{1,2})-([A-Za-z]{3})-(\d{4})/);
|
|
if (dateMatch && !isNaN(parseInt(ratingText))) {
|
|
const [, day, month, year] = dateMatch;
|
|
const monthMap = {
|
|
'Jan': 0, 'Feb': 1, 'Mar': 2, 'Apr': 3, 'May': 4, 'Jun': 5,
|
|
'Jul': 6, 'Aug': 7, 'Sep': 8, 'Oct': 9, 'Nov': 10, 'Dec': 11
|
|
};
|
|
|
|
const date = new Date(parseInt(year), monthMap[month], parseInt(day));
|
|
|
|
history.push({
|
|
date: date.toISOString().split('T')[0],
|
|
rating: parseInt(ratingText),
|
|
displayDate: dateText
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return history.sort((a, b) => new Date(a.date) - new Date(b.date));
|
|
}
|
|
|
|
module.exports = { fetchPlayerDataHTTP, parsePlayerData, fetchRatingHistory, parseRatingHistory };
|