|
|
@@ -31,8 +31,9 @@ CFG = {
|
|
|
# ---- 打桩:伪造 KSClient(feed_list_ios / profile_feed_web / get) ----
|
|
|
|
|
|
class _Resp:
|
|
|
- def __init__(self, j=None, content=b''):
|
|
|
+ def __init__(self, j=None, content=b'', headers=None):
|
|
|
self.j, self.content = j, content
|
|
|
+ self.headers = headers or {}
|
|
|
|
|
|
def json(self):
|
|
|
return self.j
|
|
|
@@ -41,6 +42,9 @@ class _Resp:
|
|
|
class _FakeKS:
|
|
|
ios_pages = {} # (uid, pcursor) -> _Resp
|
|
|
web_pages = {} # (uid, pcursor) -> _Resp
|
|
|
+ profiles = {} # uid -> _Resp(M8 query_profile 桩)
|
|
|
+ opens = {} # photoId -> _Resp(R1 open_share 桩)
|
|
|
+ redirects = {} # url -> Location(短链跟随桩)
|
|
|
gets = [] # 下载请求 URL 记录
|
|
|
dl = [] # [(url前缀, 字节内容)] 下载响应脚本
|
|
|
|
|
|
@@ -53,8 +57,16 @@ class _FakeKS:
|
|
|
def profile_feed_web(self, uid, pcursor=''):
|
|
|
return _FakeKS.web_pages[(uid, pcursor)]
|
|
|
|
|
|
+ def query_profile(self, uid):
|
|
|
+ return _FakeKS.profiles[uid]
|
|
|
+
|
|
|
+ def open_share(self, photo_id, share_object_id=''):
|
|
|
+ return _FakeKS.opens[photo_id]
|
|
|
+
|
|
|
def get(self, url, params=None, **kw):
|
|
|
_FakeKS.gets.append(url)
|
|
|
+ if url in _FakeKS.redirects: # 短链:302 Location 跟随桩
|
|
|
+ return _Resp(j=None, headers={'Location': _FakeKS.redirects[url]})
|
|
|
for pre, body in _FakeKS.dl:
|
|
|
if url.startswith(pre):
|
|
|
return _Resp(content=body)
|
|
|
@@ -72,6 +84,7 @@ def _fresh():
|
|
|
db.conn(CFG)
|
|
|
collect.KSClient = _FakeKS
|
|
|
_FakeKS.ios_pages, _FakeKS.web_pages = {}, {}
|
|
|
+ _FakeKS.profiles, _FakeKS.opens, _FakeKS.redirects = {}, {}, {}
|
|
|
_FakeKS.gets, _FakeKS.dl = [], []
|
|
|
db.exe('DELETE FROM monitoring')
|
|
|
db.exe('DELETE FROM biaoti')
|
|
|
@@ -276,6 +289,76 @@ def test_poll_all():
|
|
|
assert collect.poll_all(cfg) == 0
|
|
|
|
|
|
|
|
|
+def test_batch_add_and_delete_monitor():
|
|
|
+ """M7 批量导入:逐个查重跳过已存在;M10 删除监控账号。"""
|
|
|
+ _fresh()
|
|
|
+ added, skipped = collect.batch_add(['u1', 'u2', 'u1', ''], fenlei='测试组')
|
|
|
+ assert (added, skipped) == (2, 2) # 空 uid 与重复 u1 各计一次跳过
|
|
|
+ rows = db.query('SELECT uid, fenlei FROM monitoring ORDER BY uid')
|
|
|
+ assert [(r['uid'], r['fenlei']) for r in rows] == [('u1', '测试组'), ('u2', '测试组')]
|
|
|
+ # 与既有单个导入共存:add_monitor 先入库后,batch_add 不重复插入
|
|
|
+ collect.add_monitor('丙', 'u3')
|
|
|
+ added2, skipped2 = collect.batch_add(['u1', 'u3', 'u4'])
|
|
|
+ assert (added2, skipped2) == (1, 2)
|
|
|
+ # M10 删除
|
|
|
+ assert collect.delete_monitor('u2') is True
|
|
|
+ assert collect.delete_monitor('u2') is False # 已不存在
|
|
|
+ assert not db.query("SELECT id FROM monitoring WHERE uid='u2'")
|
|
|
+
|
|
|
+
|
|
|
+def test_refresh_monitor_stats():
|
|
|
+ """M8 资料查询:query_profile 响应 → monitoring 粉丝/关注/作品/商品数落库。"""
|
|
|
+ _fresh()
|
|
|
+ collect.add_monitor('甲', 'u1')
|
|
|
+ collect.add_monitor('乙', 'u2')
|
|
|
+ _FakeKS.profiles = {
|
|
|
+ 'u1': _Resp({'userProfile': {'profile': {'user_name': '达人甲Pro'},
|
|
|
+ 'ownerCount': {'fan': 12000, 'follow': 88,
|
|
|
+ 'photo_public': 210},
|
|
|
+ 'extraLink': {'subTitle': '35件商品'}}}),
|
|
|
+ }
|
|
|
+ # u2 无桩 → 查询异常时保留原值不中断
|
|
|
+ n = collect.refresh_monitor_stats(_cfg(), uid='u1')
|
|
|
+ assert n == 1
|
|
|
+ row = db.query("SELECT * FROM monitoring WHERE uid='u1'")[0]
|
|
|
+ assert row['name'] == '达人甲Pro'
|
|
|
+ assert (row['fensishu'], row['guanzhushu'], row['zuopinshu'],
|
|
|
+ row['shangpinshu']) == (12000, 88, 210, 35) # subTitle "N件商品"解析
|
|
|
+ # 全量刷新:u1 更新、u2 桩缺失仅计数跳过
|
|
|
+ n2 = collect.refresh_monitor_stats(_cfg())
|
|
|
+ assert n2 == 1
|
|
|
+ assert 'Pro' in db.query("SELECT name FROM monitoring WHERE uid='u1'")[0]['name']
|
|
|
+
|
|
|
+
|
|
|
+def test_resolve_photo():
|
|
|
+ """R1 单作品解析:分享文本提链接/短链跟随/result 失效判定/item 字段统一。"""
|
|
|
+ _fresh()
|
|
|
+ _FakeKS.opens = {
|
|
|
+ '3xabc123': _Resp({'result': 2100000,
|
|
|
+ 'data': {'feeds': [_feed('3xabc123', caption='磁力好物',
|
|
|
+ likes=55, views=660)]}}),
|
|
|
+ 'dead000': _Resp({'result': 2101001}), # 非成功码 → 作品失效
|
|
|
+ }
|
|
|
+ _FakeKS.redirects = {
|
|
|
+ 'https://v.kuaishou.com/ShortL': 'https://www.kuaishou.com/short-video/3xabc123',
|
|
|
+ }
|
|
|
+ # (1) 短链 → 跟随 Location → 提取 photoId → openShare 解析
|
|
|
+ it = collect.resolve_photo(_cfg(), '看看这个 https://v.kuaishou.com/ShortL 复制打开')
|
|
|
+ assert it and it['videoId'] == '3xabc123' and it['biaoti'] == '磁力好物'
|
|
|
+ assert (it['likes'], it['plays']) == (55, 660)
|
|
|
+ assert any('v.kuaishou.com/ShortL' in u for u in _FakeKS.gets) # 短链确实被请求
|
|
|
+ # (2) 直链(short-video/<id>)不发起跟随请求
|
|
|
+ _FakeKS.gets.clear()
|
|
|
+ it2 = collect.resolve_photo(_cfg(), 'https://www.kuaishou.com/short-video/3xabc123')
|
|
|
+ assert it2 and it2['videoId'] == '3xabc123'
|
|
|
+ assert not _FakeKS.gets # 直链无需 GET
|
|
|
+ # (3) 纯 photoId 直接解析
|
|
|
+ it3 = collect.resolve_photo(_cfg(), 'dead000')
|
|
|
+ assert it3 is None or it3.get('invalid') # 失效码 → invalid 标记
|
|
|
+ # (4) 无 id 可提取 → None
|
|
|
+ assert collect.resolve_photo(_cfg(), '没有链接的纯文本') is None
|
|
|
+
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
_fns = [v for k, v in list(globals().items())
|
|
|
if k.startswith('test_') and callable(v)]
|