sergebulaev/linkedin-skills / .codex-marketplace/linkedin-skills
linkedin-marketing
VerifiedPlan, draft, audit, and publish LinkedIn posts and comments. Use when the user wants to write a viral LinkedIn post, draft a comment or reply on any LinkedIn post URL, audit a draft against 2026 algorithm heuristics, remove AI tells, extract hook formulas from viral posts, or plan a week of content. Powered by the Publora API for publishing. User provides post/comment URLs, skill drafts content, user approves, then publishes.
lib/apify_client.py
View on GitHub"""Thin Apify client for the LinkedIn Skills project.
Replaces the previous private HarvestAPI dependency. Each method wraps one
public Apify actor and uses the run-sync-get-dataset-items endpoint, so the
caller gets results back in a single HTTP request (no polling required).
Auth: APIFY_TOKEN env var (or constructor arg), sent as an
`Authorization: Bearer` header (never as a `?token=` query parameter, which
would leak the credential into proxy logs and error traces).
Actors used (all no-cookies, public, "$1-$5 per 1,000 results"):
- apimaestro/linkedin-post-detail
Fetch post body, author, stats, and the reshare `share_urn` by post URL
(input key `post_urls`, no cookies). Output is nested and normalized to
the flat contract by `_normalize_post`. Use for hook extraction,
pre-comment context, and resolving the reshare parent URN. (Replaced
supreme_coder/linkedin-post, which started returning empty results.)
- apimaestro/linkedin-post-comments-replies-engagements-scraper-no-cookies
Fetch comments + replies on a post (by post ID or URL). Use for
reply-handler thread structure and to avoid duplicate comment takes.
- apimaestro/linkedin-profile-comments
Fetch a user's recent comments by username. Use for engagement-monitor
author-reply tracking.
- scraping_solutions/linkedin-posts-engagers-likers-and-commenters-no-cookies
Fetch the people who liked or commented on a post. Use for engagement
analytics (group by seniority, company, role, ICP fit).
Caching: in-process LRU (256 entries, 6h TTL). Pass `force_refresh=True` on
any method to bypass. Retries on transient 408/429/5xx (3 attempts with
exponential backoff + jitter).
"""
from __future__ import annotations
import json
import os
import random
import time
from collections import OrderedDict
from copy import deepcopy
from typing import Any, Optional, Sequence
import requests
from ._env import load_env
class ApifyError(RuntimeError):
pass
RETRYABLE_STATUSES = {408, 429, 500, 502, 503, 504}
CACHE_MAX_ENTRIES = 256
CACHE_TTL_SECONDS = 6 * 60 * 60
def _retry(attempts: int = 3, base_delay: float = 0.6):
def decorator(fn):
def wrapper(*args, **kwargs):
last_exc: Optional[Exception] = None
for attempt in range(attempts):
try:
return fn(*args, **kwargs)
except ApifyError as e:
msg = str(e)
retryable = any(f"HTTP {s}" in msg for s in RETRYABLE_STATUSES)
if not retryable or attempt == attempts - 1:
raise
last_exc = e
except (requests.ConnectionError, requests.Timeout) as e:
if attempt == attempts - 1:
raise
last_exc = e
time.sleep(base_delay * (2**attempt) + random.uniform(0, 0.25))
assert last_exc is not None
raise last_exc
return wrapper
return decorator
class ApifyClient:
BASE_URL = "https://api.apify.com/v2"
POST_ACTOR = "apimaestro~linkedin-post-detail"
POST_COMMENTS_ACTOR = (
"apimaestro~linkedin-post-comments-replies-engagements-scraper-no-cookies"
)
PROFILE_COMMENTS_ACTOR = "apimaestro~linkedin-profile-comments"
POST_ENGAGERS_ACTOR = (
"scraping_solutions~linkedin-posts-engagers-likers-and-commenters-no-cookies"
)
def __init__(self, token: Optional[str] = None, timeout: float = 180.0):
load_env()
self.token = token or os.getenv("APIFY_TOKEN")
if not self.token:
raise ApifyError(
"APIFY_TOKEN not set. Export it or pass token= explicitly."
)
self.timeout = timeout
self._session = requests.Session()
self._cache: OrderedDict[str, tuple[float, Any]] = OrderedDict()
# ---- Post body --------------------------------------------------------
def fetch_post(
self, post_url: str, *, force_refresh: bool = False
) -> dict[str, Any]:
"""Return the post body, author, and engagement stats for one post.
Args:
post_url: Any of LinkedIn's three URN URL shapes works.
force_refresh: If True, bypass cache and re-fetch from Apify.
Returns:
Dict with keys: text, authorName, authorProfileUrl, urn, shareUrn,
canShare, url, numLikes, numComments, numShares, postedAtISO, plus
extra metadata. `shareUrn` is the reshare parent URN
(`urn:li:share:*` / `urn:li:ugcPost:*`). `postedAtISO` carries the
actor's object (`{timestamp, date}`) rather than an ISO string, which
the name suggests and no caller currently relies on.
"""
items = self._run_sync(
self.POST_ACTOR, {"post_urls": [post_url]}, force_refresh=force_refresh
)
if not items:
raise ApifyError(f"no post returned for {post_url}")
post = self._normalize_post(items[0])
if not post.get("text") and not post.get("authorName"):
# apimaestro returns a nulled shell (job_title "This post cannot be
# displayed") for private, removed, or login-walled posts. Treat it
# as unavailable so callers fall back to asking the user to paste.
raise ApifyError(
f"post not retrievable (private, removed, or login-walled): {post_url}"
)
return post
@staticmethod
def _normalize_post(raw: dict[str, Any]) -> dict[str, Any]:
"""Flatten apimaestro/linkedin-post-detail's nested response to the flat
post contract the skills consume. Keeps the raw payload under `_raw`."""
post = raw.get("post") or {}
author = raw.get("author") or {}
stats = raw.get("stats") or {}
urn = post.get("urn") or {}
def _urn(prefix: str, value: Any) -> Optional[str]:
return f"{prefix}{value}" if value else None
share_urn = (
_urn("urn:li:share:", urn.get("share_urn"))
or _urn("urn:li:ugcPost:", urn.get("ugcPost_urn"))
)
activity_urn = _urn("urn:li:activity:", urn.get("activity_urn"))
return {
"text": post.get("text"),
"urn": activity_urn or share_urn,
"shareUrn": share_urn,
# apimaestro does not expose canShare; leave None so reshare only
# blocks on an explicit False (LinkedIn still rejects if disabled).
"canShare": None,
"url": post.get("url"),
"type": post.get("type"),
"authorName": author.get("name"),
"authorHeadline": author.get("headline"),
"authorProfileUrl": author.get("profile_url"),
"authorFollowers": author.get("followers"),
"numLikes": stats.get("total_reactions"),
"numComments": stats.get("comments"),
"numShares": stats.get("shares"),
"reactions": stats.get("reactions"),
"postedAtISO": post.get("created_at"),
"isReshare": raw.get("is_reshared"),
"resharedPost": raw.get("reshared_post"),
"_raw": raw,
}
# ---- Post comments ----------------------------------------------------
#: What the comments actor will sort by. Its own default is "most recent".
COMMENT_SORT_ORDERS = ("most relevant", "most recent")
def fetch_post_comments(
self,
*,
post_id: str,
max_items: int = 20,
sort_order: str = "most relevant",
scrape_replies: bool = False,
force_refresh: bool = False,
) -> list[dict[str, Any]]:
"""Return comments and their replies on a post.
Args:
post_id: Activity ID, ugcPost ID, or full post URL.
max_items: Cap on comments returned. The actor's schema caps `limit`
at 100, so a larger number is clamped rather than honoured.
sort_order: "most relevant" or "most recent". We default to the
former, against the actor's own default, because a busy post's
newest comments carry no replies at all: measured on a post with
514 comments, the newest 20 held zero reply threads while the 20
most relevant held five. Every caller in this bundle needs the
thread structure, so "most recent" would quietly return none.
Pass it explicitly when the user asks for the newest.
scrape_replies: Accepted and ignored. The actor has no such input;
replies come back either way, in each comment's `replies` list.
Kept so existing callers do not break.
force_refresh: Bypass cache.
"""
if sort_order not in self.COMMENT_SORT_ORDERS:
raise ValueError(
f"sort_order must be one of {self.COMMENT_SORT_ORDERS}, got {sort_order!r}"
)
items = self._run_sync(
self.POST_COMMENTS_ACTOR,
{
"postIds": [post_id],
# Actor input schema calls this `limit` (max 100). Sending
# `maxItems` is silently ignored and every run bills 100 rows.
"limit": min(max_items, 100),
"sortOrder": sort_order,
# No `scrapeReplies` here on purpose: it is not in the actor's
# input schema, so sending it never did anything. The actor
# ("...-post-comments-replies-engagements-scraper...") returns
# replies unconditionally.
},
force_refresh=force_refresh,
)
# The actor appends a run-summary object ({"summary": {...}}) alongside
# the comments (and returns it alone when a post has zero comments).
# Drop it so callers only ever see real comment records.
return [it for it in items if isinstance(it, dict) and "summary" not in it]
# ---- Profile (user) recent comments ----------------------------------
def fetch_user_recent_comments(
self,
*,
username: str,
result_limit: int = 30,
force_refresh: bool = False,
) -> list[dict[str, Any]]:
"""Return a user's most recent comments across LinkedIn.
`result_limit` is clamped to the actor's schema maximum of 3000.
"""
return self._run_sync(
self.PROFILE_COMMENTS_ACTOR,
# Actor takes `usernames` (array) and `limit`; the singular
# `username` / `resultLimit` pair is ignored and bills 100 rows.
{"usernames": [username], "limit": min(result_limit, 3000)},
force_refresh=force_refresh,
)
# ---- Post engagers (likers + commenters) -----------------------------
#: What the engagers actor can return. It answers for one of these per run.
ENGAGER_TYPES = ("likers", "commenters", "reshares")
def fetch_post_engagers(
self,
*,
post_url: str,
max_items: int = 50,
types: Sequence[str] = ("likers", "commenters"),
force_refresh: bool = False,
) -> list[dict[str, Any]]:
"""Return the people who liked or commented on a post.
The actor takes a required `type` and answers for that one audience per
run, defaulting to `likers`. Omitting it, as this client did until
v1.1.2, quietly returned likers only while the docstring and
`linkedin-engager-analytics` both promised commenters too.
Args:
post_url: The post to read.
max_items: Cap on records returned in total, not per type. The
budget is split evenly across `types`, so the cost of a call
does not change with how many audiences it asks for.
types: Which audiences to fetch, from `ENGAGER_TYPES`. One actor run
is billed per entry.
force_refresh: Bypass cache.
"""
wanted = tuple(dict.fromkeys(types))
unknown = [t for t in wanted if t not in self.ENGAGER_TYPES]
if not wanted or unknown:
raise ValueError(
f"types must be a non-empty subset of {self.ENGAGER_TYPES}, got {types!r}"
)
if max_items <= 0:
raise ValueError("max_items must be greater than 0")
per_type = max(1, min(max_items // len(wanted), 3000))
engagers: list[dict[str, Any]] = []
for kind in wanted:
rows = self._run_sync(
self.POST_ENGAGERS_ACTOR,
# Actor takes `resultsLimit`, not `maxItems`, and `type` is
# required. Copy rather than mutate: these dicts are cached.
{"urls": [post_url], "resultsLimit": per_type, "type": kind},
force_refresh=force_refresh,
)
engagers += [
{**r, "type": r.get("type", kind)} for r in rows if isinstance(r, dict)
]
return engagers[:max_items]
# ---- Cache helpers ----------------------------------------------------
@staticmethod
def _cache_key(actor_id: str, payload: dict[str, Any]) -> str:
return f"{actor_id}::{json.dumps(payload, sort_keys=True, default=str)}"
def _cache_get(self, key: str) -> Optional[Any]:
entry = self._cache.get(key)
if entry is None:
return None
ts, value = entry
if time.time() - ts > CACHE_TTL_SECONDS:
del self._cache[key]
return None
self._cache.move_to_end(key)
return deepcopy(value)
def _cache_put(self, key: str, value: Any) -> None:
self._cache[key] = (time.time(), deepcopy(value))
self._cache.move_to_end(key)
while len(self._cache) > CACHE_MAX_ENTRIES:
self._cache.popitem(last=False)
# ---- Internals --------------------------------------------------------
def _run_sync(
self,
actor_id: str,
payload: dict[str, Any],
*,
force_refresh: bool = False,
) -> list[dict[str, Any]]:
key = self._cache_key(actor_id, payload)
if not force_refresh:
cached = self._cache_get(key)
if cached is not None:
return cached
data = self._do_request(actor_id, payload)
if not isinstance(data, list):
raise ApifyError(
f"Unexpected response from Apify actor {actor_id}: "
f"expected a list, got {type(data).__name__}"
)
self._cache_put(key, data)
return data
@_retry()
def _do_request(
self, actor_id: str, payload: dict[str, Any]
) -> Any:
# The token goes in the Authorization header, never the query string.
# A token in the URL leaks into proxy logs, shell history, error traces
# and Referer headers; a header does not.
url = f"{self.BASE_URL}/acts/{actor_id}/run-sync-get-dataset-items"
r = self._session.post(
url,
json=payload,
headers={
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json",
},
timeout=self.timeout,
)
if r.status_code >= 400:
try:
body = r.json()
except Exception:
body = {"error": r.text[:500]}
raise ApifyError(f"HTTP {r.status_code}: {body}")
try:
data = r.json()
except ValueError as e:
raise ApifyError(
f"non-JSON response: {r.text[:300]}"
) from e
if isinstance(data, dict) and "error" in data:
raise ApifyError(f"actor failed: {data['error']}")
return data
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |