MCPcopy Create free account
hub / github.com/codenameone/CodenameOne / publish_to_devto

Function publish_to_devto

scripts/website/syndicate_blog_posts.py:353–386  ·  view source on GitHub ↗
(post: Post, body_markdown: str, api_key: str, draft: bool = False)

Source from the content-addressed store, hash-verified

351
352
353def publish_to_devto(post: Post, body_markdown: str, api_key: str, draft: bool = False) -> dict[str, Any]:
354 payload: dict[str, Any] = {
355 "article": {
356 "title": post.title,
357 "body_markdown": body_markdown,
358 "published": not draft,
359 "canonical_url": post.canonical_url,
360 "tags": DEVTO_TAGS,
361 "description": str(post.front_matter.get("description") or "")[:250] or None,
362 }
363 }
364 cover = post.cover_image
365 if cover:
366 payload["article"]["main_image"] = cover
367 payload["article"] = {k: v for k, v in payload["article"].items() if v is not None}
368
369 response = http_post_json(
370 "https://dev.to/api/articles",
371 headers={"api-key": api_key, "Accept": "application/vnd.forem.api-v1+json"},
372 payload=payload,
373 )
374 article_id = response.get("id")
375 # The URL field on dev.to returns the public canonical URL of the article,
376 # but for unpublished drafts that URL 404s for anyone who is not the author.
377 # In draft mode point users at the dashboard, where the draft is editable.
378 if draft and article_id:
379 url = f"https://dev.to/dashboard/{article_id}/edit"
380 else:
381 url = response.get("url") or response.get("canonical_url")
382 return {
383 "id": article_id,
384 "url": url,
385 "syndicated_at": dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds"),
386 }
387
388
389def parse_args(argv: list[str]) -> argparse.Namespace:

Callers 1

mainFunction · 0.85

Calls 3

http_post_jsonFunction · 0.85
getMethod · 0.65
nowMethod · 0.45

Tested by

no test coverage detected