File size: 5,369 Bytes
1f52f7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { version } = require("../package.json");

const {
  getGlobalStats,
  downloadAria,
  getDownloadStatus,
  getOngoingDownloads,
  cancelDownload,
} = require("./aria2.js");

const { getIpData } = require("./ip.js");

const {
  bytesToSize,
  deleteOldFiles,
  cleanUser,
  teleariaPort,
  teleariaURL,
} = require("./utils.js");

const commands = [
  "/about - Learn more about this bot",
  "/start - Get started and see available options",
  "/stats - View global download and upload statistics",
  "/download <url> - Start a new download with a specified URL",
  "/downloading - Check the progress of ongoing downloads",
  "/status_<gid> - Get the status of a specific download (use the GID)",
  "/cancel_<gid> - Cancel a specific download (use the GID)",
  "/ip - Display server IP information",
  "/clean - Remove oldest downloaded file",
];

const handleAbout = (ctx) => {
  ctx.reply("GitHub Repo: TeleAria (https://github.com/besoeasy/telearia)");
};

const handleStart = (ctx) => {
  const userIdHash = cleanUser(ctx.chat.id);
  ctx.reply(
    `Welcome to TeleAria! 🎉\n\n` +
      `Version: ${version} on Port : ${teleariaPort}\n` +
      `User ID: ${userIdHash}\n` +
      `Downloads URL: ${teleariaURL}/${userIdHash}/\n\n` +
      `Stremio Addon: ${teleariaURL}/manifest.json\n\n` +
      `Available Commands:\n` +
      commands.map((cmd) => `- ${cmd}`).join("\n")
  );
};

const handleStats = async (ctx) => {
  try {
    const { result: stats } = await getGlobalStats();
    ctx.reply(
      `Global Stats:\n\n` +
        `Download Speed: ${bytesToSize(stats.downloadSpeed)}\n` +
        `Upload Speed: ${bytesToSize(stats.uploadSpeed)}\n` +
        `Active Downloads: ${stats.numActive}\n` +
        `Waiting Downloads: ${stats.numWaiting}\n` +
        `Stopped Downloads: ${stats.numStopped}`
    );
  } catch (error) {
    console.error(error);
    ctx.reply("Failed to retrieve stats. Please try again later.");
  }
};

const handleDownload = async (ctx, url) => {
  try {
    const userIdHash = cleanUser(ctx.chat.id);
    const downloadData = await downloadAria(userIdHash, url);
    const downloadId = downloadData.result;

    ctx.reply(
      `Track progress with /status_${downloadId} or view all downloads with /downloading`
    );
  } catch (error) {
    console.error(error);
    ctx.reply("Failed to start download. Please try again later.");
  }
};

const handleStatus = async (ctx, downloadId) => {
  try {
    const downloadData = await getDownloadStatus(downloadId);
    const completedSize = (
      downloadData.result.completedLength /
      1024 /
      1024
    ).toFixed(2);
    const totalSize = (downloadData.result.totalLength / 1024 / 1024).toFixed(
      2
    );

    let reply =
      `Download Status:\n\nStatus: ${downloadData.result.status}\n` +
      `Progress: ${completedSize} MB / ${totalSize} MB`;

    if (downloadData.result.status === "active") {
      reply += `\nCancel with /cancel_${downloadId}\n\n`;
    }

    const files = downloadData.result.files.map((file) => file.path).join("\n");
    reply += `\nDownloaded Files:\n${files}`;

    ctx.reply(reply);
  } catch (error) {
    console.error(error);
    ctx.reply(
      `Failed to retrieve status for download ID: ${downloadId}. Please try again later.`
    );
  }
};

const handleCancel = async (ctx, downloadId) => {
  try {
    await cancelDownload(downloadId);
    ctx.reply(`Download with ID ${downloadId} canceled successfully.`);
  } catch (error) {
    console.error(error);
    ctx.reply(
      `Failed to cancel download with ID: ${downloadId}. Please try again later.`
    );
  }
};

const handleIpData = async (ctx) => {
  try {
    const ipData = await getIpData();
    ctx.reply(
      `Server IP Information:\n\n` +
        `IP: ${ipData.query}\n` +
        `Country: ${ipData.country}\n` +
        `Region: ${ipData.regionName}\n` +
        `City: ${ipData.city}\n` +
        `ISP: ${ipData.isp}`
    );
  } catch (error) {
    console.error(error);
    ctx.reply("Failed to retrieve IP data. Please try again later.");
  }
};

const handleClean = (ctx) => {
  try {
    deleteOldFiles(ctx);
  } catch (error) {
    console.error("Error during file cleanup:", error);
  }
};

const downloading = async (ctx) => {
  try {
    const { result: ongoingDownloads } = await getOngoingDownloads();

    if (ongoingDownloads.length > 0) {
      let reply = "Ongoing Downloads:\n\n";

      for (const download of ongoingDownloads) {
        const { gid, completedLength, totalLength, status } = download;

        const downloadedSize = (completedLength / 1024 / 1024).toFixed(2);
        const totalSize = (totalLength / 1024 / 1024).toFixed(2);
        const progress = ((completedLength / totalLength) * 100).toFixed(2);

        reply += `ID: /status_${gid}\n`;
        reply += `Status: ${status}\n`;
        reply += `Progress: ${downloadedSize} MB / ${totalSize} MB (${progress}%)\n`;
        reply += `-----------------------------\n\n`;
      }

      ctx.reply(reply);
    } else {
      ctx.reply("No ongoing downloads.");
    }
  } catch (error) {
    console.error(error);
    ctx.reply("Failed to retrieve ongoing downloads. Please try again later.");
  }
};

module.exports = {
  handleAbout,
  handleStart,
  handleStats,
  handleDownload,
  handleStatus,
  handleCancel,
  handleIpData,
  handleClean,
  downloading,
};