Skip to content

Commit

Permalink
feat(route): support medium (DIYgod#12475)
Browse files Browse the repository at this point in the history
* feat(route): support medium

* docs(route): support medium

* feat(medium): add radar.js

* Update docs/blog.md

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update docs/blog.md

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update docs/blog.md

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update docs/blog.md

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update docs/en/blog.md

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update lib/v2/medium/tag.js

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update lib/v2/medium/router.js

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update lib/v2/medium/maintainer.js

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update docs/en/blog.md

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update docs/en/blog.md

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update docs/en/blog.md

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update lib/v2/medium/following.js

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update lib/v2/medium/for-you.js

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* Update lib/v2/medium/list.js

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* chore(medium): update graphql

---------

Co-authored-by: Tony <TonyRL@users.noreply.github.com>
  • Loading branch information
ImSingee and TonyRL authored May 9, 2023
1 parent 7eb6400 commit 32a35f2
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,60 @@ username 为博主用户名,而非`xxx.hashnode.dev`中`xxx`所代表的 blog

<RouteEn author="5upernova-heng" example="/macmenubar/recently/developer-apps,system-tools" path="/macmenubar/recently/:category?" :paramsDesc="['分类名,多个使用逗号隔开,留空则为全部。分类名可在 URL 中找到']" radar="1" />

## Medium

### List

<Route author="ImSingee" example="/medium/list/imsingee/f2d8d48096a9" path="/medium/list/:user/:catalogId" :paramsDesc="['用户名', 'List 的 ID']">

List ID 取的是网址中最后一部分 `-` 后面的内容,例如 `https://medium.com/@imsingee/list/collection-7e67004f23f9` 的用户名为 imsingee、ID 为 `7e67004f23f9`

::: warning 注意

想要获取 Private 的 List 则只支持自建

:::

</Route>

### 个性推荐 - For You

<Route author="ImSingee" example="/medium/for-you/imsingee" path="/medium/for-you/:user" :paramsDesc="['用户名']" selfhost="1">

::: warning 注意

个性推荐需要登录后的 Cookie 值,所以只能自建,详情见部署页面的配置模块。

:::

</Route>

### 个性推荐 - Following

<Route author="ImSingee" example="/medium/following/imsingee" path="/medium/following/:user" :paramsDesc="['用户名']" selfhost="1">

::: warning 注意

个性推荐需要登录后的 Cookie 值,所以只能自建,详情见部署页面的配置模块。

:::

</Route>

### 个性推荐 - Tag

<Route author="ImSingee" example="/medium/tag/imsingee/cybersecurity" path="/medium/tag/:user/:tag" :paramsDesc="['用户名', '订阅的 Tag']" selfhost="1">

Tag 有很多,可从首页点进 Tag 以后的 URL 获取,例如 `https://medium.com/?tag=web3` 则 tag 为 `web3`

::: warning 注意

个性推荐需要登录后的 Cookie 值,所以只能自建,详情见部署页面的配置模块。

:::

</Route>

## Miris Whispers

### 博客
Expand Down
54 changes: 54 additions & 0 deletions docs/en/blog.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,60 @@ pageClass: routes

<RouteEn author="5upernova-heng" example="/macmenubar/recently/developer-apps,system-tools" path="/macmenubar/recently/:category?" :paramsDesc="['Category path name, seperate by comma, default is all categories. Category path name can be found in url']" radar="1" />

## Medium

### List

<RouteEn author="ImSingee" example="/medium/list/imsingee/f2d8d48096a9" path="/medium/list/:user/:catalogId" :paramsDesc="['Username', 'List ID']">

The List ID is the last part of the URL after `-`, for example, the username in "https://medium.com/@imsingee/list/collection-7e67004f23f9" is `imsingee`, and the ID is `7e67004f23f9`.

::: warning Note

To access private lists, only self-hosting is supported.

:::

</RouteEn>

### Personalized Recommendations - For You

<RouteEn author="ImSingee" example="/medium/for-you/imsingee" path="/medium/for-you/:user" :paramsDesc="['Username']" selfhost="1">

::: warning Note

Personalized recommendations require the cookie value after logging in, so only self-hosting is supported. See the configuration module on the deployment page for details.

:::

</RouteEn>

### Personalized Recommendations - Following

<RouteEn author="ImSingee" example="/medium/following/imsingee" path="/medium/following/:user" :paramsDesc="['Username']" selfhost="1">

::: warning Note

Personalized recommendations require the cookie value after logging in, so only self-hosting is supported. See the configuration module on the deployment page for details.

:::

</RouteEn>

### Personalized Recommendations - Tag

<RouteEn author="ImSingee" example="/medium/tag/imsingee/cybersecurity" path="/medium/tag/:user/:tag" :paramsDesc="['Username', 'Subscribed Tag']" selfhost="1">

There are many tags, which can be obtained by clicking on a tag from the homepage and looking at the URL. For example, if the URL is `https://medium.com/?tag=web3`, then the tag is `web3`.

::: warning Note

Personalized recommendations require the cookie value after logging in, so only self-hosting is supported. See the configuration module on the deployment page for details.

:::

</RouteEn>

## Miris Whispers

### Blog
Expand Down
5 changes: 5 additions & 0 deletions docs/en/install/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,11 @@ See docs of the specified route and `lib/config.js` for detailed information.
- `MASTODON_API_ACCESS_TOKEN`: user access token
- `MASTODON_API_ACCT_DOMAIN`: acct domain for particular instance
- Medium related routes: Open the console, copy the cookie (in theory, only uid and sid are required)
- `MEDIUM_ARTICLE_COOKIE`: Cookie used when requesting the full article, can access the full text of paid content when there is an active Member subscription.
- `MEDIUM_COOKIE_{username}`: Cookie of the user corresponding to the username, required for personalized recommendation related routes.
- nhentai torrent: [Registration](https://nhentai.net/register/)
- `NHENTAI_USERNAME`: nhentai username or email
Expand Down
5 changes: 5 additions & 0 deletions docs/install/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,11 @@ RSSHub 支持使用访问密钥 / 码,白名单和黑名单三种方式进行
- `MASTODON_API_ACCESS_TOKEN`: 用户 access token, 申请应用后,在应用配置页可以看到申请者的 access token
- `MASTODON_API_ACCT_DOMAIN`: 该实例本地用户 acct 标识的域名

- Medium 相关路由:打开控制台,复制 Cookie(理论上只需要 uid 和 sid 即可)

- `MEDIUM_ARTICLE_COOKIE`:请求全文时使用的 Cookie,存在活跃的 Member 订阅时可获取付费内容全文
- `MEDIUM_COOKIE_{username}`:对应 username 的用户的 Cookie,个性推荐相关路由需要

- MiniFlux 全部路由:

- `MINIFLUX_INSTANCE`: 用户所用的实例,默认为 MiniFlux 官方提供的 [付费服务地址](https://reader.miniflux.app)
Expand Down
8 changes: 8 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const calculateValue = () => {
const twitter_tokens = {};
const email_config = {};
const discuz_cookies = {};
const medium_cookies = {};

for (const name in envs) {
if (name.startsWith('BILIBILI_COOKIE_')) {
Expand All @@ -23,6 +24,9 @@ const calculateValue = () => {
} else if (name.startsWith('DISCUZ_COOKIE_')) {
const cid = name.slice(14);
discuz_cookies[cid] = envs[name];
} else if (name.startsWith('MEDIUM_COOKIE_')) {
const username = name.slice(14).toLowerCase();
medium_cookies[username] = envs[name];
}
}

Expand Down Expand Up @@ -208,6 +212,10 @@ const calculateValue = () => {
accessToken: envs.MASTODON_API_ACCESS_TOKEN,
acctDomain: envs.MASTODON_API_ACCT_DOMAIN,
},
medium: {
cookies: medium_cookies,
articleCookie: envs.MEDIUM_ARTICLE_COOKIE || '',
},
miniflux: {
instance: envs.MINIFLUX_INSTANCE || 'https://reader.miniflux.app',
token: envs.MINIFLUX_TOKEN || '',
Expand Down
31 changes: 31 additions & 0 deletions lib/v2/medium/following.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const config = require('@/config').value;

const parseArticle = require('./parse-article.js');
const { getFollowingFeedQuery } = require('./graphql.js');

module.exports = async (ctx) => {
const user = ctx.params.user;

const cookie = config.medium.cookies[user];
if (cookie === undefined) {
throw Error(`缺少 Medium 用户 ${user} 登录后的 Cookie 值`);
}

const posts = await getFollowingFeedQuery(user, cookie);
ctx.state.json = posts;

if (!posts) {
// login failed
throw Error(`Medium 用户 ${user} 的 Cookie 无效或已过期`);
}

const urls = posts.items.map((data) => data.post.mediumUrl);

const parsedArticles = await Promise.all(urls.map((url) => parseArticle(ctx, url)));

ctx.state.data = {
title: `${user} Medium Following`,
link: 'https://medium.com/?feed=following',
item: parsedArticles,
};
};
31 changes: 31 additions & 0 deletions lib/v2/medium/for-you.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const config = require('@/config').value;

const parseArticle = require('./parse-article.js');
const { getWebInlineRecommendedFeedQuery } = require('./graphql.js');

module.exports = async (ctx) => {
const user = ctx.params.user;

const cookie = config.medium.cookies[user];
if (cookie === undefined) {
throw Error(`缺少 Medium 用户 ${user} 登录后的 Cookie 值`);
}

const posts = await getWebInlineRecommendedFeedQuery(user, cookie);
ctx.state.json = posts;

if (!posts) {
// login failed
throw Error(`Medium 用户 ${user} 的 Cookie 无效或已过期`);
}

const urls = posts.items.map((data) => data.post.mediumUrl);

const parsedArticles = await Promise.all(urls.map((url) => parseArticle(ctx, url)));

ctx.state.data = {
title: `${user} Medium For You`,
link: 'https://medium.com/',
item: parsedArticles,
};
};
Loading

0 comments on commit 32a35f2

Please sign in to comment.