|
@@ -0,0 +1,36 @@
|
|
1
|
+from typing import Dict
|
|
2
|
+
|
|
3
|
+from pydantic import BaseModel, PrivateAttr
|
|
4
|
+from million.model.message import Message
|
|
5
|
+
|
|
6
|
+_default_file_path = './DNS'
|
|
7
|
+
|
|
8
|
+class DNS_solver(BaseModel):
|
|
9
|
+ file_path:str = _default_file_path
|
|
10
|
+
|
|
11
|
+ _bank: Dict[str, int] | None = PrivateAttr(None)
|
|
12
|
+
|
|
13
|
+ def solve(self, msg: Message) -> int:
|
|
14
|
+ if self._bank == None:
|
|
15
|
+ self._bank = self.load(_default_file_path)
|
|
16
|
+
|
|
17
|
+ k = self._get_key(msg)
|
|
18
|
+ if k and k in self._bank: return self._bank[k]
|
|
19
|
+
|
|
20
|
+ return None
|
|
21
|
+
|
|
22
|
+ def load(self, file_name: str) -> Dict[str, int]:
|
|
23
|
+ result = {}
|
|
24
|
+ with open(file_name, 'r') as f:
|
|
25
|
+ for line in f:
|
|
26
|
+ a,b = line.split()
|
|
27
|
+ result[a] = int(b)
|
|
28
|
+
|
|
29
|
+ return result
|
|
30
|
+
|
|
31
|
+ def _get_key(self, msg: Message) -> str:
|
|
32
|
+
|
|
33
|
+ # look into msg attributes
|
|
34
|
+ # find uri
|
|
35
|
+ return (msg.share or None) and msg.share.link or \
|
|
36
|
+ (msg.gifs or None) and msg.gifs[0].uri
|