File size: 1,754 Bytes
f176037
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import asyncio
import json
import logging

from toolbox.live_info_collect.douyin import Douyin
from project_settings import project_path

toolbox_logger = logging.getLogger("toolbox")


class LiveInfoCollectManager(object):
    def __init__(self, live_info_collect_file: str):
        self.live_info_collect_file = live_info_collect_file

        # state
        self.coro_task_dict = dict()

    def get_init_tasks(self):
        with open(self.live_info_collect_file, "r", encoding="utf-8") as f:
            tasks = json.load(f)

        for task in tasks:
            room_platform = task["platform"]
            check_interval = task["check_interval"]
            file_output_file = project_path / task["file_output_file"]

            key = f"{room_platform}"
            if key in self.coro_task_dict.keys():
                continue

            record_task = Douyin(
                platform=room_platform,
                check_interval=check_interval,
                file_output_file=file_output_file,
            )
            self.coro_task_dict[key] = record_task.start()

        future_tasks = [asyncio.create_task(task) for task in self.coro_task_dict.values()]
        return future_tasks

    async def run(self):
        future_tasks = self.get_init_tasks()
        await asyncio.wait(future_tasks)


async def main():
    import log
    from project_settings import project_path, log_directory

    log.setup_size_rotating(log_directory=log_directory)

    live_info_collect_tasks_file = project_path / "data/live_info_collect.json"

    manager = LiveInfoCollectManager(live_info_collect_tasks_file)
    await manager.run()
    return


if __name__ == "__main__":
    asyncio.run(main())