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
28 changes: 28 additions & 0 deletions lib/apify/__tests__/startLinkedinProfileScraping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@ describe("startLinkedinProfileScraping", () => {
);
expect(start).not.toHaveBeenCalled();
});

it("runs the posts actor with maxPosts when a posts depth is requested", async () => {
const r = await startLinkedinProfileScraping("sweetmaneth", 20);
expect(apifyClient.actor).toHaveBeenCalledWith("harvestapi/linkedin-profile-posts");
expect(start).toHaveBeenCalledWith(
{ targetUrls: ["https://www.linkedin.com/in/sweetmaneth"], maxPosts: 20 },
{ webhooks: expect.any(Array) },
);
expect(r).toEqual({ runId: "run-1", datasetId: "ds-1" });
});

it("posts depth keeps the full-URL passthrough and the handle guards", async () => {
await startLinkedinProfileScraping("https://www.linkedin.com/in/drew-thurlow", 5);
expect(start).toHaveBeenCalledWith(
{ targetUrls: ["https://www.linkedin.com/in/drew-thurlow"], maxPosts: 5 },
{ webhooks: expect.any(Array) },
);
await expect(startLinkedinProfileScraping("in", 5)).rejects.toThrow(/Invalid LinkedIn handle/);
});

it("posts omitted keeps the legacy profile actor and input byte-identical", async () => {
await startLinkedinProfileScraping("sweetmaneth", undefined);
expect(apifyClient.actor).toHaveBeenCalledWith("harvestapi/linkedin-profile-scraper");
expect(start).toHaveBeenCalledWith(
{ urls: ["https://www.linkedin.com/in/sweetmaneth"] },
{ webhooks: expect.any(Array) },
);
});
});
25 changes: 19 additions & 6 deletions lib/apify/linkedin/startLinkedinProfileScraping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import { OUTSTANDING_ERROR } from "@/lib/apify/errors";
import { ApifyRunInfo } from "@/lib/apify/types";

/**
* Starts a LinkedIn profile scrape via the harvestapi actor.
* Starts a LinkedIn scrape via the harvestapi actors.
* Mirrors the other `start<Platform>ProfileScraping` modules.
*
* The profile actor's dataset has no post fields, so a requested `posts`
* depth switches to the posts actor (post items with engagement counts) —
* the same semantics as X/YouTube: depth requested → post items in the
* dataset; omitted → the legacy profile snapshot.
*
* @param handle - A LinkedIn vanity slug (e.g. `drew-thurlow`) or full profile URL.
* @param posts - Max recent posts to scrape; omitted → profile snapshot.
* @returns Apify run info, or null if the run failed to start.
* @see https://apify.com/harvestapi/linkedin-profile-scraper
* @see https://apify.com/harvestapi/linkedin-profile-posts
*/
const startLinkedinProfileScraping = async (handle: string): Promise<ApifyRunInfo | null> => {
const startLinkedinProfileScraping = async (
handle: string,
posts?: number,
): Promise<ApifyRunInfo | null> => {
const cleanHandle = handle.trim().replace(/^@/, "");
// Legacy rows stored the LinkedIn path prefix ("in") as the username — never
// scrape those (linkedin.com/in/in is a real, wrong profile URL); fail loudly.
Expand All @@ -23,12 +33,15 @@ const startLinkedinProfileScraping = async (handle: string): Promise<ApifyRunInf
? cleanHandle
: `https://www.linkedin.com/in/${cleanHandle}`;

const input = { urls: [targetUrl] };
const actorId =
posts === undefined
? "harvestapi/linkedin-profile-scraper"
: "harvestapi/linkedin-profile-posts";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Posts-requested LinkedIn runs can finish without result processing because they are started on a different actor that is not registered in the webhook actorId handler map. Consider wiring the posts actorId to an appropriate LinkedIn results handler (or adding a dedicated posts handler) so posts scrapes persist/process like other platforms.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At lib/apify/linkedin/startLinkedinProfileScraping.ts, line 39:

<comment>Posts-requested LinkedIn runs can finish without result processing because they are started on a different actor that is not registered in the webhook actorId handler map. Consider wiring the posts actorId to an appropriate LinkedIn results handler (or adding a dedicated posts handler) so `posts` scrapes persist/process like other platforms.</comment>

<file context>
@@ -23,12 +33,15 @@ const startLinkedinProfileScraping = async (handle: string): Promise<ApifyRunInf
+  const actorId =
+    posts === undefined
+      ? "harvestapi/linkedin-profile-scraper"
+      : "harvestapi/linkedin-profile-posts";
+  const input =
+    posts === undefined ? { urls: [targetUrl] } : { targetUrls: [targetUrl], maxPosts: posts };
</file context>

const input =
posts === undefined ? { urls: [targetUrl] } : { targetUrls: [targetUrl], maxPosts: posts };

try {
const run = await apifyClient
.actor("harvestapi/linkedin-profile-scraper")
.start(input, { webhooks: getApifyWebhooks() });
const run = await apifyClient.actor(actorId).start(input, { webhooks: getApifyWebhooks() });

if (!run?.id || !run?.defaultDatasetId) {
console.error("Failed to start LinkedIn profile scraping for handle:", handle);
Expand Down
Loading