File size: 2,367 Bytes
88ccbf7
 
1694ab1
88ccbf7
 
 
1694ab1
88ccbf7
 
 
 
 
 
 
 
 
1694ab1
88ccbf7
 
 
 
1694ab1
88ccbf7
 
 
 
 
 
 
1694ab1
88ccbf7
 
 
 
 
 
 
 
 
 
1694ab1
 
 
88ccbf7
1694ab1
 
 
 
88ccbf7
 
 
 
 
 
 
 
 
 
 
 
 
1694ab1
 
7b7c4ce
1694ab1
 
 
 
 
 
7b7c4ce
1694ab1
 
 
 
a26bc91
7b7c4ce
1694ab1
88ccbf7
 
 
 
 
 
 
 
9765f38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Just additional code for https://github.com/rkwyu/scribd-dl

import { exec } from 'child_process';
import { promisify } from 'util';
import cors from 'cors'
import express from 'express'
import { config } from 'dotenv'
config()
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const execPromise = promisify(exec);

async function executeCommand(command) {
    try {
        const { stdout, stderr } = await execPromise(command);
        if (stderr) {
            throw new Error(`Stderr: ${stderr}`);
        }

        return stdout;
    } catch (error) {
        throw new Error(`Error executing command: ${error.message}`);
    }
}

function checkIfGenerated(output) {
    const logs = output.trim().split('\n');
    const lastLog = logs[logs.length - 1];
    if (lastLog.includes('Generated: ')) {
        return { result: true, path: lastLog.split(" output/")[1] }
    } else {
        return { result: false, path: "" }
    }
}

async function main(url) {
    return new Promise(async (resolve, reject) => {
        try {
            const command = `npm start '${url}'`;
            const output = await executeCommand(command);
            resolve(checkIfGenerated(output));
        } catch (error) {
            if (error.toString().includes("Unsupported UR")) {
                reject({ error: "Not supported URL" })
            } else {
                reject({ error: error })
            }
        }
    })

}

const app = express()
app.use(cors())

app.get("/", (req, res) => {
    res.send("/down?url=<scribd>")
})

app.all("/down", async (req, res) => {
    const browserStartAttempts = 5;
    let a;
    for (let i = 0; i < browserStartAttempts; i++) {
        try {
            const { url } = req.query;
            if (!url) return res.json({ error: "invalid url/nourl" })
            return res.json(await main(url))
        } catch (e) {
            a = e
            console.log(e)
            // return res.json(e)
        }
    }
    
    return res.json({error: e})

})
app.use(express.static(path.join(__dirname, 'output')));

const port = process.env.SERVER_PORT || 7860
app.listen(port, () => {
    console.log("listening on port: " + port)
})

// await main(`https://www.scribd.com/doc/252120132/Rangkaian-Alarm-Anti-Maling`);