Skip to content

Commit

Permalink
rss: add telegram sticker pack
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Aug 22, 2018
1 parent 0ad6bcf commit e73f80b
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
- 最热/最新主题
- Telegram
- 频道
- 贴纸包
- Readhub
- 分类
- GitHub
Expand Down
Empty file added cache/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ module.exports = {
github: {
access_token: process.env.GITHUB_ACCESS_TOKEN,
},
imgur: {
clientId: process.env.IMGUR_CLIRNT_ID,
},
};
8 changes: 8 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,14 @@ key: 产品密钥

参数: username,频道 username

### 贴纸包

举例: [https://rsshub.app/telegram/stickerpack/DIYgod](https://rsshub.app/telegram/stickerpack/DIYgod)

路由: `/telegram/stickerpack/:name`

参数: name,贴纸包 id,可在分享贴纸获得的 URL 中找到

## Readhub

### 分类 <Author uid="WhiteWorld"/>
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"git-rev-sync": "1.12.0",
"googleapis": "32.0.0",
"iconv-lite": "0.4.23",
"imgur": "^0.2.1",
"json-bigint": "0.3.0",
"koa": "2.5.2",
"koa-favicon": "2.0.1",
Expand All @@ -62,6 +63,7 @@
"readall": "1.0.0",
"redis": "2.8.0",
"rss-parser": "3.4.3",
"sharp": "^0.20.7",
"twit": "2.2.11",
"winston": "3.0.0"
},
Expand Down
5 changes: 5 additions & 0 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ router.get('/v2ex/topics/:type', require('./routes/v2ex/topics'));
// Telegram
if (config.telegram && config.telegram.token) {
router.get('/telegram/channel/:username', require('./routes/telegram/channel'));
if (config.imgur && config.imgur.clientId) {
router.get('/telegram/stickerpack/:name', require('./routes/telegram/stickerpack'));
} else {
logger.warn('Telegram Sticker Pack RSS is disabled for lacking config.');
}
} else {
logger.warn('Telegram RSS is disabled for lacking config.');
}
Expand Down
76 changes: 76 additions & 0 deletions routes/telegram/stickerpack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const axios = require('../../utils/axios');
const config = require('../../config');
const logger = require('../../utils/logger');
const sharp = require('sharp');
const imgur = require('imgur');
const fs = require('fs');

if (config.imgur && config.imgur.clientId) {
imgur.setClientId(config.imgur.clientId);
}

module.exports = async (ctx) => {
const name = ctx.params.name;
const cacheDays = 30;

const response = await axios({
method: 'get',
url: `https://api.telegram.org/bot${config.telegram.token}/getStickerSet?name=${name}`,
});

const data = response.data.result;

const links = await Promise.all(
data.stickers.map(async (item) => {
const key = `telegramstickerpack${item.file_id}`;
const cache = await ctx.cache.get(key);
if (cache) {
return Promise.resolve(cache);
} else {
const pathResponse = await axios({
method: 'get',
url: `https://api.telegram.org/bot${config.telegram.token}/getFile?file_id=${item.file_id}`,
});
const path = pathResponse.data.result.file_path;

const fileResponse = await axios({
method: 'get',
url: `https://api.telegram.org/file/bot${config.telegram.token}/${path}`,
responseType: 'arraybuffer',
});

const filePath = `cache/${item.file_id}.png`;

await sharp(fileResponse.data)
.png()
.toFile(filePath);

let imgurResponse;
try {
imgurResponse = await imgur.uploadFile(filePath);
} catch (e) {
logger.error(`Error in imgur upload ${filePath}`, e);
}
const link = imgurResponse.data.link;

fs.unlink(filePath, (e) => {
logger.error(`Error in remove ${filePath}`, e);
});

ctx.cache.set(key, link, cacheDays * 24 * 60 * 60);
return Promise.resolve(link);
}
})
);

ctx.state.data = {
title: `${data.title} - Telegram Sticker Pack`,
link: `https://t.me/addstickers/${name}`,
item: data.stickers.map((item, index) => ({
title: item.emoji,
description: `<img referrerpolicy="no-referrer" src="${links[index]}">`,
guid: item.file_id,
link: links[index],
})),
};
};
Loading

0 comments on commit e73f80b

Please sign in to comment.