Skip to content

Commit

Permalink
fix(feed_url): ✨merge redirected url
Browse files Browse the repository at this point in the history
  • Loading branch information
fengkx committed Jun 10, 2019
1 parent cecab8f commit 26c3b70
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions controlers/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ ctrl.rss = async (ctx, next) => {
throw new Error('NOT_SUB');
}
let builder = [];

builder.push(`<strong>${i18n['SUB_LIST']}</strong>`);
if (raw) {
feeds.forEach((feed) => {
Expand Down
2 changes: 2 additions & 0 deletions middlewares/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = async (ctx, next) => {
} else {
try {
const res = await got.get(url);
// handle redirect
ctx.state.feedUrl = decodeURI(res.url);
const parser = new Parser();
let feed = await parser.parseString(res.body);
delete feed.items;
Expand Down
27 changes: 27 additions & 0 deletions proxies/rssFeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,31 @@ px.getAllFeedsCount = async () => {
}
};

px.handleRedirect = async (url, realUrl) => {
try {
const db = await dbPomise;
const oldFeed = await db.get(`SELECT * FROM rss_feed WHERE url=?`, url);
const realFeed = await db.get(
`SELECT * FROM rss_feed WHERE url=?`,
realUrl
);
if (realFeed) {
await db.run(
`UPDATE subscribes SET feed_id=? WHERE feed_id=?`,
realFeed.feed_id,
oldFeed.feed_id
);
await db.run(`DELETE FROM rss_feed WHERE url=?`, oldFeed.url);
} else {
await db.run(
`UPDATE rss_feed SET url=? WHERE url=?`,
realUrl,
oldFeed.url
);
}
} catch (e) {
throw new Error('DB_ERROR');
}
};

module.exports = px;
7 changes: 6 additions & 1 deletion utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
updateHashList,
failAttempt,
getFeedByUrl,
resetErrorCount
resetErrorCount,
handleRedirect
} = require('../proxies/rssFeed');
const {
notify_error_count,
Expand All @@ -23,6 +24,10 @@ const fetch = async (feedUrl) => {
try {
logger.debug(`fetching ${feedUrl}`);
const res = await got.get(encodeURI(feedUrl));
// handle redirect
if (encodeURI(feedUrl) !== res.url) {
await handleRedirect(feedUrl, decodeURI(res.url));
}
const parser = new Parser();
const feed = await parser.parseString(res.body);
const items = feed.items.slice(0, item_num);
Expand Down

0 comments on commit 26c3b70

Please sign in to comment.