Skip to content

Commit

Permalink
add proxy support and change axios to got
Browse files Browse the repository at this point in the history
  • Loading branch information
fengkx committed Mar 10, 2019
1 parent 2fb5ec5 commit 7a8e300
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 68 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ for example `docker run --name rssbot -d -e RSSBOT_TOKEN=123456:abcdef123456-U f
| notify_error_count | NOTIFY_ERR_COUNT | 5 | error count when it will notfiy |
| view_all | RSSBOT_VIEW_ALL | false | enable or not |
| UA | RSSBOT_UA | 'Mozilla/5.0 NodeRSSBot(https://github.com/fengkx/NodeRSSBot)' | user-agent of requrest |
| proxy.protocol | PROXY_PROTOCOL | null | proxy protocol http/https/socks |
| proxy.host | PROXY_HOST | null | proxy host |
| proxy.port | PROXY_PORT | null | proxy port |

language can be setting in `zh-cn` or `en`

Expand Down Expand Up @@ -154,7 +157,7 @@ viewall 只能在私聊中使用
# TODO

- [x] export 命令
- 代理
- [x] 代理
- unit test

# 配置项
Expand All @@ -171,6 +174,9 @@ viewall 只能在私聊中使用
| notify_error_count | NOTIFY_ERR_COUNT | 5 | 发出通知的错误次数 |
| view_all | RSSBOT_VIEW_ALL | false | 是否开启 |
| UA | RSSBOT_UA | 'Mozilla/5.0 NodeRSSBot(https://github.com/fengkx/NodeRSSBot)' | 请求的 user-agent |
| proxy.protocool | PROXY_PROTOCOL | null | 代理协议 http/https/socks |
| proxy.host | PROXY_HOST | null | 代理地址 |
| proxy.port | PROXY_PORT | null | 代理端口 |

语言可以设置为 `zh-cn` or `en`
时间间隔可设置为每多少分钟或多少小时。m 表示分钟, h 表示小时
Expand Down
6 changes: 5 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const path = require('path');

module.exports = {
token: process.env.RSSBOT_TOKEN || '',
socks_proxy: process.env.socks_proxy || undefined,
proxy: {
protocol: process.env.PROXY_PROTOCOL || null,
host: process.env.PROXY_HOST || null,
port: process.env.PROXY_PORT || null
},
db_path:
process.env.RSSBOT_DB_PATH ||
path.join(__dirname, '../data/database.db'),
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const {
const bot = new Telegraf(token, {
telegram: {
// Telegram options
// agent: agent, // https.Agent instance, allows custom proxy, certificate, keep alive, etc.
agent: require('./utils/agent') // https.Agent instance, allows custom proxy, certificate, keep alive, etc.
}
});

Expand Down
6 changes: 3 additions & 3 deletions middlewares/importFromOpml.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const axios = require('axios');
const got = require('got');
const Parser = require('xml2js').Parser;
const logger = require('../utils/logger');
const RSS = require('../proxies/rssFeed');
Expand Down Expand Up @@ -28,8 +28,8 @@ module.exports = async (ctx, next) => {
const { fileLink } = ctx.state;

try {
const res = await axios.get(fileLink);
const opmlStr = res.data;
const res = await got.get(fileLink);
const opmlStr = res.body;
const outlines = await getOutlines(opmlStr);
ctx.state.outlines = outlines;
await Promise.all(
Expand Down
6 changes: 3 additions & 3 deletions middlewares/subMultiUrl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const axios = require('../utils/axios');
const got = require('../utils/got');
const Parser = require('rss-parser');
const RSS = require('../proxies/rssFeed');
const i18n = require('../i18n');
Expand All @@ -14,8 +14,8 @@ module.exports = async (ctx, next) => {
} else {
try {
const parser = new Parser();
const res = await axios.get(encodeURI(url));
const rssFeed = await parser.parseString(res.data);
const res = await got.get(encodeURI(url));
const rssFeed = await parser.parseString(res.body);
return {
feed_title: rssFeed.title,
url
Expand Down
12 changes: 6 additions & 6 deletions middlewares/testUrl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const axios = require('../utils/axios');
const got = require('../utils/got');
const Parser = require('rss-parser');
const RSS = require('../proxies/rssFeed');

Expand All @@ -11,17 +11,17 @@ module.exports = async (ctx, next) => {
await next();
} else {
try {
const res = await axios.get(url);
const res = await got.get(url);
const parser = new Parser();
let feed = await parser.parseString(res.data);
let feed = await parser.parseString(res.body);
delete feed.items;
ctx.state.feed = feed;
} catch (e) {
if (e.respone) {
switch (e.respone.status) {
if (e.response) {
switch (e.response.status) {
case 404:
case 403:
throw new Error(e.respone.status);
throw new Error(e.response.status);
default:
throw new Error('FETCH_ERROR');
}
Expand Down
Loading

0 comments on commit 7a8e300

Please sign in to comment.