fetch-reddit
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -42,17 +42,13 @@ 'use strict'; | ||
}); | ||
} | ||
if (data.json) { | ||
} else if (data.json) { | ||
data.json.data.things.forEach(function (post) { | ||
return posts = posts.concat(extractPosts(post)); | ||
}); | ||
} | ||
if (data.kind === KIND_LISTING) { | ||
} else if (data.kind === KIND_LISTING) { | ||
data.data.children.forEach(function (post) { | ||
return posts = posts.concat(extractPosts(post)); | ||
}); | ||
} | ||
if (data.kind === KIND_POST) { | ||
} else if (data.kind === KIND_POST && !data.data.is_self) { | ||
posts.push(postFromPost(data.data)); | ||
} | ||
if (data.kind === KIND_COMMENT) { | ||
} else if (data.kind === KIND_COMMENT || data.data.is_self) { | ||
posts = posts.concat(extractFromComment(data)); | ||
@@ -68,4 +64,4 @@ } | ||
} | ||
post.data.body.replace(MATCH_REPLY_URLS, function (match, title, url) { | ||
posts.push(postFromComment(post, match, title, url)); | ||
getText(post.data).replace(MATCH_REPLY_URLS, function (match, title, url, offset) { | ||
posts.push(postFromComment(post.data, match, title, url, offset)); | ||
}); | ||
@@ -143,19 +139,29 @@ if (post.data.replies) { | ||
created: new Date(post.created_utc * 1000), | ||
author: post.author | ||
author: post.author, | ||
score: post.score, | ||
subreddit: post.subreddit | ||
}; | ||
} | ||
function postFromComment(post, match, title, url) { | ||
// If the post is just text then a link, use the text as the title | ||
var remaining = post.data.body.replace(match, ''); | ||
if (!title && remaining.length < 256 && !MATCH_REPLY_URLS.test(remaining)) { | ||
title = remaining.trim(); | ||
function postFromComment(post, match, title, url, offset) { | ||
if (title === undefined) title = null; | ||
// If the post is just text and a link, use the text as the title | ||
var remaining = getText(post).replace(match, '').trim(); | ||
if (!title && remaining.length < 128 && !remaining.match(MATCH_REPLY_URLS)) { | ||
title = remaining; | ||
} | ||
return { | ||
id: url, | ||
id: post.id + ':' + offset, | ||
title: title, | ||
url: url, | ||
created: new Date(post.data.created_utc * 1000), | ||
author: post.data.author | ||
created: new Date(post.created_utc * 1000), | ||
author: post.author, | ||
score: post.score, | ||
subreddit: post.subreddit | ||
}; | ||
} | ||
function getText(post) { | ||
return post.body || post.selftext || ''; | ||
} |
{ | ||
"name": "fetch-reddit", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Easily fetch links from Reddit subs and threads", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
6096
146
5