152 lines
5.3 KiB
Python
152 lines
5.3 KiB
Python
import json
|
|
from datetime import datetime, timedelta
|
|
|
|
from api.lib import DatabaseManager
|
|
from chat_session import ChatSessionManager
|
|
|
|
apk = "wx98dfa35ad7e4b271"
|
|
aps = "MeudTZMWrJlyRMXrIrbo5QqHeLeCb_dzSueYiaawKR4"
|
|
caps = "Vt7mgU5cWc3NCeiFgwi4T8oOv_xShQLkYrqavhMy_AM"
|
|
import requests
|
|
|
|
|
|
def get_token(key, secret):
|
|
resp = requests.post("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + key + "&corpsecret=" + secret)
|
|
result = resp.json()
|
|
return result["access_token"]
|
|
|
|
|
|
at = get_token(apk, aps)
|
|
cat = get_token(apk, caps)
|
|
|
|
|
|
def get_group_list(users: list):
|
|
resp = requests.post("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list?access_token=" + at, json={
|
|
"status_filter": 0,
|
|
"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
|
|
})
|
|
result = resp.json()
|
|
print(result)
|
|
if "group_chat" in result:
|
|
mapping[gid] = result["group_chat"]
|
|
else:
|
|
mapping[gid] = result['errcode']
|
|
with open("groupchat.json", "w", encoding="utf-8") as f:
|
|
json.dump(mapping, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
def update_user_group_info():
|
|
dm = DatabaseManager('db/data', online_db_uri="zysl-log/wework/chat_msg/db/data.db")
|
|
conn = dm.get_connection()
|
|
cursor = conn.cursor()
|
|
|
|
m = cursor.execute("select id from 'group' where id = name").fetchall()
|
|
group_list = []
|
|
for gid in m:
|
|
gid = gid[0]
|
|
group_list.append(gid)
|
|
|
|
get_group_info(group_list)
|
|
with open("groupchat.json", "r", encoding="utf-8") as f:
|
|
mapping = json.load(f)
|
|
for gid, info in mapping.items():
|
|
if isinstance(info, int):
|
|
print(gid, info)
|
|
for gid in mapping:
|
|
if isinstance(mapping[gid], int):
|
|
print(gid, mapping[gid])
|
|
else:
|
|
cursor.execute("update 'group' set name = ?, owner = ? where id = ?",
|
|
(mapping[gid]["name"], mapping[gid]["owner"], gid))
|
|
cursor.execute("insert or ignore into 'group' (id, name, owner) values (?, ?, ?)",
|
|
(gid, mapping[gid]["name"], mapping[gid]["owner"]))
|
|
print(mapping[gid]["member_list"])
|
|
for member in mapping[gid]["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()
|
|
|
|
|
|
def generate_hourly_intervals(start_time_str):
|
|
"""
|
|
生成从开始时间到结束时间之间的每一个小时节点
|
|
时间格式: "YYYY-MM-DD-HH"
|
|
"""
|
|
# 解析时间字符串
|
|
start_parts = start_time_str.split("-")
|
|
end_parts = datetime.today().strftime("%Y-%m-%d-%H").split("-")
|
|
|
|
# 创建datetime对象
|
|
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]))
|
|
|
|
# 计算时间差(小时)
|
|
time_diff = end_dt - start_dt
|
|
total_hours = int(time_diff.total_seconds() / 3600)
|
|
|
|
# 生成每个小时节点
|
|
current_dt = start_dt
|
|
hour_intervals = []
|
|
|
|
for i in range(total_hours + 1):
|
|
hour_str = current_dt.strftime("%Y-%m-%d-%H")
|
|
hour_intervals.append(hour_str)
|
|
|
|
# 移动到下一个小时
|
|
current_dt += timedelta(hours=1)
|
|
|
|
return hour_intervals[1:]
|
|
|
|
|
|
if __name__ == '__main__':
|
|
db_manager = DatabaseManager('db/data', online_db_uri="zysl-log/wework/chat_msg/db/data.db")
|
|
# 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()
|
|
|