使用jQuery getJson从Tumblr博客获取所有帖子

时间:2020-11-29 19:36:29

I want to get all text posts of a Tumblr blog with jQuery getJson using Tumblr's API.

我想使用Tumblr的API获取使用jQuery getJson的Tumblr博客的所有文本帖子。

I tried using the following code, but I just got 20 posts:

我尝试使用以下代码,但我只有20个帖子:

function loadPosts () {

  var key = "api_key=xBcVPLfdDKpH0GjMCd1whW7rPoYkzLgZD3ZwpzndISFI4huSpA"
  var api = "https://api.tumblr.com/v2/blog/only-text-posts.tumblr.com/"
  var post_amount

  $.getJSON(api + "info?" + key,function(data) {
    post_amount = data.response.blog.posts
    $.getJSON(api + "posts/text?&filter=text&limit=" + post_amount + "&" + key,function(data) {
      $.each(data.response.posts, function(i, item) {
        var content = item.body
        $("#Posts ul").append('<li>' + content + '</li>')
      });
    })
  })

}

Here is a good sample Tumblr blog for testing:

以下是Tumblr博客测试的一个很好的示例:

http://only-text-posts.tumblr.com/

http://only-text-posts.tumblr.com/

2 个解决方案

#1


4  

According to the documentation, only up to 20 posts are returned. You can specify an offset with the offset parameter, and retrieve all posts with several calls:

根据文档,只返回最多20个帖子。您可以使用offset参数指定偏移量,并使用多个调用检索所有帖子:

function loadPosts () {

    var key = "api_key=your_key";
    var api = "https://api.tumblr.com/v2/blog/only-text-posts.tumblr.com/";
    var retrieve_more = function (offset) {
        $.getJSON(api + "posts/text?callback=?&filter=text&limit=20&offset=" + offset + "&" + key,function(data) {
            $.each(data.response.posts, function(i, item) {
                var content = item.body;
                $("#Posts ul").append('<li>' + content + '</li>')
            });

            if (data.response.posts.length == 20) {
                retrieve_more(offset + 20);
            }
        });
    };

    retrieve_more(0);
}

loadPosts();

fiddle

小提琴

#2


1  

As from the Tumblr Api documentation there is a limit of 20 posts per request. You can perform multiple requests with an increasing offset.

从Tumblr Api文档来看,每个请求限制为20个帖子。您可以使用增加的偏移量执行多个请求。

var max_posts_per_page = 20;
$.getJSON(api + "info?" + key,function(data) {
    post_amount = data.response.blog.posts;
    for (var offset = 0; offset < post_amount; offset += max_posts_per_page) {
        $.getJSON(api + "posts/text?&filter=text&limit=" + max_posts_per_page + "&offset=" + offset + "&" + key,function(data) {
            $.each(data.response.posts, function(i, item) {
                var content = item.body
                $("#Posts ul").append('<li>' + content + '</li>')
            });
        });
    }
});

#1


4  

According to the documentation, only up to 20 posts are returned. You can specify an offset with the offset parameter, and retrieve all posts with several calls:

根据文档,只返回最多20个帖子。您可以使用offset参数指定偏移量,并使用多个调用检索所有帖子:

function loadPosts () {

    var key = "api_key=your_key";
    var api = "https://api.tumblr.com/v2/blog/only-text-posts.tumblr.com/";
    var retrieve_more = function (offset) {
        $.getJSON(api + "posts/text?callback=?&filter=text&limit=20&offset=" + offset + "&" + key,function(data) {
            $.each(data.response.posts, function(i, item) {
                var content = item.body;
                $("#Posts ul").append('<li>' + content + '</li>')
            });

            if (data.response.posts.length == 20) {
                retrieve_more(offset + 20);
            }
        });
    };

    retrieve_more(0);
}

loadPosts();

fiddle

小提琴

#2


1  

As from the Tumblr Api documentation there is a limit of 20 posts per request. You can perform multiple requests with an increasing offset.

从Tumblr Api文档来看,每个请求限制为20个帖子。您可以使用增加的偏移量执行多个请求。

var max_posts_per_page = 20;
$.getJSON(api + "info?" + key,function(data) {
    post_amount = data.response.blog.posts;
    for (var offset = 0; offset < post_amount; offset += max_posts_per_page) {
        $.getJSON(api + "posts/text?&filter=text&limit=" + max_posts_per_page + "&offset=" + offset + "&" + key,function(data) {
            $.each(data.response.posts, function(i, item) {
                var content = item.body
                $("#Posts ul").append('<li>' + content + '</li>')
            });
        });
    }
});