Compare commits
3 Commits
ce2777bb75
...
83d787c900
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83d787c900 | ||
|
|
07e97ca7f7 | ||
|
|
17af959da4 |
290
script.py
290
script.py
@@ -1,195 +1,151 @@
|
|||||||
import json
|
import json
|
||||||
import requests
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from api.lib import DatabaseManager
|
from api.lib import DatabaseManager
|
||||||
from chat_session import ChatSessionManager
|
from chat_session import ChatSessionManager
|
||||||
|
|
||||||
|
apk = "wx98dfa35ad7e4b271"
|
||||||
# 配置常量
|
aps = "MeudTZMWrJlyRMXrIrbo5QqHeLeCb_dzSueYiaawKR4"
|
||||||
class Config:
|
caps = "Vt7mgU5cWc3NCeiFgwi4T8oOv_xShQLkYrqavhMy_AM"
|
||||||
"""配置类,存储API密钥和常量"""
|
import requests
|
||||||
CORP_ID = "wx98dfa35ad7e4b271"
|
|
||||||
EXTERNAL_CONTACT_SECRET = "MeudTZMWrJlyRMXrIrbo5QqHeLeCb_dzSueYiaawKR4"
|
|
||||||
MESSAGE_AUDIT_SECRET = "Vt7mgU5cWc3NCeiFgwi4T8oOv_xShQLkYrqavhMy_AM"
|
|
||||||
|
|
||||||
# API端点
|
|
||||||
TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
|
|
||||||
GROUP_LIST_URL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list"
|
|
||||||
GROUP_INFO_URL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get"
|
|
||||||
INNER_GROUP_URL = "https://qyapi.weixin.qq.com/cgi-bin/msgaudit/groupchat/get"
|
|
||||||
|
|
||||||
|
|
||||||
# API工具类
|
def get_token(key, secret):
|
||||||
class WeWorkAPI:
|
resp = requests.post("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + key + "&corpsecret=" + secret)
|
||||||
"""企业微信API工具类"""
|
result = resp.json()
|
||||||
|
return result["access_token"]
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.external_token = self._get_token(Config.CORP_ID, Config.EXTERNAL_CONTACT_SECRET)
|
|
||||||
self.audit_token = self._get_token(Config.CORP_ID, Config.MESSAGE_AUDIT_SECRET)
|
|
||||||
|
|
||||||
def _get_token(self, key: str, secret: str) -> str:
|
at = get_token(apk, aps)
|
||||||
"""获取访问令牌"""
|
cat = get_token(apk, caps)
|
||||||
resp = requests.post(f"{Config.TOKEN_URL}?corpid={key}&corpsecret={secret}")
|
|
||||||
result = resp.json()
|
|
||||||
return result["access_token"]
|
|
||||||
|
|
||||||
def get_group_list(self, user_ids: list) -> list:
|
|
||||||
"""获取群聊列表"""
|
|
||||||
resp = requests.post(f"{Config.GROUP_LIST_URL}?access_token={self.external_token}", json={
|
|
||||||
"status_filter": 0,
|
|
||||||
"owner_filter": {
|
|
||||||
"userid_list": user_ids
|
|
||||||
},
|
|
||||||
"limit": 1000
|
|
||||||
})
|
|
||||||
result = resp.json()
|
|
||||||
print(f"获取群聊列表结果: {result}")
|
|
||||||
return result.get("group_chat_list", [])
|
|
||||||
|
|
||||||
def get_group_info(self, group_id: str) -> dict:
|
def get_group_list(users: list):
|
||||||
"""获取群聊详细信息"""
|
resp = requests.post("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list?access_token=" + at, json={
|
||||||
resp = requests.post(f"{Config.GROUP_INFO_URL}?access_token={self.external_token}", json={
|
"status_filter": 0,
|
||||||
"chat_id": group_id,
|
"owner_filter": {
|
||||||
|
"userid_list": users
|
||||||
|
},
|
||||||
|
"limit": 1000
|
||||||
|
})
|
||||||
|
result = resp.json()
|
||||||
|
print(result)
|
||||||
|
return result["group_chat_list"]
|
||||||
|
|
||||||
|
|
||||||
|
def get_inner_group_info(gid: str):
|
||||||
|
resp = requests.post("https://qyapi.weixin.qq.com/cgi-bin/msgaudit/groupchat/get?access_token=" + cat, json={
|
||||||
|
"roomid": gid
|
||||||
|
})
|
||||||
|
result = resp.json()
|
||||||
|
print(result)
|
||||||
|
return result["group_chat"]
|
||||||
|
|
||||||
|
|
||||||
|
def get_group_info(group_list):
|
||||||
|
mapping = {}
|
||||||
|
for gid in group_list:
|
||||||
|
print(gid)
|
||||||
|
url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token=" + at
|
||||||
|
resp = requests.post(url, json={
|
||||||
|
"chat_id": gid,
|
||||||
"need_name": 1
|
"need_name": 1
|
||||||
})
|
})
|
||||||
result = resp.json()
|
result = resp.json()
|
||||||
print(f"获取群聊信息 {group_id}: {result}")
|
print(result)
|
||||||
return result
|
if "group_chat" in result:
|
||||||
|
mapping[gid] = result["group_chat"]
|
||||||
def get_inner_group_info(self, group_id: str) -> dict:
|
else:
|
||||||
"""获取内部群聊信息"""
|
mapping[gid] = result['errcode']
|
||||||
resp = requests.post(f"{Config.INNER_GROUP_URL}?access_token={self.audit_token}", json={
|
with open("groupchat.json", "w", encoding="utf-8") as f:
|
||||||
"roomid": group_id
|
json.dump(mapping, f, ensure_ascii=False, indent=4)
|
||||||
})
|
|
||||||
result = resp.json()
|
|
||||||
print(f"获取内部群聊信息 {group_id}: {result}")
|
|
||||||
return result.get("group_chat", {})
|
|
||||||
|
|
||||||
|
|
||||||
# 数据库操作类
|
def update_user_group_info():
|
||||||
class DatabaseOperations:
|
dm = DatabaseManager('db/data', online_db_uri="zysl-log/wework/chat_msg/db/data.db")
|
||||||
"""数据库操作类"""
|
conn = dm.get_connection()
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
def __init__(self, db_path: str, online_db_uri: str):
|
m = cursor.execute("select id from 'group' where id = name").fetchall()
|
||||||
self.db_manager = DatabaseManager(db_path, online_db_uri=online_db_uri)
|
group_list = []
|
||||||
self.wework_api = WeWorkAPI()
|
for gid in m:
|
||||||
|
gid = gid[0]
|
||||||
|
group_list.append(gid)
|
||||||
|
|
||||||
def get_group_ids_from_db(self) -> list:
|
get_group_info(group_list)
|
||||||
"""从数据库获取群聊ID列表"""
|
with open("groupchat.json", "r", encoding="utf-8") as f:
|
||||||
conn = self.db_manager.get_connection()
|
mapping = json.load(f)
|
||||||
cursor = conn.cursor()
|
for gid, info in mapping.items():
|
||||||
|
if isinstance(info, int):
|
||||||
# 获取需要更新的群聊ID
|
print(gid, info)
|
||||||
result = cursor.execute("select id from 'group' where id = name").fetchall()
|
for gid in mapping:
|
||||||
group_ids = [row[0] for row in result]
|
if isinstance(mapping[gid], int):
|
||||||
|
print(gid, mapping[gid])
|
||||||
cursor.close()
|
else:
|
||||||
return group_ids
|
cursor.execute("update 'group' set name = ?, owner = ? where id = ?",
|
||||||
|
(mapping[gid]["name"], mapping[gid]["owner"], gid))
|
||||||
def update_group_info_in_db(self, group_mapping: dict):
|
cursor.execute("insert or ignore into 'group' (id, name, owner) values (?, ?, ?)",
|
||||||
"""更新数据库中的群聊信息"""
|
(gid, mapping[gid]["name"], mapping[gid]["owner"]))
|
||||||
conn = self.db_manager.get_connection()
|
print(mapping[gid]["member_list"])
|
||||||
cursor = conn.cursor()
|
for member in mapping[gid]["member_list"]:
|
||||||
|
cursor.execute("insert or ignore into 'user' (id,name,nick_name,unionid) values (?, ?,?, ?)",
|
||||||
for group_id, group_info in group_mapping.items():
|
(member['userid'], member['name'], member['group_nickname'], member.get('unionid', '')))
|
||||||
if isinstance(group_info, int):
|
cursor.execute("update 'user' set name=?,nick_name=?,unionid = ? where id = ?",
|
||||||
print(f"群聊 {group_id} 错误码: {group_info}")
|
(member['name'], member['group_nickname'], member.get('unionid', ''), member['userid']))
|
||||||
continue
|
conn.commit()
|
||||||
|
|
||||||
# 更新群聊信息
|
|
||||||
cursor.execute("""
|
|
||||||
INSERT OR IGNORE INTO 'group' (id, name, owner)
|
|
||||||
VALUES (?, ?, ?)
|
|
||||||
""", (group_id, group_info["name"], group_info["owner"]))
|
|
||||||
|
|
||||||
cursor.execute("""
|
|
||||||
UPDATE 'group' SET name = ?, owner = ?
|
|
||||||
WHERE id = ?
|
|
||||||
""", (group_info["name"], group_info["owner"], group_id))
|
|
||||||
|
|
||||||
# 更新成员信息
|
|
||||||
for member in group_info["member_list"]:
|
|
||||||
cursor.execute("""
|
|
||||||
INSERT OR IGNORE INTO 'user' (id, name, nick_name, unionid)
|
|
||||||
VALUES (?, ?, ?, ?)
|
|
||||||
""", (member['userid'], member['name'], member['group_nickname'], member.get('unionid', '')))
|
|
||||||
|
|
||||||
cursor.execute("""
|
|
||||||
UPDATE 'user' SET name = ?, nick_name = ?, unionid = ?
|
|
||||||
WHERE id = ?
|
|
||||||
""", (member['name'], member['group_nickname'], member.get('unionid', ''), member['userid']))
|
|
||||||
|
|
||||||
conn.commit()
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
|
|
||||||
# 主要业务逻辑
|
def generate_hourly_intervals(start_time_str):
|
||||||
class MainProcessor:
|
"""
|
||||||
"""主处理器类"""
|
生成从开始时间到结束时间之间的每一个小时节点
|
||||||
|
时间格式: "YYYY-MM-DD-HH"
|
||||||
|
"""
|
||||||
|
# 解析时间字符串
|
||||||
|
start_parts = start_time_str.split("-")
|
||||||
|
end_parts = datetime.today().strftime("%Y-%m-%d-%H").split("-")
|
||||||
|
|
||||||
def __init__(self):
|
# 创建datetime对象
|
||||||
self.db_ops = DatabaseOperations('db/data', "zysl-log/wework/chat_msg/db/data.db")
|
start_dt = datetime(int(start_parts[0]), int(start_parts[1]), int(start_parts[2]), int(start_parts[3]))
|
||||||
|
end_dt = datetime(int(end_parts[0]), int(end_parts[1]), int(end_parts[2]), int(end_parts[3]))
|
||||||
|
|
||||||
def process_group_info(self):
|
# 计算时间差(小时)
|
||||||
"""处理群聊信息"""
|
time_diff = end_dt - start_dt
|
||||||
print("开始处理群聊信息...")
|
total_hours = int(time_diff.total_seconds() / 3600)
|
||||||
|
|
||||||
# 获取群聊ID列表
|
# 生成每个小时节点
|
||||||
group_ids = self.db_ops.get_group_ids_from_db()
|
current_dt = start_dt
|
||||||
print(f"需要处理的群聊数量: {len(group_ids)}")
|
hour_intervals = []
|
||||||
|
|
||||||
# 获取群聊信息
|
for i in range(total_hours + 1):
|
||||||
group_mapping = {}
|
hour_str = current_dt.strftime("%Y-%m-%d-%H")
|
||||||
for group_id in group_ids:
|
hour_intervals.append(hour_str)
|
||||||
print(f"处理群聊: {group_id}")
|
|
||||||
result = self.db_ops.wework_api.get_group_info(group_id)
|
|
||||||
|
|
||||||
if "group_chat" in result:
|
# 移动到下一个小时
|
||||||
group_mapping[group_id] = result["group_chat"]
|
current_dt += timedelta(hours=1)
|
||||||
else:
|
|
||||||
group_mapping[group_id] = result.get('errcode', 'unknown_error')
|
|
||||||
|
|
||||||
# 保存到文件
|
return hour_intervals[1:]
|
||||||
with open("groupchat.json", "w", encoding="utf-8") as f:
|
|
||||||
json.dump(group_mapping, f, ensure_ascii=False, indent=4)
|
|
||||||
|
|
||||||
# 更新数据库
|
|
||||||
self.db_ops.update_group_info_in_db(group_mapping)
|
|
||||||
print("群聊信息处理完成")
|
|
||||||
|
|
||||||
def process_chat_sessions(self, year: int, month: int, start_day: int, end_day: int):
|
|
||||||
"""处理聊天会话"""
|
|
||||||
print(f"开始处理聊天会话: {year}-{month}-{start_day} 到 {year}-{month}-{end_day}")
|
|
||||||
|
|
||||||
chat_session = ChatSessionManager(self.db_ops.db_manager)
|
|
||||||
|
|
||||||
# 处理每个小时的聊天会话
|
|
||||||
for day in range(start_day, end_day):
|
|
||||||
for hour in range(24):
|
|
||||||
chat_session.process_chat_session(year, month, day, hour)
|
|
||||||
|
|
||||||
# 更新问答数据
|
|
||||||
start_date = f"{year}-{month:02d}-{start_day:02d} 00:00:00"
|
|
||||||
end_date = f"{year}-{month:02d}-{end_day:02d} 00:00:00"
|
|
||||||
chat_session.update_qa_data(start_date, end_date)
|
|
||||||
|
|
||||||
print("聊天会话处理完成")
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
"""主运行方法"""
|
|
||||||
print("开始执行主程序...")
|
|
||||||
|
|
||||||
# 处理聊天会话
|
|
||||||
self.process_chat_sessions(2025, 12, 25, 26)
|
|
||||||
|
|
||||||
# 处理群聊信息
|
|
||||||
self.process_group_info()
|
|
||||||
|
|
||||||
# 上传数据库
|
|
||||||
self.db_ops.db_manager.upload_db()
|
|
||||||
print("程序执行完成")
|
|
||||||
|
|
||||||
|
|
||||||
# 主函数
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
processor = MainProcessor()
|
db_manager = DatabaseManager('db/data', online_db_uri="zysl-log/wework/chat_msg/db/data.db")
|
||||||
processor.run()
|
# update messages info
|
||||||
|
chat_session = ChatSessionManager(db_manager)
|
||||||
|
result = \
|
||||||
|
db_manager.execute_query("select datetime from 'information' order by datetime desc limit 1", fetch_all=True)[0]
|
||||||
|
datetime_str = result["datetime"]
|
||||||
|
intervals = generate_hourly_intervals(datetime_str)
|
||||||
|
start_year, start_month, start_day, start_hour = intervals[0].split("-")
|
||||||
|
end_year, end_month, end_day, end_hour = intervals[-1].split("-")
|
||||||
|
for interval in intervals:
|
||||||
|
year, month, day, hour = interval.split("-")
|
||||||
|
chat_session.process_chat_session(int(year), int(month), int(day), int(hour))
|
||||||
|
|
||||||
|
chat_session.update_qa_data(f"{start_year}-{start_month}-{start_day} {start_hour}:00:00",
|
||||||
|
f"{end_year}-{end_month}-{end_day} {end_hour}:59:59")
|
||||||
|
# update user group info
|
||||||
|
|
||||||
|
update_user_group_info()
|
||||||
|
# upload db
|
||||||
|
|
||||||
|
db_manager.upload_db()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user