File size: 6,806 Bytes
e98cc10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
Type Definitions for Medium-MCP

This module provides TypedDict, dataclass, and enum definitions for
type-safe development with mypy strict mode enabled.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum
from typing import Literal, NotRequired, TypedDict


# =============================================================================
# ENUMS
# =============================================================================


class OutputFormat(Enum):
    """Output format options for scraped content."""

    MARKDOWN = "markdown"
    HTML = "html"
    BOTH = "both"


class ScrapeTier(Enum):
    """Scraping tier identifiers for tracking source."""

    CACHE = "cache"
    GRAPHQL = "graphql"
    HTTPX = "httpx"
    BROWSER = "browser"
    WAYBACK = "wayback"
    VISION = "vision"
    CROSS_SOURCE = "cross_source"


class CircuitState(Enum):
    """Circuit breaker states."""

    CLOSED = "closed"
    OPEN = "open"
    HALF_OPEN = "half_open"


class LogLevel(Enum):
    """Logging levels."""

    DEBUG = "DEBUG"
    INFO = "INFO"
    WARNING = "WARNING"
    ERROR = "ERROR"
    CRITICAL = "CRITICAL"


# =============================================================================
# TYPED DICTS - Author & Publication
# =============================================================================


class AuthorInfo(TypedDict):
    """Author information structure."""

    name: str
    username: str
    bio: NotRequired[str]
    avatar_url: NotRequired[str]
    followers: NotRequired[int]
    following: NotRequired[int]


class PublicationInfo(TypedDict):
    """Publication/publication information."""

    name: str
    slug: str
    description: NotRequired[str]
    followers: NotRequired[int]
    url: NotRequired[str]


# =============================================================================
# TYPED DICTS - Article Data
# =============================================================================


class ArticleMetadata(TypedDict):
    """Metadata about an article."""

    url: str
    title: str
    subtitle: NotRequired[str]
    author: AuthorInfo
    publication: NotRequired[str]
    tags: list[str]
    reading_time: int
    claps: NotRequired[int]
    responses: NotRequired[int]
    is_paywalled: bool
    published_at: NotRequired[str]
    updated_at: NotRequired[str]


class ArticleContent(TypedDict):
    """Article content in various formats."""

    markdown: str
    html: NotRequired[str]
    word_count: int
    images: list[str]
    code_blocks: NotRequired[list[str]]


class ScrapeResult(TypedDict):
    """Complete result from a scrape operation."""

    metadata: ArticleMetadata
    content: ArticleContent
    source_tier: str
    cached: bool
    scraped_at: str
    embedding: NotRequired[list[float]]


class ArticleRecord(TypedDict):
    """Database record for cached articles."""

    url: str
    title: str
    author: str
    markdown_content: str
    html_content: NotRequired[str]
    tags: str  # JSON serialized
    is_paywalled: bool
    scraped_at: str
    embedding: NotRequired[str]  # JSON serialized


# =============================================================================
# TYPED DICTS - Search & Discovery
# =============================================================================


class SearchResult(TypedDict):
    """Individual search result."""

    title: str
    url: str
    author: str
    publication: NotRequired[str]
    preview: NotRequired[str]
    reading_time: NotRequired[int]


class TagFeed(TypedDict):
    """Tag-based feed result."""

    tag: str
    articles: list[SearchResult]
    count: int


# =============================================================================
# TYPED DICTS - API Responses
# =============================================================================


class GraphQLPostResponse(TypedDict):
    """GraphQL API post response structure."""

    id: str
    title: str
    content: dict  # bodyModel
    creator: AuthorInfo
    tags: NotRequired[list[dict]]
    clapCount: NotRequired[int]


class ApolloState(TypedDict):
    """Apollo client __APOLLO_STATE__ structure."""

    ROOT_QUERY: dict
    # Additional dynamic keys


# =============================================================================
# TYPED DICTS - MCP Tool Outputs
# =============================================================================


class ScrapeToolOutput(TypedDict):
    """Output schema for medium_scrape tool."""

    title: str
    author: AuthorInfo
    markdown_content: str
    html_content: NotRequired[str]
    tags: list[str]
    reading_time: int
    is_paywalled: bool
    source_tier: str
    url: str


class BatchToolOutput(TypedDict):
    """Output schema for medium_batch tool."""

    success: list[ScrapeToolOutput]
    failed: list[dict]
    stats: dict


class ReportToolOutput(TypedDict):
    """Output schema for medium_report tool."""

    title: str
    executive_summary: str
    key_insights: list[str]
    articles_analyzed: int
    generated_at: str


# =============================================================================
# DATACLASSES - Options & Config
# =============================================================================


@dataclass
class ScrapeOptions:
    """Options for scraping operations."""

    force_refresh: bool = False
    recursive_depth: int = 0
    enable_enhancements: bool = False
    output_format: OutputFormat = OutputFormat.BOTH
    timeout_seconds: int = 30
    max_retries: int = 3


@dataclass
class HTTPConfig:
    """HTTP client configuration."""

    max_connections: int = 100
    max_keepalive_connections: int = 20
    keepalive_expiry: float = 5.0
    connect_timeout: float = 5.0
    read_timeout: float = 30.0
    http2: bool = True


@dataclass
class ResilienceConfig:
    """Circuit breaker and retry configuration."""

    failure_threshold: int = 5
    recovery_timeout: int = 300
    rate_limit_requests: int = 10
    rate_limit_period: int = 60


@dataclass
class ScraperConfig:
    """Complete scraper configuration."""

    max_workers: int = 5
    headless: bool = True
    http: HTTPConfig = field(default_factory=HTTPConfig)
    resilience: ResilienceConfig = field(default_factory=ResilienceConfig)


# =============================================================================
# TYPE ALIASES
# =============================================================================

# Common callback signatures
ProgressCallback = type[None] | type["async def(str) -> None"]  # type: ignore
ErrorHandler = type[None] | type["def(Exception, str) -> None"]  # type: ignore

# JSON-like structures
JSONValue = str | int | float | bool | None | list["JSONValue"] | dict[str, "JSONValue"]
JSONDict = dict[str, JSONValue]