使用Tumblr API从帖子标题中提取第一句话

Using Tumblr API to pull first sentence from post captions

本文关键字:提取 一句话 标题 Tumblr API 使用      更新时间:2023-09-26

我有一个关于Tumblr API的快速问题。我正在为一个摄影师的网站工作,并希望使用该API从她的主页上最近的两个帖子中流式传输前50 - 100个单词。我编写的JS文件从最近的两篇文章中提取标题,但我不知道如何只提取前50个左右的单词。任何建议都将非常感激!!

function buildURL (blogname, apiKey) {
    return 'http://api.tumblr.com/v2/blog/'
        + blogname
        + '.tumblr.com/posts?api_key='
        + apiKey
        + '&limit=2'
        + '&callback=?'
}
var key = '/* Put your Tumblr Key Here */'
var url = buildURL('ehockstein', key)
$(function () {
    $.getJSON(url, function (data) {
        console.log(data)
        createPosts(data.response.posts)
    })
})
function createPosts (posts) {
    posts.forEach(function (post) {
        var postElement = $('<div class="post"></div>')
        postElement.addClass(post.type)
        if (post.type === 'photo') {
            var caption = post.caption
            postElement.append(caption)
        }
        postElement.appendTo('#tumblr-posts')
    })
}

Tumblr API不支持返回部分标题。您需要从API中提取完整的标题,并在您自己的代码中创建包含前50 - 100个单词的子字符串。

它看起来像这样:

var shortCaption = caption.substring(0, 499)

取前500个字符,也就是将近100个单词。

然后可以将shortCaption附加到postElement(而不是附加caption)。

您可以在这里了解更多关于子字符串如何工作的信息