如何从 API 检索用户的 Medium 故事?

How to retrieve Medium stories for a user from the API?

我正在尝试通过显示一些带有帖子图片和指向原始 Medium 出版物的链接的卡片,将 Medium 博客集成到应用程序中。

从 Medium API 文档我可以看到如何检索出版物和创建帖子,但它没有提到检索帖子。是否可以使用媒体的 API 检索当前用户的 posts/stories?

使用该 REST 方法,您将执行此操作:GET https://api.medium.com/v1/users/{{userId}}/publications,这将 return 标题、图像和项目的 URL。 更多详细信息:https://github.com/Medium/medium-api-docs#32-publications

您还可以在 Medium 上任何 URL 的末尾添加“?format=json”并获取有用的数据。

API 是只写的,不用于检索帖子(Medium 工作人员告诉我)

您可以直接使用 RSS 提要:

https://medium.com/feed/@your_profile

您可以简单地通过 GET 获取 RSS 提要,然后如果您需要 JSON 格式,只需使用像 rss-to-json 这样的 NPM 模块就可以了。

如果有人感兴趣,我已经使用 AWS Lambda 和 AWS API 网关构建了一个基本功能。 this blog post here and the repository for the the Lambda function built with Node.js is found here on Github 上有详细的解释。希望这里有人觉得它有用。

勾选这个你会得到关于你自己的所有信息post.......

mediumController.getBlogs = (req, res) => {
    parser('https://medium.com/feed/@profileName', function (err, rss) {
        if (err) {
            console.log(err);
        }

        var stories = [];

        for (var i = rss.length - 1; i >= 0; i--) {

            var new_story = {};

            new_story.title = rss[i].title;
            new_story.description = rss[i].description;
            new_story.date = rss[i].date;
            new_story.link = rss[i].link;
            new_story.author = rss[i].author;
            new_story.comments = rss[i].comments;

            stories.push(new_story);
        }
        console.log('stories:');
        console.dir(stories);
        res.json(200, {
            Data: stories
        })
    });

}

(更新 JS Fiddle 和解释它的 Clay 函数,因为我们更新函数语法更清晰)

我将下面提到的 Github 包 @mark-fasel 包装到一个 Clay 微服务中,使您能够做到这一点:

简化的Return格式:https://www.clay.run/services/nicoslepicos/medium-get-user-posts-new/code

我整理了一些 fiddle,因为一位用户询问如何使用 HTML 中的端点来获取他们最近 3 篇文章的标题: https://jsfiddle.net/h405m3ma/3/

您可以将 API 称为:

curl -i -H "Content-Type: application/json" -X POST -d '{"username":"nicolaerusan"}' https://clay.run/services/nicoslepicos/medium-get-users-posts-simple

您还可以使用 clay-client npm 包在您的节点代码中轻松使用它,只需编写:

Clay.run('nicoslepicos/medium-get-user-posts-new', {"profile":"profileValue"})
.then((result) => {

  // Do what you want with returned result
  console.log(result);

})
.catch((error) => {

  console.log(error);

});

希望对您有所帮助!

编辑:

可以向以下URL提出请求,您将得到答复。不幸的是,响应是 RSS 格式,如果需要,需要对 JSON 进行一些解析。

https://medium.com/feed/@yourhandle

⚠️ 以下方法不再适用,因为它在 Cloudflare 的 DDoS 保护之后。

如果您打算使用 JavaScript 或 jQuery 或 Angular 等从客户端获取它,那么您需要构建一个 API 网关或为您的提要提供服务的网络服务。在 PHP、RoR 或任何不应该是这种情况的服务器端。

您可以直接以 JSON 格式获取,如下所示:

https://medium.com/@yourhandle/latest?format=json    

就我而言,我在 express 应用程序中创建了一个简单的 Web 服务并将其托管在 Heroku 上。 React App 访问 Heroku 上公开的 API 并获取数据。

const MEDIUM_URL = "https://medium.com/@yourhandle/latest?format=json";

router.get("/posts", (req, res, next) => {
  request.get(MEDIUM_URL, (err, apiRes, body) => {
    if (!err && apiRes.statusCode === 200) {
      let i = body.indexOf("{");
      const data = body.substr(i);
      res.send(data);
    } else {
      res.sendStatus(500).json(err);
    }
  });
});
const MdFetch = async (name) => {
  const res = await fetch(
    `https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/${name}`
  );
  return await res.json();
};

const data = await MdFetch('@chawki726');

将您的帖子作为 JSON 个对象

您可以替换您的用户名而不是@USERNAME。

https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@USERNAME

使用此 url,此 url 将提供 json 格式的帖子
将 studytact 替换为您的 Feed 名称

https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/studytact

现在这个 URL:

https://medium.com/@username/latest?format=json

位于 Cloudflare 的 DDoS 保护服务后面,因此您通常不会收到 JSON 格式的提要,而是通常会收到一个 HTML,它应该呈现一个网站以完成 reCAPTCHA 和API 请求中没有任何数据。

以及以下内容:

https://medium.com/feed/@username

最近有 10 个帖子的限制。

我建议 this free Cloudflare Worker 我为此目的制作的。它用作外观,因此您不必担心帖子是如何从源、reCAPTCHA 或分页中​​获取的。

Full article关于它。

Live example。要获取以下项目,请使用 API 提供的 JSON 字段 next 的值添加查询参数 ?next=

我创建了一个自定义 REST API 来检索 Medium 上给定 post 的统计信息,您只需向我的自定义 API 发送一个 GET 请求,然后您将检索统计数据作为 Json abject,如下所示: 要求:

curl https://endpoint/api/stats?story_url=THE_URL_OF_THE_MEDIUM_STORY

回复:

{
   "claps": 78,
   "comments": 1
}

API 在合理的响应时间内(< 2 秒)响应,您可以找到更多相关信息 in the following Medium article.