-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 添加 WikipediaTopViewsSource 并增强查询源优先级系统 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Disaster-Terminator
wants to merge
1
commit into
main
Choose a base branch
from
feature/query-sources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| """API clients module""" | ||
|
|
||
Disaster-Terminator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from .dashboard_client import DashboardClient | ||
|
|
||
| __all__ = ["DashboardClient"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| """ | ||
| Dashboard API Client | ||
|
|
||
| Fetches points data from Microsoft Rewards Dashboard API. | ||
| """ | ||
|
|
||
| import logging | ||
| import re | ||
| from typing import Any | ||
|
|
||
| from playwright.async_api import Page | ||
|
|
||
| from constants import API_ENDPOINTS, REWARDS_URLS | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class DashboardClient: | ||
| """Client for fetching data from Microsoft Rewards Dashboard API""" | ||
|
|
||
| def __init__(self, page: Page): | ||
| """ | ||
| Initialize Dashboard client | ||
|
|
||
| Args: | ||
| page: Playwright Page object | ||
| """ | ||
| self.page = page | ||
| self._cached_points: int | None = None | ||
| base = REWARDS_URLS.get("dashboard", "https://rewards.bing.com") | ||
| self._base_url = base.rstrip("/") | ||
|
|
||
| async def get_current_points(self) -> int | None: | ||
| """ | ||
| Get current points from Dashboard API | ||
|
|
||
| Attempts to fetch points via API call first, falls back to | ||
| parsing page content if API fails. | ||
|
|
||
| Returns: | ||
| Points balance or None if unable to determine | ||
| """ | ||
| try: | ||
| points = await self._fetch_points_via_api() | ||
| if points is not None and points >= 0: | ||
| self._cached_points = points | ||
| return points | ||
| except TimeoutError as e: | ||
| logger.warning(f"API request timeout: {e}") | ||
| except ConnectionError as e: | ||
| logger.warning(f"API connection error: {e}") | ||
| except Exception as e: | ||
| logger.warning(f"API call failed: {e}") | ||
|
|
||
| try: | ||
| points = await self._fetch_points_via_page_content() | ||
| if points is not None and points >= 0: | ||
| self._cached_points = points | ||
| return points | ||
| except Exception as e: | ||
| logger.debug(f"Page content parsing failed: {e}") | ||
|
|
||
| return self._cached_points | ||
|
|
||
| async def _fetch_points_via_api(self) -> int | None: | ||
| """ | ||
| Fetch points via internal API endpoint | ||
|
|
||
| Returns: | ||
| Points balance or None | ||
| """ | ||
| try: | ||
| api_url = f"{self._base_url}{API_ENDPOINTS['dashboard_balance']}" | ||
| response = await self.page.evaluate( | ||
| f""" | ||
| async () => {{ | ||
| try {{ | ||
| const resp = await fetch('{api_url}', {{ | ||
| method: 'GET', | ||
| credentials: 'include' | ||
| }}); | ||
| if (!resp.ok) return null; | ||
| return await resp.json(); | ||
| }} catch {{ | ||
| return null; | ||
| }} | ||
| }} | ||
| """ | ||
| ) | ||
|
|
||
| if response and isinstance(response, dict): | ||
| available = response.get("availablePoints") | ||
| balance = response.get("pointsBalance") | ||
| points = available if available is not None else balance | ||
| if points is not None: | ||
| try: | ||
| return int(points) | ||
| except (ValueError, TypeError): | ||
| pass | ||
|
|
||
| except Exception as e: | ||
| logger.debug(f"API fetch error: {e}") | ||
|
|
||
| return None | ||
|
|
||
| async def _fetch_points_via_page_content(self) -> int | None: | ||
| """ | ||
| Extract points from page content as fallback | ||
|
|
||
| Returns: | ||
| Points balance or None | ||
| """ | ||
| try: | ||
| content = await self.page.content() | ||
|
|
||
| patterns = [ | ||
| r'"availablePoints"\s*:\s*(\d+)', | ||
| r'"pointsBalance"\s*:\s*(\d+)', | ||
| r'"totalPoints"\s*:\s*(\d+)', | ||
| ] | ||
|
|
||
| for pattern in patterns: | ||
| match = re.search(pattern, content) | ||
| if match: | ||
| points = int(match.group(1)) | ||
| if 0 <= points <= 1000000: | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return points | ||
|
|
||
| except Exception as e: | ||
| logger.debug(f"Page content extraction error: {e}") | ||
|
|
||
| return None | ||
|
|
||
| async def get_dashboard_data(self) -> dict[str, Any] | None: | ||
| """ | ||
| Fetch full dashboard data | ||
|
|
||
| Returns: | ||
| Dashboard data dict or None | ||
| """ | ||
| try: | ||
| api_url = f"{self._base_url}{API_ENDPOINTS['dashboard_data']}" | ||
| response = await self.page.evaluate( | ||
| f""" | ||
| async () => {{ | ||
| try {{ | ||
| const resp = await fetch('{api_url}', {{ | ||
| method: 'GET', | ||
| credentials: 'include' | ||
| }}); | ||
| if (!resp.ok) return null; | ||
| return await resp.json(); | ||
| }} catch {{ | ||
| return null; | ||
| }} | ||
| }} | ||
| """ | ||
| ) | ||
|
|
||
| if response is not None and isinstance(response, dict): | ||
| return dict(response) | ||
|
|
||
| except TimeoutError as e: | ||
| logger.warning(f"Dashboard API timeout: {e}") | ||
| except ConnectionError as e: | ||
| logger.warning(f"Dashboard API connection error: {e}") | ||
| except Exception as e: | ||
| logger.warning(f"Dashboard API error: {e}") | ||
|
|
||
| return None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.