Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cli-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25050,7 +25050,11 @@
"type",
"author",
"score",
"text"
"text",
"post_hint",
"url_overridden_by_dest",
"preview_image_url",
"gallery_urls"
],
"type": "js",
"modulePath": "reddit/read.js",
Expand Down
57 changes: 56 additions & 1 deletion clis/reddit/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ cli({
help: `Max expansion passes when --expand-more is on (${REDDIT_EXPAND_ROUNDS_MIN}–${REDDIT_EXPAND_ROUNDS_MAX}; each round can fan out new "more" stubs)`,
},
],
columns: ['type', 'author', 'score', 'text'],
columns: ['type', 'author', 'score', 'text', 'post_hint', 'url_overridden_by_dest', 'preview_image_url', 'gallery_urls'],
func: async (page, kwargs) => {
// Note: --limit / --depth / --replies / --max-length keep their original
// Math.max-style behaviour for backward compatibility (grandfathered in
Expand Down Expand Up @@ -161,6 +161,40 @@ cli({
// `text`) to sidestep the silent-column-drop audit (PR #1329).
const result = await page.evaluate(`
(async function() {
function decodeHtml(s) {
if (typeof s !== 'string' || !s) return '';
return s
.replace(/&/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&#x27;/gi, "'")
.replace(/&#39;/g, "'");
}
function extractRedditMedia(d) {
var post_hint = (d && d.post_hint) || '';
var url_overridden_by_dest = (d && d.url_overridden_by_dest) || '';
var preview_image_url = decodeHtml(
(d && d.preview && d.preview.images && d.preview.images[0] && d.preview.images[0].source && d.preview.images[0].source.url) || ''
);
var gallery_urls = [];
var items = d && d.gallery_data && d.gallery_data.items;
var meta = d && d.media_metadata;
if (Array.isArray(items) && meta) {
for (var gi = 0; gi < items.length; gi++) {
var it = items[gi];
var m = it && meta[it.media_id];
var u = m && m.s && m.s.u;
if (u) gallery_urls.push(decodeHtml(u));
}
}
return {
post_hint: post_hint,
url_overridden_by_dest: url_overridden_by_dest,
preview_image_url: preview_image_url,
gallery_urls: gallery_urls,
};
}
var postId = ${JSON.stringify(postId)};
var linkFullname = 't3_' + postId;

Expand Down Expand Up @@ -392,11 +426,16 @@ cli({
// Post header row.
var body = post.selftext || '';
if (body.length > maxLength) body = body.slice(0, maxLength) + '\\n... [truncated]';
var postMedia = extractRedditMedia(post);
rows.push({
type: 'POST',
author: post.author || '[deleted]',
score: post.score || 0,
text: post.title + (body ? '\\n\\n' + body : '') + (post.url && !post.is_self ? '\\n' + post.url : ''),
post_hint: postMedia.post_hint,
url_overridden_by_dest: postMedia.url_overridden_by_dest,
preview_image_url: postMedia.preview_image_url,
gallery_urls: postMedia.gallery_urls,
});

// Recursive comment walker.
Expand All @@ -418,6 +457,10 @@ cli({
author: d.author || '[deleted]',
score: d.score || 0,
text: indentedBody,
post_hint: '',
url_overridden_by_dest: '',
preview_image_url: '',
gallery_urls: [],
});

var t1Children = [];
Expand All @@ -443,6 +486,10 @@ cli({
author: '',
score: '',
text: cutoffIndent + '[+' + totalHidden + ' more replies]',
post_hint: '',
url_overridden_by_dest: '',
preview_image_url: '',
gallery_urls: [],
});
}
return;
Expand All @@ -463,6 +510,10 @@ cli({
author: '',
score: '',
text: moreIndent + '[+' + hidden + ' more replies]',
post_hint: '',
url_overridden_by_dest: '',
preview_image_url: '',
gallery_urls: [],
});
}
}
Expand All @@ -489,6 +540,10 @@ cli({
author: '',
score: '',
text: '[+' + hiddenTopLevel + ' more top-level comments]',
post_hint: '',
url_overridden_by_dest: '',
preview_image_url: '',
gallery_urls: [],
});
}

Expand Down
29 changes: 24 additions & 5 deletions clis/reddit/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,18 @@ describe('reddit read adapter', () => {
it('uses an ephemeral Reddit site tab by default', () => {
expect(command?.browser).toBe(true);
expect(command?.siteSession).toBeUndefined();
expect(command?.columns).toEqual(['type', 'author', 'score', 'text']);
expect(command?.columns).toEqual([
'type', 'author', 'score', 'text',
'post_hint', 'url_overridden_by_dest', 'preview_image_url', 'gallery_urls',
]);
});

it('embeds extractRedditMedia in the browser-evaluated source and applies it to the POST row', async () => {
const page = makePage({ kind: 'ok', rows: [], expandMeta: { rounds: 0, fetched: 0, capped: false, errors: [] } });
await command.func(page, { 'post-id': 'abc123', limit: 5 });
const src = page.evaluate.mock.calls[0][0];
expect(src).toContain('function extractRedditMedia');
expect(src).toContain('var postMedia = extractRedditMedia(post)');
});

it('exposes the new --expand-more / --expand-rounds args', () => {
Expand Down Expand Up @@ -153,16 +164,24 @@ describe('reddit read adapter', () => {
const page = makePage({
kind: 'ok',
rows: [
{ type: 'POST', author: 'alice', score: 10, text: 'Title' },
{ type: 'L0', author: 'bob', score: 5, text: 'Comment' },
{ type: 'POST', author: 'alice', score: 10, text: 'Title',
post_hint: 'image', url_overridden_by_dest: 'https://i.redd.it/a.jpg',
preview_image_url: 'https://preview.redd.it/a.jpg?width=640',
gallery_urls: [] },
{ type: 'L0', author: 'bob', score: 5, text: 'Comment',
post_hint: '', url_overridden_by_dest: '', preview_image_url: '', gallery_urls: [] },
],
expandMeta: { rounds: 0, fetched: 0, capped: false, errors: [] },
});
const result = await command.func(page, { 'post-id': 'abc123', limit: 5 });
expect(page.goto).toHaveBeenCalledWith('https://www.reddit.com');
expect(result).toEqual([
{ type: 'POST', author: 'alice', score: 10, text: 'Title' },
{ type: 'L0', author: 'bob', score: 5, text: 'Comment' },
{ type: 'POST', author: 'alice', score: 10, text: 'Title',
post_hint: 'image', url_overridden_by_dest: 'https://i.redd.it/a.jpg',
preview_image_url: 'https://preview.redd.it/a.jpg?width=640',
gallery_urls: [] },
{ type: 'L0', author: 'bob', score: 5, text: 'Comment',
post_hint: '', url_overridden_by_dest: '', preview_image_url: '', gallery_urls: [] },
]);
});

Expand Down
Loading