Skip to content

Commit

Permalink
feat: add a way to post a video
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrzeptowski committed Mar 11, 2024
1 parent 8bb0b58 commit 964b934
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
89 changes: 88 additions & 1 deletion src/reddit/subreddit/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,14 @@ export interface UploadResponse {
};
}

type PostTypes = "self" | "link" | "crosspost" | "image" | "gallery";
type PostTypes =
| "self"
| "link"
| "crosspost"
| "image"
| "gallery"
| "video"
| "videogif";

interface PostOptions {
kind: PostTypes;
Expand All @@ -212,6 +219,7 @@ interface PostOptions {
// This only applies to link and cross posts
resubmit: boolean;
websocketUrl?: string;
videoPosterUrl?: string;
items?: {
caption?: string;
mediaId: string;
Expand Down Expand Up @@ -1042,6 +1050,82 @@ export class SubredditControls extends BaseControls {
}
}

/**
* Submit a video post.
* @param subreddit The subreddit to submit the post to.
* @param title The title of the post.
* @param videoFile The video file to post.
* @param videoFileName The name of the video file.
* @param thumbnailFile The thumbnail file to use.
* @param thumbnailFileName The name of the thumbnail file.
* @param videoGif Whether the video is a gif.
* @param noWebsockets Whether to disable websockets for the video.
* @param options Any extra options.
*
* @returns A promise that resolves to the ID of the new post or undefined
*/
async postVideo(
subreddit: string,
title: string,
videoFile: Blob,
videoFileName: string,
thumbnailFile: Blob,
thumbnailFileName: string,
videoGif = false,
noWebsockets = false,
options: LinkPostOptions = {}
): Promise<string | undefined> {
let url, videoPosterUrl, websocketUrl;
const kind = videoGif ? "videogif" : "video";

try {
const video =
videoFile instanceof MediaVideo
? videoFile
: await this.uploadMedia({
file: videoFile,
name: videoFileName,
type: videoGif ? "gif" : "video",
});
debug("Video upload response %o", video);
url = video?.fileUrl;
websocketUrl = video?.websocketUrl;
} catch (error) {
debug("Failed to upload video: %o", error);
throw new Error(`Failed to upload video: ${(error as Error).message}`);
}
try {
const thumbnail =
thumbnailFile instanceof MediaImg
? thumbnailFile
: await this.uploadMedia({
file: thumbnailFile,
name: thumbnailFileName,
type: "img",
});
debug("Thumbnail upload response %o", thumbnail);
videoPosterUrl = thumbnail?.fileUrl;
} catch (error) {
debug("Failed to upload thumbnail: %o", error);
throw new Error(
`Failed to upload thumbnail: ${(error as Error).message}`
);
}

return this.post(subreddit, {
title,
kind,
url,
videoPosterUrl,
websocketUrl: noWebsockets ? undefined : websocketUrl,
sendReplies: options.sendReplies ?? false,
resubmit: !options.unique,
captcha: options.captcha,
nsfw: options.nsfw ?? false,
spoiler: options.spoiler ?? false,
});
}

/**
* Submit a gallery post.
* @param params Options for submitting a gallery post.
Expand Down Expand Up @@ -1310,6 +1394,9 @@ export class SubredditControls extends BaseControls {
};
});
}
if (options.kind === "video" || options.kind === "videogif") {
request.video_poster_url = options.videoPosterUrl;
}

return request;
}
Expand Down
36 changes: 36 additions & 0 deletions src/reddit/subreddit/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,42 @@ export class Subreddit extends Content implements SubredditData {
);
}

/**
* Submit a video post.
* @param title The title of the post.
* @param videoFile The video file to post.
* @param videoFileName The name of the video file.
* @param thumbnailFile The thumbnail file to use.
* @param thumbnailFileName The name of the thumbnail file.
* @param videoGif Whether the video is a gif.
* @param noWebsockets Whether to disable websockets for the video.
* @param options Any extra options.
*
* @returns A promise that resolves to the ID of the new post or undefined
*/
async postVideo(
title: string,
videoFile: Blob,
videoFileName: string,
thumbnailFile: Blob,
thumbnailFileName: string,
videoGif = false,
noWebsockets = false,
options: LinkPostOptions = {}
): Promise<string | undefined> {
return this.controls.postVideo(
this.displayName,
title,
videoFile,
videoFileName,
thumbnailFile,
thumbnailFileName,
videoGif,
noWebsockets,
options
);
}

/**
* Submit a gallery post.
* @param params Options for submitting a gallery post.
Expand Down

0 comments on commit 964b934

Please sign in to comment.