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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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import httpx
import time
from typing import Dict, List, Optional, Any, Union
import json
from dataclasses import asdict
import asyncio
from datetime import datetime
from itertools import islice

from .types import (
    StackOverflowQuestion,
    StackOverflowAnswer,
    StackOverflowComment,
    SearchResult,
    SearchResultComments
)

from .env import (
    MAX_REQUEST_PER_WINDOW,
    RATE_LIMIT_WINDOW_MS,
    RETRY_AFTER_MS
)

STACKOVERFLOW_API = "https://api.stackexchange.com/2.3"
BATCH_SIZE = 100  # API limit for batch requests

class StackExchangeAPI:
    def __init__(self, api_key: Optional[str] = None):
        self.api_key = api_key
        self.request_timestamps = []
        self.client = httpx.AsyncClient(timeout=30.0)
    
    async def close(self):
        await self.client.aclose()
    
    def _check_rate_limit(self) -> bool:
        now = time.time() * 1000
                
        self.request_timestamps = [
            ts for ts in self.request_timestamps
            if now - ts < RATE_LIMIT_WINDOW_MS
        ]
        
        if len(self.request_timestamps) >= MAX_REQUEST_PER_WINDOW:
            return False
        
        self.request_timestamps.append(now)
        return True
    
    async def _with_rate_limit(self, func, *args, retries=3, attempts=10, **kwargs):
        """Execute a function with rate limiting.

        Args:
            func (_type_): Function to execute with rate limiting
            retries (int, optional): Number of retries after API rate limit error. Defaults to 3.
            attempts (int, optional): Number of times to retry after hitting local rate limit. Defaults to 10.

        Raises:
            Exception: When maximum rate limiting attempts are exceeded
            e: Original error if retries are exhausted

        Returns:
            Any: Result from the function
        """
        if retries is None:
            retries = 3
        
        if attempts <= 0:
            raise Exception("Maximum rate limiting attempts exceeded")
    
        if not self._check_rate_limit():
            print("Rate limit exceeded, waiting before retry")
            await asyncio.sleep(RETRY_AFTER_MS / 1000)
            return await self._with_rate_limit(func, *args, retries=retries, attempts=attempts-1, **kwargs)
        
        try: 
            return await func(*args, **kwargs)
        except httpx.HTTPStatusError as e:
            if retries > 0 and e.response.status_code == 429:
                print("Rate limit hit (429), retrying after delay...")
                await asyncio.sleep(RETRY_AFTER_MS/1000)
                return await self._with_rate_limit(func, *args, retries=retries-1, attempts=attempts, **kwargs)
            raise e
    
    async def fetch_batch_answers(self, question_ids: List[int]) -> Dict[int, List[StackOverflowAnswer]]:
        """Fetch answers for multiple questions in a single API call.
        
        Args:
            question_ids (List[int]): List of Stack Overflow question IDs
            
        Returns:
            Dict[int, List[StackOverflowAnswer]]: Dictionary mapping question IDs to their answers
        """
        if not question_ids:
            return {}
        
        result = {}
        
        # Process in batches of BATCH_SIZE (API limit)
        for i in range(0, len(question_ids), BATCH_SIZE):
            batch = question_ids[i:i+BATCH_SIZE]
            ids_string = ";".join(str(qid) for qid in batch)
            
            params = {
                "site": "stackoverflow",
                "sort": "votes",
                "order": "desc",
                "filter": "withbody",
                "pagesize": "100"
            }
            
            if self.api_key:
                params["key"] = self.api_key
            
            async def _do_fetch():
                response = await self.client.get(
                    f"{STACKOVERFLOW_API}/questions/{ids_string}/answers",
                    params=params
                )
                response.raise_for_status()
                return response.json()
            
            data = await self._with_rate_limit(_do_fetch)
            
            for answer_data in data.get("items", []):
                question_id = answer_data.get("question_id")
                if question_id not in result:
                    result[question_id] = []
                
                answer = StackOverflowAnswer(
                    answer_id=answer_data.get("answer_id"),
                    question_id=question_id,
                    score=answer_data.get("score", 0),
                    is_accepted=answer_data.get("is_accepted", False),
                    body=answer_data.get("body", ""),
                    creation_date=answer_data.get("creation_date", 0),
                    last_activity_date=answer_data.get("last_activity_date", 0),
                    link=answer_data.get("link", ""),
                    owner=answer_data.get("owner")
                )
                result[question_id].append(answer)
        
        return result
    
    async def fetch_batch_comments(self, post_ids: List[int]) -> Dict[int, List[StackOverflowComment]]:
        """Fetch comments for multiple posts in a single API call.
        
        Args:
            post_ids (List[int]): List of Stack Overflow post IDs (questions or answers)
            
        Returns:
            Dict[int, List[StackOverflowComment]]: Dictionary mapping post IDs to their comments
        """
        if not post_ids:
            return {}
        
        result = {}
        
        # Process in batches of BATCH_SIZE (API limit)
        for i in range(0, len(post_ids), BATCH_SIZE):
            batch = post_ids[i:i+BATCH_SIZE]
            ids_string = ";".join(str(pid) for pid in batch)
            
            params = {
                "site": "stackoverflow",
                "sort": "votes",
                "order": "desc",
                "filter": "withbody",
                "pagesize": "100" 
            }
            
            if self.api_key:
                params["key"] = self.api_key
            
            async def _do_fetch():
                response = await self.client.get(
                    f"{STACKOVERFLOW_API}/posts/{ids_string}/comments",
                    params=params
                )
                response.raise_for_status()
                return response.json()
            
            data = await self._with_rate_limit(_do_fetch)
            
            for comment_data in data.get("items", []):
                post_id = comment_data.get("post_id")
                if post_id not in result:
                    result[post_id] = []
                
                comment = StackOverflowComment(
                    comment_id=comment_data.get("comment_id"),
                    post_id=post_id,
                    score=comment_data.get("score", 0),
                    body=comment_data.get("body", ""),
                    creation_date=comment_data.get("creation_date", 0),
                    owner=comment_data.get("owner")
                )
                result[post_id].append(comment)
        
        return result
    
    async def advanced_search(
        self,
        query: Optional[str] = None,
        tags: Optional[List[str]] = None,
        excluded_tags: Optional[List[str]] = None,
        min_score: Optional[int] = None,
        title: Optional[str] = None,
        body: Optional[str] = None,
        answers: Optional[int] = None,
        has_accepted_answer: Optional[bool] = None,
        views: Optional[int] = None,
        url: Optional[str] = None,
        user_id: Optional[int] = None,
        is_closed: Optional[bool] = None,
        is_wiki: Optional[bool] = None,
        is_migrated: Optional[bool] = None,
        has_notice: Optional[bool] = None,
        from_date: Optional[datetime] = None,
        to_date: Optional[datetime] = None,
        sort_by: Optional[str] = "votes",
        limit: Optional[int] = 5,
        include_comments: bool = False,
        retries: Optional[int] = 3
    ) -> List[SearchResult]:
        """Advanced search for Stack Overflow questions with many filter options."""
        params = {
            "site": "stackoverflow",
            "sort": sort_by,
            "order": "desc",
            "filter": "withbody"
        }
        
        if query:
            params["q"] = query
            
        if tags:
            params["tagged"] = ";".join(tags)
            
        if excluded_tags:
            params["nottagged"] = ";".join(excluded_tags)
            
        if title:
            params["title"] = title
            
        if body:
            params["body"] = body
            
        if answers is not None:
            params["answers"] = str(answers)
            
        if has_accepted_answer is not None:
            params["accepted"] = "true" if has_accepted_answer else "false"
            
        if views is not None:
            params["views"] = str(views)
            
        if url:
            params["url"] = url
            
        if user_id is not None:
            params["user"] = str(user_id)
            
        if is_closed is not None:
            params["closed"] = "true" if is_closed else "false"
            
        if is_wiki is not None:
            params["wiki"] = "true" if is_wiki else "false"
            
        if is_migrated is not None:
            params["migrated"] = "true" if is_migrated else "false"
            
        if has_notice is not None:
            params["notice"] = "true" if has_notice else "false"
            
        if from_date:
            params["fromdate"] = str(int(from_date.timestamp()))
            
        if to_date:
            params["todate"] = str(int(to_date.timestamp()))
            
        if limit:
            params["pagesize"] = str(limit)
        
        if self.api_key:
            params["key"] = self.api_key
            
        async def _do_search():
            response = await self.client.get(f"{STACKOVERFLOW_API}/search/advanced", params=params)
            response.raise_for_status()
            return response.json()
        
        data = await self._with_rate_limit(_do_search, retries=retries)
        
        questions = []
        question_ids = []
        
        for question_data in data.get("items", []):
            if min_score is not None and question_data.get("score", 0) < min_score:
                continue
            
            question = StackOverflowQuestion(
                question_id=question_data.get("question_id"),
                title=question_data.get("title", ""),
                body=question_data.get("body", ""),
                score=question_data.get("score", 0),
                answer_count=question_data.get("answer_count", 0),
                is_answered=question_data.get("is_answered", False),
                accepted_answer_id=question_data.get("accepted_answer_id"),
                creation_date=question_data.get("creation_date", 0),
                last_activity_date=question_data.get("last_activity_date", 0),
                view_count=question_data.get("view_count", 0),
                tags=question_data.get("tags", []),
                link=question_data.get("link", ""),
                is_closed=question_data.get("closed_date") is not None,
                owner=question_data.get("owner")
            )
            questions.append(question)
            question_ids.append(question.question_id)
            
        answers_by_question = await self.fetch_batch_answers(question_ids)
        
        results = []
        
        if include_comments:
            all_post_ids = question_ids.copy()
            for qid, answers in answers_by_question.items():
                all_post_ids.extend([a.answer_id for a in answers])
            
            # Batch fetch all comments
            all_comments = await self.fetch_batch_comments(all_post_ids)
            
            # Construct results with comments
            for question in questions:
                question_answers = answers_by_question.get(question.question_id, [])
                
                # Create comment structure
                question_comments = all_comments.get(question.question_id, [])
                answer_comments = {}
                
                for answer in question_answers:
                    answer_comments[answer.answer_id] = all_comments.get(answer.answer_id, [])
                
                comments = SearchResultComments(
                    question=question_comments,
                    answers=answer_comments
                )
                
                results.append(SearchResult(
                    question=question,
                    answers=question_answers,
                    comments=comments
                ))
        else:
            for question in questions:
                question_answers = answers_by_question.get(question.question_id, [])
                results.append(SearchResult(
                    question=question,
                    answers=question_answers,
                    comments=None
                ))
                
        return results
    
    async def search_by_query(
        self, 
        query: str,
        tags: Optional[List[str]] = None,
        excluded_tags: Optional[List[str]] = None,
        min_score: Optional[int] = None,
        title: Optional[str] = None,
        body: Optional[str] = None,
        has_accepted_answer: Optional[bool] = None,
        answers: Optional[int] = None,
        sort_by: Optional[str] = "votes",
        limit: Optional[int] = 5,
        include_comments: bool = False,
        retries: Optional[int] = 3
    ) -> List[SearchResult]:
        """Search Stack Overflow for questions matching a query with additional filters."""
        return await self.advanced_search(
            query=query,
            tags=tags,
            excluded_tags=excluded_tags,
            min_score=min_score,
            title=title,
            body=body,
            has_accepted_answer=has_accepted_answer,
            answers=answers,
            sort_by=sort_by,
            limit=limit,
            include_comments=include_comments,
            retries=retries
        )
    
    async def fetch_answers(self, question_id: int) -> List[StackOverflowAnswer]:
        """Fetch answers for a specific question.
        
        Note: This is kept for backward compatibility, but new code should
        use fetch_batch_answers for better performance.
        """
        answers_dict = await self.fetch_batch_answers([question_id])
        return answers_dict.get(question_id, [])
    
    async def fetch_comments(self, post_id: int) -> List[StackOverflowComment]:
        """Fetch comments for a specific post.
        
        Note: This is kept for backward compatibility, but new code should
        use fetch_batch_comments for better performance.
        """
        comments_dict = await self.fetch_batch_comments([post_id])
        return comments_dict.get(post_id, [])
    
    async def get_question(self, question_id: int, include_comments: bool = True) -> SearchResult:
        """Get a specific question by ID."""
        params = {
            "site": "stackoverflow",
            "filter": "withbody"
        }
        
        if self.api_key:
            params["key"] = self.api_key
        
        async def _do_fetch():
            response = await self.client.get(
                f"{STACKOVERFLOW_API}/questions/{question_id}", 
                params=params
            )
            response.raise_for_status()
            return response.json()
        
        data = await self._with_rate_limit(_do_fetch)
        
        if not data.get("items"):
            raise ValueError(f"Question with ID {question_id} not found")
        
        question_data = data["items"][0]
        question = StackOverflowQuestion(
            question_id=question_data.get("question_id"),
            title=question_data.get("title", ""),
            body=question_data.get("body", ""),
            score=question_data.get("score", 0),
            answer_count=question_data.get("answer_count", 0),
            is_answered=question_data.get("is_answered", False),
            accepted_answer_id=question_data.get("accepted_answer_id"),
            creation_date=question_data.get("creation_date", 0),
            last_activity_date=question_data.get("last_activity_date", 0),
            view_count=question_data.get("view_count", 0),
            tags=question_data.get("tags", []),
            link=question_data.get("link", ""),
            is_closed=question_data.get("closed_date") is not None,
            owner=question_data.get("owner")
        )
        
        answers = await self.fetch_answers(question.question_id)
        
        comments = None
        if include_comments:
            post_ids = [question.question_id] + [answer.answer_id for answer in answers]
            all_comments = await self.fetch_batch_comments(post_ids)
            
            question_comments = all_comments.get(question.question_id, [])
            answer_comments = {}
            
            for answer in answers:
                answer_comments[answer.answer_id] = all_comments.get(answer.answer_id, [])
            
            comments = SearchResultComments(
                question=question_comments,
                answers=answer_comments
            )
        
        return SearchResult(
            question=question,
            answers=answers,
            comments=comments
        )