Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加更多控件类型和新增获取指定聊天窗口的聊天记录,并按时间信息分组的功能 #70

Merged
merged 3 commits into from
Sep 17, 2024

Conversation

xieyumc
Copy link
Contributor

@xieyumc xieyumc commented Sep 15, 2024

增加更多控件类型

我获取聊天记录时,发现有两个控件无法识别

“以下是新信息”

image

这个控件在微信里显示的是“以下是新信息”,但在list_item_control.Name里却是"以下为新消息",有点怪

增加了新的条件:

# 或者是新消息通知
elif "以下为新消息" in list_item_control.Name:
      value = 6

“英文版的对方打开红包”

image

之前把"Red packet"加入了判断条件,但没想到微信的命名规范没统一,这次的是"Red Packet"(Packet的“P”是大写)

所以应该把控件的字母全部都转化为小写再比较

elif "红包" in list_item_control.Name or "red packet" in list_item_control.Name.lower():

新增获取指定聊天窗口的聊天记录,并按时间信息分组的功能

在处理聊天记录时,我发现更多时候我们并不知道需要获取多少条聊天记录,而是需要获取一个时间节点的消息,如昨天的消息

所以我新增了一个方法get_dialogs_by_time_blocks,会以时间块来分区聊天记录,返回嵌套列表,元素是一串以时间消息分割的消息列表,例如:

[
[('时间信息', '', '2024年9月11日 9:01'), ('用户发送', 'Queen', '测试'), ('用户发送', 'Queen', 'hi')]
[('时间信息', '', '2024年9月12日 18:27'), ('用户发送', 'Queen', '你好')], 
[('时间信息', '', '2024年9月14日 11:01'), ('用户发送', 'Queen', '你好收到'), ('用户发送', 'Queen', '好的收到')]
]

这样处理聊天记录时更方便了

Copy link
Owner

@LTEnjoy LTEnjoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢你的贡献!我已经检查运行过代码了,没有什么大问题。 但是在按照时间分组的函数里面,有一个逻辑问题是你把current_group初始化为空列表[]。这会导致如果搜索到的第一批聊天记录里的第一条不是时间消息,程序也会把这条消息作为一个新的分组的第一条消息。实际上是不应该的,因为所有分组的第一条消息都应该是时间消息。我对程序做了一下改动,你可以参考检查一下:

def get_dialogs_by_time_blocks(self, name: str, n_time_blocks: int, search_user: bool = True) -> List[List]:
    """
    获取指定聊天窗口的聊天记录,并按时间信息分组。
    Args:
        name: 聊天窗口的姓名
        n_time_blocks: 获取的时间分块数量
        search_user: 是否需要搜索用户
    Return:
        groups: 聊天记录列表,每个元素为一个时间分块内的消息列表
    """
    n_msg = n_time_blocks * 5
    prev_dialogs = None
    groups = []
    while True:
        dialogs = self.get_dialogs(name, n_msg, search_user)

        # 如果获取的dialogs和之前一样,说明没有更多消息了,退出循环
        if prev_dialogs == dialogs:
            break
        # 分组逻辑调整:处理顺序改为从最新消息到最早消息
        groups = []
        current_group = None

        # 遍历所有消息,按照时间信息分组
        for msg in dialogs:
            # 遇见时间信息则新建一个分组
            if msg[0] == '时间信息':
                # 将上一个分组加入到groups中
                if current_group is not None:
                    groups.append(current_group)

                # 初始化新的分组
                current_group = [msg]

            elif current_group is not None:
                current_group.append(msg)

        # 将最后一个分组加入到groups中
        if current_group is not None:
            groups.append(current_group)

        # 获取n_time_blocks个时间块,取groups的最后n_time_blocks个元素
        if len(groups) >= n_time_blocks:
            groups = groups[-n_time_blocks:]
            break
        else:
            prev_dialogs = dialogs
            n_msg *= 2
            search_user = False  # 后续不需要再次搜索用户

    return groups

@xieyumc
Copy link
Contributor Author

xieyumc commented Sep 17, 2024

😯确实是我疏忽了,感谢指出错误

我按照你的代码修改了这部分逻辑,麻烦你再Review一下

@LTEnjoy LTEnjoy merged commit 4a39114 into LTEnjoy:main Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants