Last active
May 23, 2024 18:54
-
-
Save raulillana/0ea94c625fcbdafa8f9289659770792f to your computer and use it in GitHub Desktop.
Steam Workshop Scraper for mods (WorkshopID and ModID)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 1. Execute this code in the console of your Workshop collection page. | |
* ex: https://steamcommunity.com/sharedfiles/filedetails/?id=3247039796 | |
* 2. Wait for the CSV to download. | |
* 3. Import in excel and clean maps. | |
*/ | |
async function obtenerDetallesMods() { | |
const collectionItems = document.querySelectorAll('#profileBlock .collectionItem'); | |
const detallesMods = []; | |
const promises = []; | |
collectionItems.forEach(item => { | |
const title = item.querySelector('.workshopItemTitle').textContent.trim(); | |
const url = item.querySelector('.workshopItem > a').getAttribute('href'); | |
const workshopId = obtenerWorkshopId(url); | |
// Creamos una promesa para obtener el Mod ID | |
const promise = obtenerModId(url) | |
.then(modId => { | |
detallesMods.push({ | |
title: '=HYPERLINK("' + url +'"; "' + title +'")', | |
workshopId: workshopId, | |
modId: modId, | |
url: url | |
}); | |
}) | |
.catch(error => { | |
console.error(`Error obteniendo Mod ID para ${title}:`, error); | |
}); | |
promises.push(promise); | |
}); | |
// Esperamos a que todas las promesas se resuelvan | |
await Promise.all(promises); | |
// Una vez que se han obtenido todos los detalles, generamos el CSV | |
descargarCSV(detallesMods); | |
} | |
function obtenerWorkshopId(url) { | |
const match = url.match(/(\d+)$/); | |
return match ? match[1] : 'Workshop ID no encontrado'; | |
} | |
async function obtenerModId(url) { | |
try { | |
const response = await fetch(url); | |
const text = await response.text(); | |
const match = text.match(/Mod ID:\s*(.*)/); | |
return match ? match[1].replace(/<[^>]*>?/gm, '') : 'Mod ID no encontrado'; | |
} catch (error) { | |
console.error('Error al obtener Mod ID:', error); | |
throw error; | |
} | |
} | |
function descargarCSV(detallesMods) { | |
// Crear el contenido CSV | |
let csvContent = "data:text/csv;charset=utf-8," | |
+ "Title,Workshop ID,Mod ID,URL\n"; | |
detallesMods.forEach(detalle => { | |
const row = `${detalle.title},${detalle.workshopId},${detalle.modId},${detalle.url}\n`; | |
csvContent += row; | |
}); | |
// Crear un enlace para descargar el archivo CSV | |
const encodedUri = encodeURI(csvContent); | |
const link = document.createElement("a"); | |
link.setAttribute("href", encodedUri); | |
link.setAttribute("download", "mod_details.csv"); | |
document.body.appendChild(link); // Necesario para Firefox | |
link.click(); | |
} | |
// Ejecutar la función para obtener los detalles de los mods | |
obtenerDetallesMods(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment