fetch-reddit
Advanced tools
Comparing version 0.0.5 to 0.0.6
125
lib/index.js
@@ -11,4 +11,6 @@ 'use strict'; | ||
var API_URL = 'https://www.reddit.com'; | ||
var REDDIT_URL = 'https://www.reddit.com'; | ||
var MATCH_REPLY_URLS = /(?:\[([^\]]+)\]\s*\()?(https?\:\/\/[^\)\s]+)\)?/gi; | ||
var REPLACE_CHAR = String.fromCharCode(0); | ||
var INFER_TITLE_MAX_LENGTH = 128; // Max length of remaining text to use as a title for a link | ||
@@ -21,3 +23,3 @@ var KIND_COMMENT = 't1'; | ||
path = path[0] === '/' ? path : '/' + path; | ||
return fetch(API_URL + path + '.json?' + (0, _qs.stringify)(query)).then(function (response) { | ||
return fetch(REDDIT_URL + path + '.json?' + (0, _qs.stringify)(query)).then(function (response) { | ||
return response.json(); | ||
@@ -32,3 +34,3 @@ }); | ||
return { | ||
posts: extractPosts(data), | ||
posts: extractPosts(data, path), | ||
loadMore: getLoadMoreFn(data, { path: path, query: query }) | ||
@@ -39,25 +41,38 @@ }; | ||
function extractPosts(data) { | ||
var posts = []; | ||
if (data instanceof Array) { | ||
data.forEach(function (post) { | ||
return posts = posts.concat(extractPosts(post)); | ||
}); | ||
} else if (data.json) { | ||
data.json.data.things.forEach(function (post) { | ||
return posts = posts.concat(extractPosts(post)); | ||
}); | ||
} else if (data.kind === KIND_LISTING) { | ||
data.data.children.forEach(function (post) { | ||
return posts = posts.concat(extractPosts(post)); | ||
}); | ||
} else if (data.kind === KIND_POST && !data.data.is_self) { | ||
posts.push(postFromPost(data.data)); | ||
} else if (data.kind === KIND_COMMENT || data.data.is_self) { | ||
posts = posts.concat(extractFromComment(data)); | ||
function extractPosts(_x2, _x3) { | ||
var _again = true; | ||
_function: while (_again) { | ||
var data = _x2, | ||
path = _x3; | ||
_again = false; | ||
if (data instanceof Array) { | ||
return data.reduce(function (posts, post) { | ||
return posts.concat(extractPosts(post, path)); | ||
}, []); | ||
} | ||
if (data.json) { | ||
_x2 = data.json.data.things; | ||
_x3 = path; | ||
_again = true; | ||
continue _function; | ||
} | ||
if (data.kind === KIND_LISTING) { | ||
_x2 = data.data.children; | ||
_x3 = path; | ||
_again = true; | ||
continue _function; | ||
} | ||
if (data.kind === KIND_POST && !data.data.is_self) { | ||
return postFromPost(data.data); | ||
} | ||
if (data.kind === KIND_COMMENT || data.data.is_self) { | ||
return extractFromComment(data, path); | ||
} | ||
return []; | ||
} | ||
return posts; | ||
} | ||
function extractFromComment(post) { | ||
function extractFromComment(post, path) { | ||
var posts = []; | ||
@@ -67,4 +82,9 @@ if (post.kind === 'more') { | ||
} | ||
getText(post.data).replace(MATCH_REPLY_URLS, function (match, title, url, offset) { | ||
posts.push(postFromComment(post.data, match, title, url, offset)); | ||
// Use REPLACE_CHAR to avoid regex problems with escaped ] characters within the title string | ||
getText(post.data).replace(/\\]/g, REPLACE_CHAR).replace(MATCH_REPLY_URLS, function (match, title, url, offset) { | ||
if (title) { | ||
// Bring back the removed ] and then we can safely unescape everything | ||
title = title.replace(new RegExp(REPLACE_CHAR, 'g'), '\\]').replace(/\\(.)/g, '$1'); | ||
} | ||
posts.push(postFromComment(post.data, match, title, url, offset, path)); | ||
}); | ||
@@ -74,3 +94,3 @@ if (post.data.replies) { | ||
if (reply.kind === KIND_COMMENT) { | ||
posts = posts.concat(extractFromComment(reply)); | ||
posts = posts.concat(extractFromComment(reply, path)); | ||
} | ||
@@ -82,15 +102,15 @@ }); | ||
function getLoadMoreFn(_x2, _x3) { | ||
var _again = true; | ||
function getLoadMoreFn(_x4, _x5) { | ||
var _again2 = true; | ||
_function: while (_again) { | ||
var data = _x2, | ||
options = _x3; | ||
_again = false; | ||
_function2: while (_again2) { | ||
var data = _x4, | ||
options = _x5; | ||
_again2 = false; | ||
if (data instanceof Array) { | ||
_x2 = data.pop(); | ||
_x3 = options; | ||
_again = true; | ||
continue _function; | ||
_x4 = data.pop(); | ||
_x5 = options; | ||
_again2 = true; | ||
continue _function2; | ||
} | ||
@@ -114,6 +134,6 @@ if (options.parent) { | ||
if (data.data.children) { | ||
_x2 = data.data.children.pop(); | ||
_x3 = options; | ||
_again = true; | ||
continue _function; | ||
_x4 = data.data.children.pop(); | ||
_x5 = options; | ||
_again2 = true; | ||
continue _function2; | ||
} | ||
@@ -143,16 +163,19 @@ } | ||
url: post.url, | ||
thumbnail: post.thumbnail, | ||
created: new Date(post.created_utc * 1000), | ||
author: post.author, | ||
score: post.score, | ||
subreddit: post.subreddit | ||
subreddit: post.subreddit, | ||
permalink: getPermalink(post), | ||
// Post-specific fields | ||
thumbnail: post.thumbnail, | ||
num_comments: post.num_comments | ||
}; | ||
} | ||
function postFromComment(post, match, title, url, offset) { | ||
function postFromComment(post, match, title, url, offset, path) { | ||
if (title === undefined) title = null; | ||
// If the post is just text and a link, use the text as the title | ||
// If the post is just a small amount of 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)) { | ||
if (!title && remaining.length < INFER_TITLE_MAX_LENGTH && !remaining.match(MATCH_REPLY_URLS)) { | ||
title = remaining; | ||
@@ -167,3 +190,6 @@ } | ||
score: post.score, | ||
subreddit: post.subreddit | ||
subreddit: post.subreddit, | ||
permalink: getPermalink(post, path), | ||
// Comment-specific fields | ||
comment_id: post.id | ||
}; | ||
@@ -174,2 +200,9 @@ } | ||
return post.body || post.selftext || ''; | ||
} | ||
function getPermalink(post, path) { | ||
if (post.permalink) { | ||
return REDDIT_URL + post.permalink; | ||
} | ||
return REDDIT_URL + path + '/' + post.id; | ||
} |
{ | ||
"name": "fetch-reddit", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Easily fetch links from Reddit subs and threads", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
7071
176