|
19 | 19 | limitations under the License. |
20 | 20 | """ |
21 | 21 | from __future__ import annotations |
| 22 | +import os |
22 | 23 | import types |
23 | 24 | import asyncio |
24 | 25 | import dataclasses |
|
38 | 39 | import json |
39 | 40 | from pyee import AsyncIOEventEmitter |
40 | 41 | from wechaty.exceptions import WechatyOperationError, WechatyPayloadError |
| 42 | +from wechaty.config import config |
41 | 43 | from wechaty_puppet import ( |
42 | 44 | FileBox, |
43 | 45 | RoomQueryFilter, |
|
50 | 52 | # from wechaty.utils import type_check |
51 | 53 | from ..accessory import Accessory |
52 | 54 | from ..config import AT_SEPARATOR, PARALLEL_TASK_NUM |
| 55 | +from wechaty.utils.data_util import save_pickle_data, load_pickle_data |
53 | 56 | from wechaty.utils.async_helper import gather_with_concurrency |
54 | 57 |
|
55 | 58 | if TYPE_CHECKING: |
@@ -222,11 +225,31 @@ async def find_all(cls, |
222 | 225 | """ |
223 | 226 | log.info('Room find_all <%s>', query) |
224 | 227 |
|
225 | | - # 1. load rooms with concurrent tasks |
226 | | - room_ids = await cls.get_puppet().room_search() |
227 | | - rooms: List[Room] = [cls.load(room_id) for room_id in room_ids] |
228 | | - tasks: List[Task] = [asyncio.create_task(room.ready()) for room in rooms] |
229 | | - await gather_with_concurrency(PARALLEL_TASK_NUM, tasks) |
| 228 | + # 0. load from local cache file |
| 229 | + if config.cache_rooms and os.path.exists(config.cache_room_path): |
| 230 | + room_payloads = load_pickle_data(config.cache_room_path) |
| 231 | + assert isinstance(room_payloads, list) |
| 232 | + assert isinstance(room_payloads[0], RoomPayload) |
| 233 | + |
| 234 | + room_payloads: List[RoomPayload] = room_payloads |
| 235 | + rooms = [] |
| 236 | + for room_payload in room_payloads: |
| 237 | + room = cls.load(room_payload.id) |
| 238 | + |
| 239 | + # type: ignore |
| 240 | + room._payload = room_payload |
| 241 | + rooms.append(room) |
| 242 | + else: |
| 243 | + |
| 244 | + # 1. load rooms with concurrent tasks |
| 245 | + room_ids = await cls.get_puppet().room_search() |
| 246 | + rooms: List[Room] = [cls.load(room_id) for room_id in room_ids] |
| 247 | + tasks: List[Task] = [asyncio.create_task(room.ready()) for room in rooms] |
| 248 | + await gather_with_concurrency(PARALLEL_TASK_NUM, tasks) |
| 249 | + |
| 250 | + if config.cache_rooms: |
| 251 | + room_payloads = [room.payload for room in rooms if room.payload is not None] |
| 252 | + save_pickle_data(room_payloads, config.cache_contact_path) |
230 | 253 |
|
231 | 254 | # 2. filter the rooms |
232 | 255 | if not query: |
|
0 commit comments