|
1 | 1 | import os |
2 | 2 | from typing import Union |
3 | 3 | import warnings |
| 4 | +import requests |
4 | 5 | from .config import ( |
5 | 6 | DEFAULT_PROVIDER, |
6 | 7 | DEFAULT_PROVIDER_API_KEY, |
@@ -649,6 +650,85 @@ def load(data: dict) -> "BrowserConfig": |
649 | 650 | return config |
650 | 651 | return BrowserConfig.from_kwargs(config) |
651 | 652 |
|
| 653 | + def set_nstproxy( |
| 654 | + self, |
| 655 | + token: str, |
| 656 | + channel_id: str, |
| 657 | + country: str = "ANY", |
| 658 | + state: str = "", |
| 659 | + city: str = "", |
| 660 | + protocol: str = "http", |
| 661 | + session_duration: int = 10, |
| 662 | + ): |
| 663 | + """ |
| 664 | + Fetch a proxy from NSTProxy API and automatically assign it to proxy_config. |
| 665 | +
|
| 666 | + Get your NSTProxy token from: https://app.nstproxy.com/profile |
| 667 | +
|
| 668 | + Args: |
| 669 | + token (str): NSTProxy API token. |
| 670 | + channel_id (str): NSTProxy channel ID. |
| 671 | + country (str, optional): Country code (default: "ANY"). |
| 672 | + state (str, optional): State code (default: ""). |
| 673 | + city (str, optional): City name (default: ""). |
| 674 | + protocol (str, optional): Proxy protocol ("http" or "socks5"). Defaults to "http". |
| 675 | + session_duration (int, optional): Session duration in minutes (0 = rotate each request). Defaults to 10. |
| 676 | +
|
| 677 | + Raises: |
| 678 | + ValueError: If the API response format is invalid. |
| 679 | + PermissionError: If the API returns an error message. |
| 680 | + """ |
| 681 | + |
| 682 | + # --- Validate input early --- |
| 683 | + if not token or not channel_id: |
| 684 | + raise ValueError("[NSTProxy] token and channel_id are required") |
| 685 | + |
| 686 | + if protocol not in ("http", "socks5"): |
| 687 | + raise ValueError(f"[NSTProxy] Invalid protocol: {protocol}") |
| 688 | + |
| 689 | + # --- Build NSTProxy API URL --- |
| 690 | + params = { |
| 691 | + "fType": 2, |
| 692 | + "count": 1, |
| 693 | + "channelId": channel_id, |
| 694 | + "country": country, |
| 695 | + "protocol": protocol, |
| 696 | + "sessionDuration": session_duration, |
| 697 | + "token": token, |
| 698 | + } |
| 699 | + if state: |
| 700 | + params["state"] = state |
| 701 | + if city: |
| 702 | + params["city"] = city |
| 703 | + |
| 704 | + url = "https://api.nstproxy.com/api/v1/generate/apiproxies" |
| 705 | + |
| 706 | + try: |
| 707 | + response = requests.get(url, params=params, timeout=10) |
| 708 | + response.raise_for_status() |
| 709 | + |
| 710 | + data = response.json() |
| 711 | + |
| 712 | + # --- Handle API error response --- |
| 713 | + if isinstance(data, dict) and data.get("err"): |
| 714 | + raise PermissionError(f"[NSTProxy] API Error: {data.get('msg', 'Unknown error')}") |
| 715 | + |
| 716 | + if not isinstance(data, list) or not data: |
| 717 | + raise ValueError("[NSTProxy] Invalid API response — expected a non-empty list") |
| 718 | + |
| 719 | + proxy_info = data[0] |
| 720 | + |
| 721 | + # --- Apply proxy config --- |
| 722 | + self.proxy_config = ProxyConfig( |
| 723 | + server=f"{protocol}://{proxy_info['ip']}:{proxy_info['port']}", |
| 724 | + username=proxy_info["username"], |
| 725 | + password=proxy_info["password"], |
| 726 | + ) |
| 727 | + |
| 728 | + except Exception as e: |
| 729 | + print(f"[NSTProxy] ❌ Failed to set proxy: {e}") |
| 730 | + raise |
| 731 | + |
652 | 732 | class VirtualScrollConfig: |
653 | 733 | """Configuration for virtual scroll handling. |
654 | 734 | |
|
0 commit comments