Skip to content
Draft
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
1 change: 1 addition & 0 deletions api/src/processing/match-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default function({
case "picker":
responseType = "picker";
switch (host) {
case "substack":
case "instagram":
case "twitter":
case "snapchat":
Expand Down
13 changes: 10 additions & 3 deletions api/src/processing/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ import facebook from "./services/facebook.js";
import bluesky from "./services/bluesky.js";
import xiaohongshu from "./services/xiaohongshu.js";
import newgrounds from "./services/newgrounds.js";
import substack from "./services/substack.js";


let freebind;

export default async function({ host, patternMatch, params, authType }) {
export default async function({ host, patternMatch, params, authType }) {
const { url } = params;
assert(url instanceof URL);
let dispatcher, requestIP;
Expand Down Expand Up @@ -74,8 +76,7 @@ export default async function({ host, patternMatch, params, authType }) {
youtubeHLS = false;
}

const subtitleLang =
params.subtitleLang !== "none" ? params.subtitleLang : undefined;
const subtitleLang =params.subtitleLang !== "none" ? params.subtitleLang : undefined;

switch (host) {
case "twitter":
Expand Down Expand Up @@ -276,6 +277,12 @@ export default async function({ host, patternMatch, params, authType }) {
});
break;

case "substack":
console.log(url);

r = await substack({ url });
break;

default:
return createResponse("error", {
code: "error.api.service.unsupported"
Expand Down
7 changes: 7 additions & 0 deletions api/src/processing/service-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ export const services = {
],
subdomains: "*",
},
substack: {
patterns: [
"@:user/note/:noteId",
":user/p/:noteId"
],
subdomains: "*",
},
twitch: {
patterns: [":channel/clip/:clip"],
tld: "tv",
Expand Down
4 changes: 4 additions & 0 deletions api/src/processing/service-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ export const testers = {

"youtube": pattern =>
pattern.id?.length <= 11,

"substack": pattern =>
pattern.user?.length <= 30 && pattern.noteId?.length <= 20,

}
45 changes: 45 additions & 0 deletions api/src/processing/services/substack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { createStream } from "../../stream/manage.js";


export default async function({url}) {
try {
const m = url.href.match(/\/c-(\d+)(?=\b|$|[?\/#])/);
const id = m ? m[1] : null;
const response = await fetch("https://substack.com/api/v1/reader/comment/" + id);

if (!response.ok) {
throw new Error(`Failed to fetch: ${response.status} ${response.statusText}`);
}

const data = await response.json();

const imageAttachments = (data.item?.comment?.attachments || []).filter(att => att.type === "image");

const picker = imageAttachments.map((att, i) => {
const imgUrl = att.imageUrl;
const imageId = att.imageUrl.split("/").pop(); // extract image id for thumbnail url
const thumbUrl = `https://substackcdn.com/image/fetch/$s_!xQMH!,w_200,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F${imageId}`;

return {
type: "photo",
url: imgUrl,
thumb: createStream({
service: "substack",
type: "proxy",
url: thumbUrl,
filename: `substack_${i + 1}.jpg`
})
};
});

return {
picker
};

} catch (error) {
return {
picker: [],
error: error.message
};
}
}
3 changes: 3 additions & 0 deletions api/src/processing/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ function aliasURL(url) {
url = new URL(`https://www.reddit.com/video/${parts[1]}`);
}
break;
case "substack":
url = new URL(url.href)
break;
}

return url;
Expand Down