ani-auto 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/notify.js CHANGED
@@ -3,83 +3,106 @@
3
3
  const { execSync } = require('child_process');
4
4
  const { loadConfig } = require('./config');
5
5
 
6
- // Bot token is shared — each user only needs their own chat ID
7
- // Users get their chat ID by messaging @anime_auto_downloader_bot on Telegram
8
6
  const BOT_TOKEN = '8695132702:AAEFDoSL6nkdwDOoz5wmoVkmuhaYa4tNw0o';
9
7
 
10
- // ── Telegram ───────────────────────────────────────────────────────────────
8
+ // ── Telegram helpers ───────────────────────────────────────────────────────
11
9
 
12
- async function sendTelegram(text) {
10
+ async function sendTelegram(payload) {
13
11
  const config = loadConfig();
14
12
  const chatId = config.telegramChatId;
15
- if (!chatId) return;
16
- if (config.telegramNotify === false) return;
13
+ if (!chatId || config.telegramNotify === false) return;
17
14
  try {
18
15
  const fetch = require('node-fetch');
19
- await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
16
+ const isPhoto = !!payload.photo;
17
+ const endpoint = isPhoto ? 'sendPhoto' : 'sendMessage';
18
+ await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/${endpoint}`, {
20
19
  method: 'POST',
21
20
  headers: { 'Content-Type': 'application/json' },
22
- body: JSON.stringify({
23
- chat_id: String(chatId),
24
- text,
25
- parse_mode: 'HTML',
26
- }),
21
+ body: JSON.stringify({ chat_id: String(chatId), parse_mode: 'HTML', ...payload }),
27
22
  });
28
23
  } catch (e) {
29
- // silently fail — never crash the download for a notification
24
+ // silently fail
30
25
  }
31
26
  }
32
27
 
33
- // ── KDE Desktop (notify-send) ──────────────────────────────────────────────
28
+ // ── KDE desktop ────────────────────────────────────────────────────────────
34
29
 
35
- function sendDesktop(title, body, urgency = 'normal') {
30
+ async function sendDesktop(title, body, urgency = 'normal', posterUrl = null) {
36
31
  try {
37
- // KDE uses notify-send from libnotify
38
- execSync(`notify-send -u ${urgency} -a "ani-auto" "${title}" "${body}"`, { stdio: 'ignore' });
39
- } catch (e) {
40
- // silently fail if notify-send not available
41
- }
32
+ let icon = '';
33
+ if (posterUrl) {
34
+ const os = require('os');
35
+ const path = require('path');
36
+ const fs = require('fs');
37
+ const fetch = require('node-fetch');
38
+ const tmp = path.join(os.tmpdir(), 'ani-auto-poster.jpg');
39
+ try {
40
+ const res = await fetch(posterUrl);
41
+ fs.writeFileSync(tmp, await res.buffer());
42
+ icon = `-i "${tmp}"`;
43
+ } catch (e) {}
44
+ }
45
+ execSync(`notify-send -u ${urgency} ${icon} -a "ani-auto" "${title}" "${body}"`, { stdio: 'ignore' });
46
+ } catch (e) {}
42
47
  }
43
48
 
44
- // ── Public notification functions ─────────────────────────────────────────
49
+ // ── Public notification functions ──────────────────────────────────────────
45
50
 
46
- async function notifyEpisode(animeTitle, episode) {
47
- const msg = `✅ <b>${animeTitle}</b>\nEpisode ${episode} downloaded`;
48
- const short = `Episode ${episode} downloaded`;
49
- await sendTelegram(msg);
50
- sendDesktop(animeTitle, short);
51
+ async function notifyEpisode(animeTitle, episode, quality, posterUrl) {
52
+ const caption =
53
+ `<b>${animeTitle}</b>\n` +
54
+ `<code>━━━━━━━━━━━━━━━</code>\n` +
55
+ `✅ Episode ${episode} downloaded\n` +
56
+ `🎞 Quality: ${quality || 'default'}`;
57
+
58
+ if (posterUrl) {
59
+ await sendTelegram({ photo: posterUrl, caption });
60
+ } else {
61
+ await sendTelegram({ text: `🎬 ${caption}` });
62
+ }
63
+ await sendDesktop(animeTitle, `Episode ${episode} downloaded`, 'normal', posterUrl);
51
64
  }
52
65
 
53
66
  async function notifyFailed(animeTitle, episode, error) {
54
- const msg = `❌ <b>${animeTitle}</b>\nEpisode ${episode} failed: ${error}`;
55
- const short = `Episode ${episode} failed`;
56
- await sendTelegram(msg);
57
- sendDesktop(animeTitle, short, 'critical');
67
+ const text =
68
+ `❌ <b>${animeTitle}</b>\n` +
69
+ `<code>━━━━━━━━━━━━━━━</code>\n` +
70
+ `Episode ${episode} failed\n` +
71
+ `<i>${error}</i>`;
72
+ await sendTelegram({ text });
73
+ await sendDesktop(animeTitle, `Episode ${episode} failed`, 'critical');
58
74
  }
59
75
 
60
76
  async function notifySummary(totals) {
61
- if (totals.downloaded === 0 && totals.failed === 0) return; // skip empty runs
62
- const msg =
63
- `📋 <b>ani-auto run complete</b>\n` +
64
- `✅ Downloaded: ${totals.downloaded}\n` +
65
- `⏭ Skipped: ${totals.skipped}\n` +
66
- `❌ Failed: ${totals.failed}`;
67
- const short = `↓${totals.downloaded} ✗${totals.failed}`;
68
- await sendTelegram(msg);
69
- sendDesktop('ani-auto run complete', short);
77
+ if (totals.downloaded === 0 && totals.failed === 0) return;
78
+ const text =
79
+ `📊 <b>ani-auto run complete</b>\n` +
80
+ `<code>━━━━━━━━━━━━━━━</code>\n` +
81
+ `✅ Downloaded: ${totals.downloaded}\n` +
82
+ `⏭ Skipped: ${totals.skipped}\n` +
83
+ `❌ Failed: ${totals.failed}`;
84
+ await sendTelegram({ text });
85
+ await sendDesktop('ani-auto run complete', `Downloaded: ${totals.downloaded} Failed: ${totals.failed}`);
70
86
  }
71
87
 
72
- async function notifyDaemonStart(intervalMinutes) {
73
- const msg = `🚀 <b>ani-auto daemon started</b>\nChecking every ${intervalMinutes} minutes`;
74
- const short = `Checking every ${intervalMinutes} minutes`;
75
- await sendTelegram(msg);
76
- sendDesktop('ani-auto daemon started', short);
88
+ async function notifyDaemonStart(intervalMinutes, downloadingCount) {
89
+ const { version } = require('../package.json');
90
+ const text =
91
+ `🚀 <b>ani-auto daemon started</b>\n` +
92
+ `<code>━━━━━━━━━━━━━━━</code>\n` +
93
+ `📦 Version: v${version}\n` +
94
+ `📺 Downloading: ${downloadingCount} anime\n` +
95
+ `⏱ Fallback check every ${intervalMinutes} minutes`;
96
+ await sendTelegram({ text });
97
+ await sendDesktop(
98
+ `ani-auto v${version} started`,
99
+ `Downloading ${downloadingCount} anime · every ${intervalMinutes} min`
100
+ );
77
101
  }
78
102
 
79
103
  async function notifyDaemonStop() {
80
- const msg = `🛑 <b>ani-auto daemon stopped</b>`;
81
- await sendTelegram(msg);
82
- sendDesktop('ani-auto daemon stopped', '');
104
+ await sendTelegram({ text: '🛑 <b>ani-auto daemon stopped</b>' });
105
+ await sendDesktop('ani-auto daemon stopped', '');
83
106
  }
84
107
 
85
108
  module.exports = {