Skip to content

Commit

Permalink
#72 Add dry-run option to Slack app
Browse files Browse the repository at this point in the history
  • Loading branch information
astropenguin committed Oct 31, 2021
1 parent 319809d commit d99421b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions arxiv_post/apps/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,18 @@


# runtime functions
def post(articles: Sequence[Article], webhook_url: str) -> None:
def post(articles: Sequence[Article], webhook_url: str, dry_run: bool) -> None:
"""Post articles to Slack."""
for article in articles:
try:
_post(webhook_url, json=to_payload(article))
logger.debug(f"Posted {article.arxiv_url}")
payload = to_payload(article)

if not dry_run:
_post(webhook_url, json=payload)

logger.debug(f"Posted an article ({article.arxiv_url})")
except TOMLDecodeError:
logger.warn(f"Failed to post {article.arxiv_url}")
logger.warn(f"Failed to post an article ({article.arxiv_url})")


def to_payload(article: Article) -> Dict[str, Any]:
Expand Down
4 changes: 3 additions & 1 deletion arxiv_post/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def cmd_slack(
n_concurrent: int = N_CONCURRENT,
timeout: float = TIMEOUT,
webhook_url: str = "",
dry_run: bool = False,
debug: bool = False,
) -> None:
"""Translate and post articles to Slack.
Expand All @@ -63,6 +64,7 @@ def cmd_slack(
n_concurrent: Number of simultaneous execution.
timeout: Timeout for each post execution (in seconds).
webhook_url: URL of Slack incoming webhook.
dry_run: If True, articles are not posted to Slack.
debug: If True, debug-level log messages are shown.
Returns:
Expand Down Expand Up @@ -94,7 +96,7 @@ def cmd_slack(
timeout=timeout,
)

return slack.post(translated, webhook_url)
return slack.post(translated, webhook_url, dry_run)


def cli() -> None:
Expand Down

0 comments on commit d99421b

Please sign in to comment.