/
subscription.ts
129 lines (119 loc) · 5.51 KB
/
subscription.ts
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
import { Route } from '@/types';
import got from '@/utils/got';
import { config } from '@/config';
import ConfigNotFoundError from '@/errors/types/config-not-found';
export const route: Route = {
path: '/subscription/:parameters?',
description: `
1. If no specific parameters are specified, all subscription sources will be output by default.
2. Please obtain the Category ID or Subscription Source ID on the \`Category\` (shortcut \`g\` \`c\`) or \`Source\` (shortcut \`g\` \`f\`) page. The URL of each category (or subscription source) will display its ID information.
3. Support for category names and category IDs, to output multiple categories, please repeat entering \`category=\` and connect with \`&\`, or directly use **English** commas between different category names. For example, you can subscribe through \`/miniflux/subscription/category=technology&category=1\` or \`/miniflux/subscription/categories=technology,1\`.
4. Support specifying the subscription source name or subscription source ID, similar to setting categories. For example, you can subscribe through \`/miniflux/subscription/feed=1&feed=Archdaily\` or \`/miniflux/subscription/feeds=1,Archdaily\`.
5. Support simultaneously specifying subscription source information and category information; it will output subscription sources that meet the selected categories' criteria. Consider an example: by using \`/miniflux/subscription/feeds=1,archdaily&category=art,7\`, if the Subscription Source ID is 1 or the Subscription Source Name is ArchDaily indeed falls under Category 'art' or has a Category ID of 7, then output that subscription source information.
`,
categories: ['other'],
example: '/miniflux/subscription/categories=test',
parameters: {
parameters: 'Category name or category ID or/and subscription source name or subscription source ID',
},
features: {
requireConfig: [
{
name: 'MINIFLUX_INSTANCE',
description: 'The instance used by the user, by default, is the official MiniFlux [paid service address](https://reader.miniflux.app)',
},
{
name: 'MINIFLUX_TOKEN',
description: "User's API key, please log in to the instance used and go to `Settings` -> `API Key` -> `Create a new API key` to obtain.",
},
],
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: 'Subscriptions',
maintainers: ['emdoe', 'DIYgod'],
handler,
};
async function handler(ctx) {
const instance = config.miniflux.instance;
const token = config.miniflux.token;
if (!token) {
throw new ConfigNotFoundError('This RSS feed is disabled due to its incorrect configuration: the token is missing.');
}
function set(item) {
if (item.search('=') === -1) {
return '';
}
const filter = item.substring(0, item.indexOf('='));
const option = item.substring(item.lastIndexOf('=') + 1);
if (filter.search('categor') !== -1) {
option.split(',').map((item) => categories.push(item.toString().toLowerCase()));
return filter;
}
if (filter.search('feed') === -1) {
return '';
} else {
option.split(',').map((item) => feeds.push(item.toString().toLowerCase()));
return filter;
}
}
function addFeed(item) {
subscription.push({
title: item.title,
link: item.site_url,
pubData: item.last_modified_header,
description: 'Feed URL: ' + `<a href=${item.feed_url}>${item.feed_url}</a>`,
});
}
const response = await got.get(`${instance}/v1/feeds`, {
headers: { 'X-Auth-Token': token },
});
const subscription = [];
const categories = [];
const feeds = [];
const feedsList = response.data;
const parameters = ctx.req
.param('parameters')
?.split('&')
.map((parameter) => set(parameter))
.join('');
if (parameters) {
for (const item of feedsList) {
if (categories.length && feeds.length) {
const categoryTitle = item.category.title.toLowerCase();
const categoryID = item.category.id.toString();
const feedID = item.id.toString();
const feedTitle = item.title.toLowerCase();
if ((categories.includes(categoryID) || categories.includes(categoryTitle)) && (feeds.includes(feedID) || feeds.includes(feedTitle))) {
addFeed(item);
}
} else if (categories.length) {
const categoryTitle = item.category.title.toLowerCase();
const categoryID = item.category.id.toString();
if (categories.includes(categoryID) || categories.includes(categoryTitle)) {
addFeed(item);
}
} else if (feeds.length) {
const feedID = item.id.toString();
const feedTitle = item.title.toLowerCase();
if (feeds.includes(feedID) || feeds.includes(feedTitle)) {
addFeed(item);
}
}
}
} else {
for (const item of feedsList) {
addFeed(item);
}
}
return {
title: `MiniFlux | Subscription List`,
link: instance,
description: `A subscription tracking feed.`,
item: subscription,
allowEmpty: true,
};
}