jsonp attempts
This commit is contained in:
16
gpt/index.html
Normal file
16
gpt/index.html
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Chess Game Downloader</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Chess Game Downloader</h1>
|
||||
<input type="text" id="username" placeholder="Enter username">
|
||||
<button onclick="downloadGames()">Download Games</button>
|
||||
|
||||
<script src="main.js"></script>
|
||||
</body>
|
||||
</html>
|
94
gpt/main.js
Normal file
94
gpt/main.js
Normal file
@@ -0,0 +1,94 @@
|
||||
// main.js
|
||||
|
||||
function jsonp(url, callbackName) {
|
||||
return new Promise((resolve) => {
|
||||
const script = document.createElement('script');
|
||||
script.src = `${url}&callback=${callbackName}`;
|
||||
document.body.appendChild(script);
|
||||
|
||||
window[callbackName] = (data) => {
|
||||
delete window[callbackName];
|
||||
document.body.removeChild(script);
|
||||
resolve(data);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function getGames(username, userAgents) {
|
||||
const gamesUrl = `https://api.chess.com/pub/player/${username}/games/archives`;
|
||||
|
||||
try {
|
||||
const response = await jsonp(gamesUrl);
|
||||
// Extracting URLs
|
||||
const gameUrls = response.archives.map(url => `${url}/.pgn`);
|
||||
return gameUrls;
|
||||
} catch (error) {
|
||||
console.error('Error fetching game URLs:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchGames(url, userAgent) {
|
||||
const callbackName = `jsonp_callback_${Math.round(100000 * Math.random())}`;
|
||||
|
||||
try {
|
||||
const response = await jsonp(url, callbackName);
|
||||
// Process the JSONP response as needed
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error(`Error fetching game from ${url}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function concatenateGames(gameResults) {
|
||||
// Customize how you want to concatenate the games
|
||||
return gameResults.join('\n\n');
|
||||
}
|
||||
|
||||
async function downloadGames() {
|
||||
const username = document.getElementById('username').value;
|
||||
|
||||
// Function 1: Get game URLs
|
||||
const allUserAgents = await loadUserAgents();
|
||||
const urls = await getGames(username, allUserAgents);
|
||||
|
||||
// Function 2: Asynchronously fetch games
|
||||
const gamePromises = urls.map(async (url, index) => {
|
||||
const userAgent = allUserAgents[index % allUserAgents.length];
|
||||
return await fetchGames(url, userAgent);
|
||||
});
|
||||
|
||||
try {
|
||||
// Function 3: Concatenate game results
|
||||
const gameResults = await Promise.all(gamePromises);
|
||||
const concatenatedGames = concatenateGames(gameResults);
|
||||
|
||||
// Function 4: Trigger download
|
||||
download(concatenatedGames, 'chess_games.pgn', 'application/pgn');
|
||||
} catch (error) {
|
||||
console.error('Error fetching games:', error);
|
||||
}
|
||||
}
|
||||
|
||||
function download(content, fileName, contentType) {
|
||||
const a = document.createElement('a');
|
||||
const file = new Blob([content], { type: contentType });
|
||||
a.href = URL.createObjectURL(file);
|
||||
a.download = fileName;
|
||||
a.click();
|
||||
}
|
||||
|
||||
async function loadUserAgents() {
|
||||
try {
|
||||
const userAgentsModule = await import('./userAgents.js');
|
||||
return userAgentsModule.default;
|
||||
} catch (error) {
|
||||
console.error('Error loading user agents:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
// Your initialization logic here...
|
||||
});
|
14
gpt/userAgents.js
Normal file
14
gpt/userAgents.js
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
// userAgents.js
|
||||
|
||||
const userAgents = [
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35",
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35",
|
||||
"Mozilla/5.0 (Windows NT 6.1; rv:109.0) Gecko/20100101 Firefox/113.0",
|
||||
"Mozilla/5.0 (Android 12; Mobile; rv:109.0) Gecko/113.0 Firefox/113.0",
|
||||
"mozilla/5.0 (macintosh; intel mac os x 10.15; rv:109.0) gecko/20100101 firefox/113.0",
|
||||
// Add more user agents as needed
|
||||
];
|
||||
|
||||
export default userAgents;
|
Reference in New Issue
Block a user