Files

49 lines
1.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""分析汪礼平跨月重复的转账/红包对象"""
import json, pymysql, re
from collections import defaultdict
cfg = json.load(open('db_config.json'))
conn = pymysql.connect(host=cfg['host'], port=cfg['port'], user=cfg['user'],
password=str(cfg['password']), database=cfg['database'], charset='utf8mb4')
cur = conn.cursor()
cur.execute("""
SELECT recorded_date, amount, note FROM variable_expenses
WHERE member_id=1 AND (category='转账/红包' OR category LIKE '%红包%' OR note LIKE '%微信转账%' OR note LIKE '%微信红包%')
""")
rows = cur.fetchall()
def peer_of(note):
# note 形如 [微信] 转账|对方名|备注 或 转账 | 对方 | 备注
parts = [p.strip() for p in note.replace('[微信] ', '').split('|')]
if len(parts) >= 2:
return parts[1]
m = re.search(r'转账\s*[:|]\s*([^|\s]+)', note)
return None
# 按对方+月份统计
by_peer_month = defaultdict(set) # peer -> set of 'YYYY-MM'
by_peer_total = defaultdict(float)
by_peer_count = defaultdict(int)
for d, amt, note in rows:
p = peer_of(note)
if not p:
p = '(未知)'
month = d.strftime('%Y-%m')
by_peer_month[p].add(month)
by_peer_total[p] += float(amt)
by_peer_count[p] += 1
# 按月数排序,找出跨月重复的
print('=== 转账对象出现月份数(>=2个月视为规律性) ===')
multi = []
for p, months in by_peer_month.items():
if len(months) >= 2:
multi.append((len(months), by_peer_count[p], by_peer_total[p], p, sorted(months)))
multi.sort(key=lambda x: (-x[0], -x[2]))
for nmon, ncnt, tot, p, months in multi:
print(f' {p:16s} 出现{nmon}个月共{ncnt}笔 累计¥{tot:.0f} 月:{months}')
conn.close()