Skip to content

Commit 77f7ba7

Browse files
committed
feat: support webm
1 parent b25f721 commit 77f7ba7

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

lib/action/picture.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,50 @@
11
import type { Context, Telegram } from 'telegraf';
22

3-
import { craftExtraPhoto } from '#action/utils.js';
3+
import { craftExtra } from '#action/utils.js';
44
import type { Picture } from '#types/index.js';
55
import * as user from '#user/index.js';
66
import * as wiki from '#wiki/index.js';
77

8+
function isVideo(pic: Picture) {
9+
return pic.url.endsWith('webm');
10+
}
11+
812
export async function replyPotd(ctx: Context, date: string) {
913
try {
1014
const pic_src = await user.getPicSource(ctx.message!.from.id);
1115
if (pic_src === null) throw new Error('picture source is null');
1216

1317
const pic = await wiki.getPotd(date, pic_src);
18+
const action = isVideo(pic) ?
19+
ctx.replyWithVideo(pic.url, craftExtra(pic)) :
20+
ctx.replyWithPhoto(pic.url, craftExtra(pic));
1421

15-
await ctx.replyWithPhoto(pic.url, craftExtraPhoto(pic));
22+
await action;
1623
} catch (err) {
1724
await ctx.reply('An error occurred internally!');
1825
console.error(err.message);
1926
}
2027
}
2128

2229
export async function sendPicture(bot: Telegram, user_id: number, pic: Picture) {
23-
return bot.sendPhoto(user_id, pic.url, craftExtraPhoto(pic))
24-
.catch(async err => {
25-
switch (err.response.description) {
26-
case 'Forbidden: bot was blocked by the user':
27-
if (await user.setBlockedBot(user_id)) return;
28-
console.error(`Failed to mark the bot is blocked by the user: ${user_id}.`);
29-
break;
30-
case 'Forbidden: user is deactivated':
31-
if (await user.unsubscribe(user_id)) return;
32-
console.error(`Failed to unsubscribe for the user: ${user_id}.`);
33-
break;
34-
default:
35-
console.error(`An error occurred while sending a picture to the user: '${user_id}'.`);
36-
console.error(err);
37-
}
38-
});
30+
try {
31+
const action = isVideo(pic) ?
32+
bot.sendVideo(user_id, pic.url, craftExtra(pic)) :
33+
bot.sendPhoto(user_id, pic.url, craftExtra(pic));
34+
await action;
35+
} catch (err) {
36+
switch (err.response.description) {
37+
case 'Forbidden: bot was blocked by the user':
38+
if (await user.setBlockedBot(user_id)) return;
39+
console.error(`Failed to mark the bot is blocked by the user: ${user_id}.`);
40+
break;
41+
case 'Forbidden: user is deactivated':
42+
if (await user.unsubscribe(user_id)) return;
43+
console.error(`Failed to unsubscribe for the user: ${user_id}.`);
44+
break;
45+
default:
46+
console.error(`An error occurred while sending a picture to the user: '${user_id}'.`);
47+
console.error(err);
48+
}
49+
}
3950
}

lib/action/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export function craftMenu(subscribed: boolean, pic_src: PicSource) {
2323
return menu;
2424
}
2525

26-
export function craftExtraPhoto(pic: Picture) {
27-
let extra: Types.ExtraPhoto = { parse_mode: 'HTML' };
26+
export function craftExtra(pic: Picture) {
27+
let extra: Types.ExtraPhoto | Types.ExtraVideo = { parse_mode: 'HTML' };
2828
let caption = `[${pic.date}]\n\n`;
29-
// NOTE: character limit of photo caption is 1024
29+
// NOTE: character limit of photo/video caption is 1024
3030
const res = utils.paginate(pic.caption, 960, []);
3131

3232
if (res.end_idx < pic.caption.length) {

lib/wiki/potd.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ export async function getUrlOfPotd(date: string, src: PicSource) {
1616
const img_url = await req.getImageUrl(filename, src);
1717
let segments = img_url.split('/');
1818

19-
segments.splice(5, 0, 'thumb');
19+
if (filename.endsWith('.webm')) {
20+
segments.splice(5, 0, 'transcoded');
2021

21-
let last_part = `1024px-${ segments[8] }`;
22-
if (filename.endsWith('.svg')) {
23-
last_part += '.png';
22+
let last_part = `${ segments[8] }.1080p.vp9.webm`;
23+
segments.push(last_part);
24+
} else {
25+
segments.splice(5, 0, 'thumb');
26+
27+
let last_part = `1024px-${ segments[8] }`;
28+
if (filename.endsWith('.svg')) {
29+
last_part += '.png';
30+
}
31+
segments.push(last_part);
2432
}
25-
segments.push(last_part);
2633

2734
return segments.join('/');
2835
}

0 commit comments

Comments
 (0)