Skip to content

Commit 2249670

Browse files
committed
Stats and traffic sources end their range on your computer's today, not Warsaw's
1 parent 4740c13 commit 2249670

5 files changed

Lines changed: 1033 additions & 5 deletions

File tree

‎substack_api_mcp/core/context.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json as jsonlib
1010
import time
1111
from dataclasses import dataclass, field
12+
from datetime import UTC, datetime, tzinfo
1213
from typing import Any
1314
from urllib.parse import urljoin, urlsplit
1415

@@ -89,6 +90,11 @@ class ToolContext:
8990
_cache: dict[str, tuple[float, str]] = field(default_factory=dict, repr=False)
9091
_http: AsyncSession | None = field(default=None, repr=False)
9192

93+
@property
94+
def user_zone(self) -> tzinfo:
95+
"""This computer's time zone, for "today" and date ranges."""
96+
return datetime.now().astimezone().tzinfo or UTC
97+
9298
def _client(self) -> AsyncSession:
9399
if self._http is None:
94100
headers = {"accept-language": self.session.locale or "en-US,en;q=0.9"}

‎substack_api_mcp/substack/client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def publication(ctx: ToolContext) -> dict[str, Any]:
4949
if await _suspended(ctx):
5050
raise TargetBlockedError(
5151
"Substack suspended this account and hid its publication. The notice and the appeal link are on "
52-
"substack.com in your Cloud Browser."
52+
"substack.com in Chrome."
5353
)
5454
raise ToolError("This Substack account has no publication it can publish to.", ErrorCode.APP_PERMISSION_DENIED)
5555

‎substack_api_mcp/substack/tools/get_publication_stats.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from datetime import datetime, timedelta
66
from typing import TYPE_CHECKING, Any
7-
from zoneinfo import ZoneInfo
87

98
from substack_api_mcp.core.tools import tool
109
from substack_api_mcp.substack.client import call, publication
@@ -55,7 +54,8 @@ async def get_publication_stats(ctx: ToolContext, args: dict[str, Any]) -> Any:
5554
pub = await publication(ctx)
5655
# The dashboard's summary for the last N days: subscribers and ARR at the start and the end.
5756
summary = await call(ctx, "GET", f"{pub['base']}/api/v1/publish-dashboard/summary-v2", params={"range": days})
58-
today = datetime.now(ZoneInfo("Europe/Warsaw")).date()
57+
# The user's calendar day, so a US evening does not end the range on tomorrow (docs/05, 24.10.2026).
58+
today = datetime.now(ctx.user_zone).date()
5959
window = {"from": (today - timedelta(days=days)).isoformat(), "to": today.isoformat(), "offset": 0, "limit": 1}
6060
# Cancelled paid subscriptions in the period (Analytics > Unsubscribes); Substack keeps no count of free ones.
6161
cancels = await call(ctx, "GET", f"{pub['base']}/api/v1/publication/stats/unsubscribes", params=window)

‎substack_api_mcp/substack/tools/list_traffic_sources.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from datetime import datetime, timedelta
66
from typing import TYPE_CHECKING, Any
7-
from zoneinfo import ZoneInfo
87

98
from substack_api_mcp.core.helpers import limit
109
from substack_api_mcp.core.tools import tool
@@ -61,7 +60,8 @@ async def list_traffic_sources(ctx: ToolContext, args: dict[str, Any]) -> Any:
6160
wanted = limit(args, 20, 50) # Substack answers 400 above 50 (22.10.2026)
6261
offset = offset_cursor(args.get("cursor"))
6362
pub = await publication(ctx)
64-
today = datetime.now(ZoneInfo("Europe/Warsaw")).date()
63+
# The user's calendar day, so a US evening does not end the range on tomorrow (docs/05, 24.10.2026).
64+
today = datetime.now(ctx.user_zone).date()
6565
params = {
6666
"from_date": (today - timedelta(days=int(period[:-1]))).isoformat(),
6767
"to_date": today.isoformat(),

0 commit comments

Comments
 (0)