|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -import json |
3 | | -from typing import Any |
4 | | - |
5 | | -import requests |
6 | | - |
7 | | -from src.api.base import Base |
8 | | -from src.utils import UA |
9 | | - |
10 | | -from .article import ArticleAPI |
11 | | -from .chat import ChatAPI |
12 | | -from .chatroom import ChatRoomAPI |
13 | | -from .config import GLOBAL_CONFIG |
14 | | -from .user import UserAPI |
15 | | - |
16 | | - |
17 | | -class UserInfo(object): |
18 | | - |
19 | | - def __init__(self, username: str, password: str, api_key: str) -> None: |
20 | | - self.username = username |
21 | | - self.password = password |
22 | | - self.api_key = api_key |
23 | | - self.ws: dict[str, Any] = {} |
24 | | - self.in_chatroom = False |
25 | | - |
26 | | - def online(self, *funcs) -> None: |
27 | | - if (len(self.api_key) != 0): |
28 | | - API.set_token(self.api_key) |
29 | | - API.set_current_user(self.username) |
30 | | - else: |
31 | | - API.login(self.username, self.password) |
32 | | - self.api_key = API.api_key |
33 | | - for func in funcs: |
34 | | - func() |
35 | | - self.in_chatroom = True |
36 | | - GLOBAL_CONFIG.auth_config.username = self.username |
37 | | - GLOBAL_CONFIG.auth_config.password = self.password |
38 | | - GLOBAL_CONFIG.auth_config.key = self.api_key |
39 | | - API.user_key_write_to_config_file() |
40 | | - |
41 | | - def out_chatroom(self) -> None: |
42 | | - if 'fishpi.cn/chat-room-channel' in self.ws: |
43 | | - self.ws['fishpi.cn/chat-room-channel'].stop() |
44 | | - self.in_chatroom = False |
45 | | - |
46 | | - def offline(self) -> None: |
47 | | - keys = list(self.ws.keys()) |
48 | | - for key in keys: |
49 | | - self.ws[key].stop() |
50 | | - self.in_chatroom = False |
51 | | - |
52 | | - def out_chat(self) -> None: |
53 | | - if 'fishpi.cn/chat-channel' in self.ws: |
54 | | - self.ws['fishpi.cn/chat-channel'].stop() |
55 | | - |
56 | | - def chat(self, func) -> None: |
57 | | - self.out_chat() |
58 | | - self.out_chatroom() |
59 | | - func() |
60 | | - |
61 | | - |
62 | | -class FishPi(Base): |
63 | | - def __init__(self): |
64 | | - self.sockpuppets: dict[str, UserInfo] = {} |
65 | | - self.user = UserAPI() |
66 | | - self.chatroom = ChatRoomAPI() |
67 | | - self.article = ArticleAPI() |
68 | | - self.chat = ChatAPI() |
69 | | - super().__init__(self) |
70 | | - |
71 | | - def set_token(self, key): |
72 | | - super().set_token(key) |
73 | | - self.user.set_token(key) |
74 | | - self.chatroom.set_token(key) |
75 | | - self.article.set_token(key) |
76 | | - self.chat.set_token(key) |
77 | | - |
78 | | - def get_current_user(self): |
79 | | - return self.sockpuppets[self.current_user] |
80 | | - |
81 | | - def get_breezemoons(self, page: int = 1, size: int = 10) -> dict | None: |
82 | | - res = requests.get( |
83 | | - f'{GLOBAL_CONFIG.host}/api/breezemoons?p={page}&size={size}', headers={'User-Agent': UA}) |
84 | | - print(res.text) |
85 | | - response = json.loads(res.text) |
86 | | - if 'code' in response and response['code'] == 0: |
87 | | - return response['breezemoons'] |
88 | | - else: |
89 | | - print(response['msg']) |
90 | | - return None |
91 | | - |
92 | | - |
93 | | -API = FishPi() |
0 commit comments