Skip to content

Commit

Permalink
feat(route): bilibili/user/article (DIYgod#14522)
Browse files Browse the repository at this point in the history
* bilibili/user/article fix DIYgod#14231

* Update cache.js

delete UA

* using INITIAL_STATE data

* resolve code review

* Update lib/v2/bilibili/cache.js

---------
  • Loading branch information
Qixingchen authored Feb 23, 2024
1 parent 65ba168 commit 170b837
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
36 changes: 34 additions & 2 deletions lib/v2/bilibili/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ module.exports = {
return aid;
},
getArticleDataFromCvid: async (ctx, cvid, uid) => {
const url = `https://www.bilibili.com/read/cv${cvid}`;
const url = `https://www.bilibili.com/read/cv${cvid}/`;
const data = await ctx.cache.tryGet(
url,
async () =>
Expand All @@ -218,7 +218,39 @@ module.exports = {
).data
);
const $ = cheerio.load(data);
const description = $('#read-article-holder').html();
let description = $('#read-article-holder').html();
if (!description) {
try {
const newFormatData = JSON.parse(
$('script:contains("window.__INITIAL_STATE__")')
.text()
.match(/window\.__INITIAL_STATE__\s*=\s*(.*?);\(/)[1]
);

if (newFormatData?.readInfo?.opus?.content?.paragraphs) {
description = '';
for (const element of newFormatData.readInfo.opus.content.paragraphs) {
if (element.para_type === 1) {
for (const text of element.text.nodes) {
if (text?.word?.words) {
description += `<p>${text.word.words}</p>`;
}
}
}
if (element.para_type === 2) {
for (const image of element.pic.pics) {
description += `<p ><img src="${image.url}@progressive.webp"></p>`;
}
}
if (element.para_type === 3 && element.line?.pic?.url) {
description += `<figure><img src="${element.line.pic.url}"></figure>`;
}
}
}
} catch {
/* empty */
}
}
return { url, description };
},
};
2 changes: 1 addition & 1 deletion lib/v2/bilibili/maintainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
'/popular/all': ['ziminliu'],
'/ranking/:rid?/:day?/:arc_type?/:disableEmbed?': ['DIYgod'],
'/readlist/:listid': ['hoilc'],
'/user/article/:uid': ['lengthmin'],
'/user/article/:uid': ['lengthmin', 'Qixingchen'],
'/user/bangumi/:uid/:type?': ['wdssmq'],
'/user/channel/:uid/:sid/:disableEmbed?': ['weirongxu'],
'/user/coin/:uid/:disableEmbed?': ['DIYgod'],
Expand Down

0 comments on commit 170b837

Please sign in to comment.