| 67 | self.ddgs = DDGS(proxy=PROXY['https'], timeout=50) if len(PROXY) != 0 else DDGS(timeout=50) |
| 68 | |
| 69 | def query(self, text: str, max_results: int) -> List[Dict[str, Any]]: |
| 70 | initial_delay = 1 |
| 71 | max_retries = 3 |
| 72 | |
| 73 | for attempt in range(max_retries): |
| 74 | try: |
| 75 | time.sleep(random.choice([i for i in range(5, 10 + 20 * WORLD_SIZE, 5)])) # Avoid frequent requests |
| 76 | response = list(self.ddgs.text(' '.join(text.strip("'").split(' ')[:100]), max_results=max_results)) |
| 77 | return response[:max_results] |
| 78 | except Exception as e: |
| 79 | error_message = str(e) |
| 80 | if "202" in error_message or "Accepted" in error_message: |
| 81 | delay = initial_delay * (2 ** attempt) + random.uniform(0, 1) |
| 82 | print(f"Received 202 status code, waiting {delay:.2f} seconds before retrying... (Attempt {attempt + 1}/{max_retries})") |
| 83 | time.sleep(delay) |
| 84 | elif isinstance(e, RequestException): |
| 85 | print(f"Network error: {e}") |
| 86 | time.sleep(random.uniform(1, 3)) |
| 87 | else: |
| 88 | print(f"Unknown error: {e}") |
| 89 | raise ValueError |
| 90 | ## API: Search Engine Retrieval + Screenshot of top section |
| 91 | class DDGSQueryRun: |
| 92 | name = "duckduckgo_search" |