diff --git a/.gitignore b/.gitignore index 76add87..a4613e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ node_modules -dist \ No newline at end of file +dist +.env +.wrangler +.github +.vscode +.idea +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 0695032..1255889 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,15 @@ RSSWorker 是一个轻量级的 RSS 订阅工具,可以部署在 Cloudflare Wo > 网页端:用户页面 > 链接中的用户ID > 格式:https://www.xiaohongshu.com/user/profile/5d2aec020000000012037401 +> 微博更新后需要加上Cookie +> 获取方法(参考 https://docs.rsshub.app/zh/deploy/config#%E5%BE%AE%E5%8D%9A ) : +> 1. 打开并登录微博 +> 2. 从个人微博主页的网址中获取uid,在`https://m.weibo.cn/api/container/getIndex?type=uid&value=`后追加uid,访问该链接 +> 2. 按下F12打开控制台,切换至Network(网络)面板 +> 3. 在该网页切换至任意关注分组,并在面板打开最先捕获到的请求 (该情形下捕获到的请求路径应包含/feed/group) +> 4. 查看该请求的Headers(请求头), 找到Cookie字段并复制内容 +> 5. 命令行中输入`wrangler secret put WEIBO_COOKIE`,按下回车后再将第4步中复制的Cookie字段粘贴,后按下回车 + ## 部署 [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/yllhwa/RSSWorker) diff --git a/src/lib/weibo/user.js b/src/lib/weibo/user.js index 0affc47..1ac81ad 100644 --- a/src/lib/weibo/user.js +++ b/src/lib/weibo/user.js @@ -11,6 +11,10 @@ let deal = async (ctx) => { const containerData = await fetch(`https://m.weibo.cn/api/container/getIndex?type=uid&value=${uid}`, { headers: { Referer: `https://m.weibo.cn/u/${uid}`, + Cookie: ctx.env.WEIBO_COOKIE || '', + Accept: 'application/json, text/plain, */*', + 'User-Agent': + 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', 'MWeibo-Pwa': 1, 'X-Requested-With': 'XMLHttpRequest', }, @@ -24,6 +28,10 @@ let deal = async (ctx) => { const cards = await fetch(`https://m.weibo.cn/api/container/getIndex?type=uid&value=${uid}&containerid=${containerId}`, { headers: { Referer: `https://m.weibo.cn/u/${uid}`, + Cookie: ctx.env.WEIBO_COOKIE || '', + Accept: 'application/json, text/plain, */*', + 'User-Agent': + 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', 'MWeibo-Pwa': 1, 'X-Requested-With': 'XMLHttpRequest', }, @@ -40,7 +48,7 @@ let deal = async (ctx) => { // TODO: getShowData() on demand? The API seems to return most things we need since 2022/05/21. // Need more investigation, pending for now since the current version works fine. // const data = await ctx.cache.tryGet(key, () => weiboUtils.getShowData(uid, item.mblog.bid)); - const data = await weiboUtils.getShowData(uid, item.mblog.bid); + const data = await weiboUtils.getShowData(ctx, uid, item.mblog.bid); if (data && data.text) { item.mblog.text = data.text; @@ -60,7 +68,7 @@ let deal = async (ctx) => { // const retweetData = await ctx.cache.tryGet(`weibo:retweeted:${retweet.user.id}:${retweet.bid}`, () => // weiboUtils.getShowData(retweet.user.id, retweet.bid) // ); - const retweetData = await weiboUtils.getShowData(retweet.user.id, retweet.bid); + const retweetData = await weiboUtils.getShowData(ctx, retweet.user.id, retweet.bid); if (retweetData !== undefined && retweetData.text) { item.mblog.retweeted_status.text = retweetData.text; } @@ -99,7 +107,7 @@ let deal = async (ctx) => { description, isPinned: item.profile_type_id?.startsWith('proweibotop'), }; - }) + }), ); // remove pinned weibo if they are too old (older than all the rest weibo) diff --git a/src/lib/weibo/utils.js b/src/lib/weibo/utils.js index 67ea3fe..4f1dff7 100644 --- a/src/lib/weibo/utils.js +++ b/src/lib/weibo/utils.js @@ -60,14 +60,14 @@ const weiboUtils = { if (!showEmojiInDescription) { htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace( /]*?alt=["']?([^>]+?)["']?\s[^>]*?\/><\/span>/g, - '$1' + '$1', ); } // 去掉链接的图标,保留 a 标签链接 if (!showLinkIconInDescription) { htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace( /(]*>)]*><\/span>[^<>]*?([^<>]*?)<\/span><\/a>/g, - '$1$2' + '$1$2', ); } // 去掉乱七八糟的图标 // 不需要,上述的替换应该已经把所有的图标都替换掉了,且这条 regex 会破坏上述替换不发生时的输出 @@ -80,7 +80,7 @@ const weiboUtils = { // 处理外部链接 htmlNewLineUnreplaced = htmlNewLineUnreplaced.replace(/https:\/\/weibo\.cn\/sinaurl\/.*?&u=(http.*?")/g, (match, p1) => - decodeURIComponent(p1) + decodeURIComponent(p1), ); let html = htmlNewLineUnreplaced.replace(/\n/g, '
'); @@ -130,7 +130,7 @@ const weiboUtils = { // drop live photo const livePhotoCount = status.pics ? status.pics.filter((pic) => pic.type === 'livephotos').length : 0; - const pics = status.pics && status.pics.filter((pic) => (pic.type !== 'livephotos') && (pic.type !== 'livephoto')); + const pics = status.pics && status.pics.filter((pic) => pic.type !== 'livephotos' && pic.type !== 'livephoto'); // 添加微博配图 if (pics) { @@ -246,13 +246,14 @@ const weiboUtils = { return { description: html, title, link, guid, author, pubDate }; }, - getShowData: async (uid, bid) => { + getShowData: async (ctx, uid, bid) => { const link = `https://m.weibo.cn/statuses/show?id=${bid}`; const itemResponse = await fetch(link, { headers: { Referer: `https://m.weibo.cn/u/${uid}`, 'MWeibo-Pwa': 1, 'X-Requested-With': 'XMLHttpRequest', + Cookie: ctx.env.WEIBO_COOKIE || '', 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', }, @@ -325,6 +326,7 @@ const weiboUtils = { const response = await fetch(link, { headers: { Referer: `https://card.weibo.com/article/m/show/id/${articleId}`, + Cookie: ctx.env.WEIBO_COOKIE || '', 'MWeibo-Pwa': 1, 'X-Requested-With': 'XMLHttpRequest', 'User-Agent': @@ -383,7 +385,7 @@ const weiboUtils = { element: (elem) => { elem.setAttribute( 'style', - 'display: table;text-align: center;margin-left: auto;margin-right: auto;clear: both;min-width: 50px;' + 'display: table;text-align: center;margin-left: auto;margin-right: auto;clear: both;min-width: 50px;', ); }, }) @@ -409,6 +411,7 @@ const weiboUtils = { const response = await fetch(link, { headers: { Referer: `https://m.weibo.cn/detail/${id}`, + Cookie: ctx.env.WEIBO_COOKIE || '', 'MWeibo-Pwa': 1, 'X-Requested-With': 'XMLHttpRequest', 'User-Agent': diff --git a/wrangler.toml b/wrangler.toml index df66fd4..3a01e9d 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,7 +1,7 @@ name = "rss-worker" main = "src/worker.js" -node_compat = true -compatibility_date = "2023-09-22" +compatibility_date = "2024-09-23" +compatibility_flags = ["nodejs_compat"] # [build] # command = "npm run build"