|
| 1 | +import puppeteer from 'puppeteer-extra'; |
| 2 | +import StealthPlugin from 'puppeteer-extra-plugin-stealth'; |
| 3 | +import fs from 'fs/promises'; |
| 4 | + |
| 5 | +puppeteer.use(StealthPlugin()); |
| 6 | + |
| 7 | +const pageUrl = 'https://www.minecraft.net/en-us/download/server/bedrock'; |
| 8 | + |
| 9 | +const browser = await puppeteer.launch({ |
| 10 | + headless: 'new', |
| 11 | + args: [ |
| 12 | + '--no-sandbox', |
| 13 | + '--disable-setuid-sandbox', |
| 14 | + '--disable-http2', |
| 15 | + '--disable-dev-shm-usage', |
| 16 | + '--disable-gpu', |
| 17 | + '--disable-features=IsolateOrigins,site-per-process' |
| 18 | + ] |
| 19 | +}); |
| 20 | + |
| 21 | +try { |
| 22 | + const page = await browser.newPage(); |
| 23 | + |
| 24 | + await page.setUserAgent( |
| 25 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' |
| 26 | + ); |
| 27 | + |
| 28 | + await page.goto(pageUrl, { waitUntil: 'networkidle2', timeout: 120000 }); |
| 29 | + |
| 30 | + // Wait for release .zip links to be visible |
| 31 | + await page.waitForSelector('a[href$=".zip"]', { timeout: 30000 }); |
| 32 | + |
| 33 | + // Grab release URLs |
| 34 | + const releaseLinks = await page.evaluate(() => { |
| 35 | + const anchors = Array.from(document.querySelectorAll('a[href$=".zip"]')); |
| 36 | + return { |
| 37 | + Linux: anchors.find(a => a.href.includes('bin-linux/') && !a.href.includes('preview'))?.href || null, |
| 38 | + Windows: anchors.find(a => a.href.includes('bin-win/') && !a.href.includes('preview'))?.href || null |
| 39 | + }; |
| 40 | + }); |
| 41 | + |
| 42 | + // Click preview radio buttons to load preview download links |
| 43 | + const previewLinks = {}; |
| 44 | + |
| 45 | + for (const [labelOptions, key, match] of [ |
| 46 | + [['Ubuntu (Linux) Preview'], 'Linux', 'bin-linux-preview/'], |
| 47 | + [['Window Preview', 'Windows Preview'], 'Windows', 'bin-win-preview/'] |
| 48 | + ]) { |
| 49 | + // Wait for any matching label text |
| 50 | + await page.waitForFunction( |
| 51 | + (texts) => [...document.querySelectorAll('label')].some(l => texts.includes(l.textContent.trim())), |
| 52 | + {}, |
| 53 | + labelOptions |
| 54 | + ); |
| 55 | + |
| 56 | + // Click the corresponding label |
| 57 | + await page.evaluate((texts) => { |
| 58 | + const label = [...document.querySelectorAll('label')].find(l => texts.includes(l.textContent.trim())); |
| 59 | + if (label) label.click(); |
| 60 | + }, labelOptions); |
| 61 | + |
| 62 | + // Wait for preview .zip link to appear |
| 63 | + await page.waitForFunction( |
| 64 | + (match) => [...document.querySelectorAll('a[href$=".zip"]')].some(a => a.href.includes(match)), |
| 65 | + { timeout: 15000 }, |
| 66 | + match |
| 67 | + ); |
| 68 | + |
| 69 | + // Grab the preview URL |
| 70 | + previewLinks[key] = await page.evaluate((match) => { |
| 71 | + const a = [...document.querySelectorAll('a[href$=".zip"]')].find(a => a.href.includes(match)); |
| 72 | + return a ? a.href : null; |
| 73 | + }, match); |
| 74 | + } |
| 75 | + |
| 76 | + const allLinks = { |
| 77 | + Release: releaseLinks, |
| 78 | + Preview: previewLinks |
| 79 | + }; |
| 80 | + |
| 81 | + // Sanity check |
| 82 | + const missing = []; |
| 83 | + if (!allLinks.Release.Linux) missing.push('Release Linux'); |
| 84 | + if (!allLinks.Release.Windows) missing.push('Release Windows'); |
| 85 | + if (!allLinks.Preview.Linux) missing.push('Preview Linux'); |
| 86 | + if (!allLinks.Preview.Windows) missing.push('Preview Windows'); |
| 87 | + if (missing.length > 0) { |
| 88 | + throw new Error(`❌ One or more download URLs are missing: ${missing.join(', ')}`); |
| 89 | + } |
| 90 | + |
| 91 | + // Write to file |
| 92 | + await fs.writeFile('bedrock-urls.json', JSON.stringify(allLinks, null, 2)); |
| 93 | + console.log('✅ Bedrock download URLs scraped successfully.'); |
| 94 | + |
| 95 | +} catch (err) { |
| 96 | + console.error('❌ Failed to scrape Bedrock URLs:', err.message); |
| 97 | + process.exit(1); |
| 98 | +} finally { |
| 99 | + await browser.close(); |
| 100 | +} |
0 commit comments