Paijo commited on
Commit
31c7681
·
verified ·
1 Parent(s): 2f5b470

update app/hunter/strategies/ai.py

Browse files
Files changed (1) hide show
  1. app/hunter/strategies/ai.py +62 -0
app/hunter/strategies/ai.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import logging
3
+ from typing import List
4
+ from app.hunter.strategy import BaseStrategy
5
+
6
+ # Try to import g4f, but handle failure if not installed (though we just installed it)
7
+ try:
8
+ from g4f.client import AsyncClient
9
+ from g4f.Provider import RetryProvider, PollinationsAI, BlackboxPro
10
+
11
+ HAS_G4F = True
12
+ except ImportError:
13
+ HAS_G4F = False
14
+ AsyncClient = None # Ensure attribute exists for patching in tests
15
+ RetryProvider = None
16
+ PollinationsAI = None
17
+ BlackboxPro = None
18
+
19
+ logger = logging.getLogger(__name__)
20
+
21
+
22
+ class AIStrategy(BaseStrategy):
23
+ PROMPT = (
24
+ "Find me 5 working URLS for free proxy lists updated in the last 24 hours. "
25
+ "They should be direct links to raw text files on GitHub, Pastebin, or similar sites. "
26
+ "Do not explain. Just list the URLs starting with https://."
27
+ )
28
+
29
+ @property
30
+ def name(self) -> str:
31
+ return "ai"
32
+
33
+ async def discover(self) -> List[str]:
34
+ if not HAS_G4F:
35
+ logger.warning("g4f library not installed, skipping AI strategy")
36
+ return []
37
+
38
+ try:
39
+ # Simple list of providers that don't usually fail with auth errors
40
+ providers = [PollinationsAI, BlackboxPro]
41
+
42
+ client = AsyncClient(provider=RetryProvider(providers))
43
+
44
+ response = await client.chat.completions.create(
45
+ model="gpt-3.5-turbo",
46
+ messages=[{"role": "user", "content": self.PROMPT}],
47
+ )
48
+
49
+ content = response.choices[0].message.content
50
+ if not content:
51
+ return []
52
+
53
+ return self._extract_urls(content)
54
+
55
+ except Exception as e:
56
+ logger.error(f"AI Strategy failed: {str(e)}")
57
+ return []
58
+
59
+ def _extract_urls(self, text: str) -> List[str]:
60
+ # Regex to find https:// URLs
61
+ url_pattern = re.compile(r'https://[^\s<>"]+|www\.[^\s<>"]+')
62
+ return [url for url in url_pattern.findall(text) if "http" in url]