Skip to content

Commit

Permalink
Include captions on i.redd.it and v.redd.it media
Browse files Browse the repository at this point in the history
  • Loading branch information
brookst authored and larsjohnsen committed Feb 16, 2023
1 parent 93e1f36 commit 1fc7d6f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/modules/hosts/ireddit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ export default new Host('ireddit', {
name: 'i.redd.it',
domains: ['i.redd.it'],
attribution: false,
detect({ pathname }, thing) { return pathname.endsWith('.gif') && thing && thing.isLinkPost() && thing.getFullname(); },
detect({ pathname }, thing) { return (/\.(webp|gif|jpe?g|png|svg)$/i).test(pathname) && thing && thing.isLinkPost() && thing.getFullname(); },
async handleLink(href, fullname) {
const postMetadata = await getPostMetadata({ id: fullname.replace('t3_', '') });
if (!postMetadata.preview) throw new Error('Post has no preview.');
const preview = postMetadata.preview.images[0];
if (preview.variants.mp4) {
return {
type: 'VIDEO',
caption: postMetadata.selftext_html,
loop: true,
muted: true,
fallback: preview.variants.gif && preview.variants.gif.source.url,
Expand All @@ -26,6 +27,7 @@ export default new Host('ireddit', {
} else {
return {
type: 'IMAGE',
caption: postMetadata.selftext_html,
src: preview.source.url,
};
}
Expand Down
9 changes: 7 additions & 2 deletions lib/modules/hosts/vreddit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { difference, sortBy } from 'lodash-es';
import { Host } from '../../core/host';
import { ajax } from '../../environment';
import { getPostMetadata } from '../../utils';

export default new Host('vreddit', {
name: 'v.redd.it',
Expand All @@ -24,8 +25,8 @@ export default new Host('vreddit', {
advanced: true,
},
},
detect: ({ pathname }) => pathname.slice(1),
async handleLink(href, id) {
detect({ pathname }, thing) { return {fullname: thing.getFullname(), id: pathname.slice(1)}; },
async handleLink(href, {fullname, id}) {
const originalPlaylistUrl = `https://v.redd.it/${id}/DASHPlaylist.mpd`;
const mpd = await ajax({ url: originalPlaylistUrl });
const manifest = new DOMParser().parseFromString(mpd, 'text/xml');
Expand Down Expand Up @@ -53,6 +54,9 @@ export default new Host('vreddit', {

if (!videoSourcesByBandwidth.length) throw new Error('Video has no valid sources');

// Get postMetadata for video caption
const postMetadata = await getPostMetadata({ id: fullname.replace('t3_', '') });

const sources = (muted && id) ?
videoSourcesByBandwidth.map(rep => ({
source: rep.querySelector('BaseURL').textContent,
Expand All @@ -65,6 +69,7 @@ export default new Host('vreddit', {
return {
type: 'VIDEO',
loop: true,
caption: postMetadata.selftext_html,
muted,
sources,
};
Expand Down

0 comments on commit 1fc7d6f

Please sign in to comment.