Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Request, Response } from "express";
import { getArtistSocials } from "../lib/supabase/getArtistSocials";
import type { RequestHandler } from "express";
import { getArtistSocials } from "../../lib/supabase/getArtistSocials";

/**
* Handler for GET /api/artist/socials
* Handler for GET /artist/socials
* Retrieves all social media profiles associated with an artist account
*/
export const getArtistSocialsHandler = async (req: Request, res: Response) => {
export const getArtistSocialsHandler: RequestHandler = async (req, res) => {
try {
// Get query parameters
const { artist_account_id, page, limit } = req.query;

// Validate required parameters
if (!artist_account_id || typeof artist_account_id !== "string") {
return res.status(400).json({
res.status(400).json({
status: "error",
message: "Missing required parameter: artist_account_id",
socials: [],
Expand All @@ -23,21 +21,22 @@ export const getArtistSocialsHandler = async (req: Request, res: Response) => {
total_pages: 0,
},
});
return;
}

// Call the database function with parameters
const result = await getArtistSocials({
artist_account_id,
page: typeof page === "string" ? parseInt(page, 10) : undefined,
limit: typeof limit === "string" ? parseInt(limit, 10) : undefined,
});

// Return the response
return res.status(result.status === "success" ? 200 : 500).json(result);
const statusCode = result.status === "success" ? 200 : 500;
res.status(statusCode).json(result);
return;
} catch (error) {
console.error("[ERROR] getArtistSocialsHandler error:", error);

return res.status(500).json({
res.status(500).json({
status: "error",
message:
error instanceof Error ? error.message : "An unknown error occurred",
Expand All @@ -49,5 +48,6 @@ export const getArtistSocialsHandler = async (req: Request, res: Response) => {
total_pages: 0,
},
});
return;
}
};
2 changes: 2 additions & 0 deletions controllers/ArtistSocialsController/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { getArtistSocialsHandler } from "./getArtistSocialsHandler";
export { postArtistSocialsScrapeHandler } from "./postArtistSocialsScrapeHandler";
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { RequestHandler } from "express";
import { getAccountSocials } from "../../lib/supabase/getAccountSocials";
import { scrapeProfileUrlBatch } from "../../lib/apify/scrapeProfileUrlBatch";

export const postArtistSocialsScrapeHandler: RequestHandler = async (
req,
res
) => {
try {
const { artist_account_id } = req.body ?? {};

if (!artist_account_id || typeof artist_account_id !== "string") {
res.status(400).json({
status: "error",
message: "artist_account_id body parameter is required",
});
return;
}

const { status, socials } = await getAccountSocials(artist_account_id);

if (status === "error") {
res.status(500).json({
status: "error",
message: "Failed to fetch artist socials",
});
return;
}

if (!socials.length) {
res.json([]);
return;
}

const results = await scrapeProfileUrlBatch(
socials.map((social) => ({
profileUrl: social.profile_url,
username: social.username,
}))
);

res.json(results);
return;
} catch (error) {
console.error("[ERROR] postArtistSocialsScrapeHandler error:", error);
res.status(500).json({
status: "error",
message: "Internal server error",
});
return;
}
};
7 changes: 5 additions & 2 deletions lib/apify/scrapeProfileUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ export interface ProfileScrapeResult {
runId: string | null;
datasetId: string | null;
error: string | null;
supported: boolean;
}

export type ScrapeProfileResult = ProfileScrapeResult & {
supported: boolean;
};

const PLATFORM_SCRAPERS: Array<{
match: (url: string) => boolean;
scraper: ScrapeRunner;
Expand Down Expand Up @@ -54,7 +57,7 @@ const PLATFORM_SCRAPERS: Array<{
export const scrapeProfileUrl = async (
profileUrl: string | null | undefined,
username: string
): Promise<ProfileScrapeResult | null> => {
): Promise<ScrapeProfileResult | null> => {
if (!profileUrl) {
return null;
}
Expand Down
28 changes: 28 additions & 0 deletions lib/apify/scrapeProfileUrlBatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
ProfileScrapeResult,
ScrapeProfileResult,
scrapeProfileUrl,
} from "./scrapeProfileUrl";

type ScrapeProfileUrlBatchInput = {
profileUrl: string | null | undefined;
username: string | null | undefined;
};

export const scrapeProfileUrlBatch = async (
inputs: ScrapeProfileUrlBatchInput[]
): Promise<ProfileScrapeResult[]> => {
const results = await Promise.all(
inputs.map(({ profileUrl, username }) =>
scrapeProfileUrl(profileUrl ?? null, username ?? "")
)
);

return results
.filter((result): result is ScrapeProfileResult => result !== null)
.map(({ runId, datasetId, error }) => ({
runId,
datasetId,
error,
}));
};
8 changes: 6 additions & 2 deletions routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { generateImageHandler } from "./controllers/ImageGenerationController";
import { getCommentsHandler } from "./controllers/CommentsController";
import { getArtistSegmentsHandler } from "./controllers/ArtistSegmentsController";
import { getSegmentFansHandler } from "./controllers/SegmentFansController";
import { getArtistSocialsHandler } from "./controllers/ArtistSocialsController";
import {
getArtistSocialsHandler,
postArtistSocialsScrapeHandler,
} from "./controllers/ArtistSocialsController";
import { getSocialPostsHandler } from "./controllers/SocialPostsController";
import { getPostCommentsHandler } from "./controllers/PostCommentsController";
import {
Expand Down Expand Up @@ -94,7 +97,8 @@ routes.get("/comments", getCommentsHandler as any);

routes.get("/artist/segments", getArtistSegmentsHandler as any);
routes.get("/segment/fans", getSegmentFansHandler as any);
routes.get("/artist/socials", getArtistSocialsHandler as any);
routes.get("/artist/socials", getArtistSocialsHandler);
routes.post("/artist/socials/scrape", postArtistSocialsScrapeHandler);
routes.get("/social/posts", getSocialPostsHandler as any);
routes.post("/social/scrape", postSocialScrapeHandler as any);
routes.get("/post/comments", getPostCommentsHandler as any);
Expand Down