2020-10-12 12:11:36 +00:00
|
|
|
/* istanbul ignore file */
|
|
|
|
|
2020-10-16 07:58:08 +00:00
|
|
|
'use strict';
|
2020-10-02 12:01:51 +00:00
|
|
|
|
2020-10-10 14:27:01 +00:00
|
|
|
// Core modules
|
2020-10-16 07:58:08 +00:00
|
|
|
const fs = require('fs');
|
2020-10-10 14:27:01 +00:00
|
|
|
|
|
|
|
// Public modules from npm
|
2020-10-16 07:58:08 +00:00
|
|
|
// const { File } = require('megajs');
|
2020-10-10 14:27:01 +00:00
|
|
|
|
|
|
|
// Modules from file
|
2020-10-16 07:58:08 +00:00
|
|
|
const { prepareBrowser, preparePage } = require('../puppeteer-helper.js');
|
|
|
|
const shared = require('../shared.js');
|
2020-10-10 14:27:01 +00:00
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
class GameDownload {
|
2020-10-08 21:09:05 +00:00
|
|
|
constructor() {
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Platform that hosts game files
|
|
|
|
* @type String
|
|
|
|
*/
|
2020-10-16 07:58:08 +00:00
|
|
|
this.hosting = '';
|
2020-10-08 21:09:05 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Link to game files
|
|
|
|
* @type String
|
|
|
|
*/
|
|
|
|
this.link = null;
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
|
|
|
* @public
|
2020-10-08 21:09:05 +00:00
|
|
|
* Operating systems supported by the game version indicated in this class.
|
|
|
|
* Can be *WINDOWS/LINUX/MACOS*
|
|
|
|
* @type String[]
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2020-10-08 21:09:05 +00:00
|
|
|
this.supportedOS = [];
|
|
|
|
}
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-08 21:09:05 +00:00
|
|
|
/**
|
|
|
|
* @public
|
2020-10-10 14:27:01 +00:00
|
|
|
* Download the game data in the indicated path.
|
|
|
|
* Supported hosting platforms: MEGA, NOPY
|
|
|
|
* @param {String} path Save path
|
|
|
|
* @return {Promise<Boolean>} Result of the operation
|
2020-10-08 21:09:05 +00:00
|
|
|
*/
|
2020-10-10 14:27:01 +00:00
|
|
|
async download(path) {
|
2020-10-16 07:58:08 +00:00
|
|
|
if (this.link.includes('mega.nz'))
|
2020-10-10 14:27:28 +00:00
|
|
|
return await downloadMEGA(this.link, path);
|
2020-10-16 07:58:08 +00:00
|
|
|
else if (this.link.includes('nopy.to'))
|
2020-10-10 14:27:28 +00:00
|
|
|
return await downloadNOPY(this.link, path);
|
2020-10-10 14:27:01 +00:00
|
|
|
}
|
2020-09-29 15:11:43 +00:00
|
|
|
}
|
2020-10-02 12:01:51 +00:00
|
|
|
module.exports = GameDownload;
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-10 14:27:01 +00:00
|
|
|
async function downloadMEGA(url, savepath) {
|
|
|
|
// The URL is masked
|
2020-10-16 07:21:19 +00:00
|
|
|
const browser = await prepareBrowser();
|
|
|
|
const page = await preparePage(browser);
|
2020-10-10 14:27:01 +00:00
|
|
|
await page.setCookie(...shared.cookies); // Set cookies to avoid login
|
|
|
|
await page.goto(url);
|
2020-10-16 07:58:08 +00:00
|
|
|
await page.waitForSelector('a.host_link');
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-10 14:27:01 +00:00
|
|
|
// Obtain the link for the unmasked page and click it
|
2020-10-16 07:58:08 +00:00
|
|
|
const link = await page.$('a.host_link');
|
2020-10-10 14:27:01 +00:00
|
|
|
await link.click();
|
|
|
|
await page.goto(url, {
|
2020-10-16 08:05:28 +00:00
|
|
|
waitUntil: shared.WAIT_STATEMENT
|
2020-10-10 14:27:01 +00:00
|
|
|
}); // Go to the game page and wait until it loads
|
|
|
|
|
|
|
|
// Obtain the URL after the redirect
|
2020-10-16 07:21:19 +00:00
|
|
|
const downloadURL = page.url();
|
2020-10-10 14:27:01 +00:00
|
|
|
|
|
|
|
// Close browser and page
|
|
|
|
await page.close();
|
|
|
|
await browser.close();
|
|
|
|
|
2020-10-16 07:21:19 +00:00
|
|
|
const stream = fs.createWriteStream(savepath);
|
|
|
|
const file = File.fromURL(downloadURL);
|
2020-10-10 14:27:01 +00:00
|
|
|
file.download().pipe(stream);
|
|
|
|
return fs.existsSync(savepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function downloadNOPY(url, savepath) {
|
|
|
|
// Prepare browser
|
2020-10-16 07:21:19 +00:00
|
|
|
const browser = await prepareBrowser();
|
|
|
|
const page = await preparePage(browser);
|
2020-10-10 14:27:01 +00:00
|
|
|
await page.goto(url);
|
2020-10-16 07:58:08 +00:00
|
|
|
await page.waitForSelector('#download');
|
2020-10-10 14:27:01 +00:00
|
|
|
|
|
|
|
// Set the save path
|
2020-10-16 07:58:08 +00:00
|
|
|
await page._client.send('Page.setDownloadBehavior', {
|
|
|
|
behavior: 'allow',
|
2020-10-16 08:05:28 +00:00
|
|
|
downloadPath: path.basename(path.dirname(savepath)) // It's a directory
|
2020-10-10 14:27:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Obtain the download button and click it
|
2020-10-16 07:58:08 +00:00
|
|
|
const downloadButton = await page.$('#download');
|
2020-10-10 14:27:01 +00:00
|
|
|
await downloadButton.click();
|
|
|
|
|
|
|
|
// Await for all the connections to close
|
|
|
|
await page.waitForNavigation({
|
2020-10-16 07:58:08 +00:00
|
|
|
waitUntil: 'networkidle0',
|
2020-10-16 08:05:28 +00:00
|
|
|
timeout: 0 // Disable timeout
|
2020-10-10 14:27:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Close browser and page
|
|
|
|
await page.close();
|
|
|
|
await browser.close();
|
|
|
|
|
|
|
|
return fs.existsSync(savepath);
|
|
|
|
}
|