File size: 5,187 Bytes
8dc4abf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import axios from "axios";
import FormData from "form-data";
import * as cheerio from "cheerio";
import { sendSuccess, ErrorResponses } from "../../lib/response-helper.js";

class TeraBox {
  constructor() {
    this.BASE_URL = "https://terabxdownloader.org";
    this.AJAX_PATH = "/wp-admin/admin-ajax.php";
    this.HEADERS = {
      "accept": "*/*",
      "accept-language": "id-ID",
      "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
      "Referer": "https://terabxdownloader.org/",
      "Referrer-Policy": "strict-origin-when-cross-origin"
    };
    this.CREATED_BY = "Ditzzy";
    this.NOTE = "Thank you for using this scrape, I hope you appreciate me for making this scrape by not deleting wm";
  }

  wrapResponse(data) {
    return {
      created_by: this.CREATED_BY,
      note: this.NOTE,
      results: data
    };
  }

  transformFolder(rawFolder) {
    return {
      name: rawFolder["πŸ“‚ Name"],
      type: rawFolder["πŸ“‹ Type"],
      size: rawFolder["πŸ“ Size"]
    };
  }

  transformFile(rawFile) {
    return {
      name: rawFile["πŸ“‚ Name"],
      type: rawFile["πŸ“‹ Type"],
      fullPath: rawFile["πŸ“ Full Path"],
      size: rawFile["πŸ“ Size"],
      downloadLink: rawFile["πŸ”½ Direct Download Link"]
    };
  }

  transformSummary(rawSummary) {
    return {
      totalFolders: rawSummary["πŸ“ Total Folders"],
      totalFiles: rawSummary["πŸ“„ Total Files"],
      totalItems: rawSummary["πŸ”’ Total Items"]
    };
  }

  extractData(rawResponse) {
    const rawData = rawResponse.data;

    return {
      folders: (rawData["πŸ“ Folders"] || []).map(folder => this.transformFolder(folder)),
      files: (rawData["πŸ“„ Files"] || []).map(file => this.transformFile(file)),
      summary: rawData["πŸ“Š Summary"] 
        ? this.transformSummary(rawData["πŸ“Š Summary"])
        : { totalFolders: 0, totalFiles: 0, totalItems: 0 },
      shortlink: rawData["πŸ”— ShortLink"] || ""
    };
  }

  async getNonce() {
    const { data } = await axios.get(this.BASE_URL);

    const $ = cheerio.load(data);
    const nncSc = $('#jquery-core-js-extra').html();
    
    if (!nncSc) {
      throw new Error("Nonce script not found, Unable to continue.");
    }

    const match = nncSc.match(/"nonce"\s*:\s*"([^"]+)"/i);

    if (!match) {
      throw new Error('Nonce script found but Nonce value could not be found');
    }

    return match[1];
  }

  async download(url) {
    try {
      const nonce = await this.getNonce();

      const form = new FormData();
      form.append('action', 'terabox_fetch');
      form.append('url', url);
      form.append('nonce', nonce);

      const config = {
        url: this.BASE_URL + this.AJAX_PATH,
        method: "POST",
        headers: this.HEADERS,
        data: form
      };

      const { data } = await axios.request(config);
      const extractedData = this.extractData(data);

      return this.wrapResponse(extractedData);
    } catch (e) {
      throw new Error(`Error downloading from TeraBox: ${e instanceof Error ? e.message : String(e)}`);
    }
  }
}

/** @type {import("../../types/plugin").ApiPluginHandler} */
const handler = {
  name: "Terabox Downloader",
  description: "Download terabox files or folders",
  version: "1.0.0",
  method: "GET",
  category: ["downloader"],
  alias: ["terabox"],
  tags: ["downloader"],
  parameters: {
    query: [
      {
        name: "url",
        type: "string",
        required: true,
        description: "Your Terabox URL",
        example: "https://1024terabox.com/s/1hTspAuZCdy5vDAPiUOn3ig"
      }
    ],
    body: [],
    headers: []
  },
  responses: {
    200: {
      status: 200,
      description: "Successfully retrieved Terabox data",
      example: {
        status: 200,
        author: "Ditzzy",
        note: "Thank you for using this API!",
        results: {}
      }
    },
    400: {
      status: 400,
      description: "Invalid Terabox URL provided",
      example: {
        status: 400,
        message: "Invalid URL - must be a valid Terabox URL"
      }
    },
    404: {
      status: 404,
      description: "Missing required parameter",
      example: {
        status: 404,
        message: "Missing required parameter: ..."
      }
    },
    500: {
      status: 500,
      description: "Server error or Terabox API unavailable",
      example: {
        status: 500,
        message: "An error occurred, please try again later."
      }
    }
  },
  exec: async (req, res) => {
    const { url } = req.query
    
    if (!url) return ErrorResponses.missingParameter(res, "url");
    
    const regex = /^(https?:\/\/)?(www\.)?(terabox\.com|teraboxapp\.com|1024tera\.com|1024terabox\.com|terabox\.app|nephobox\.com)\/(s\/|sharing\/embed\?surl=)[\w-]+/i
    
    if (!regex.test(url)) return ErrorResponses.invalidUrl(res, "Invalid URL - must be a valid Terabox URL");
    
    const terabox = new TeraBox();    
    try {
      const download = await terabox.download(url);
      
      return sendSuccess(res, download.results);
    } catch (e) {
      console.error("Terabox download error:", e);
      return ErrorResponses.serverError(res);
    }
  }
}

export default handler;