diff --git a/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt b/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt index d40c9f7..c498a20 100644 --- a/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt +++ b/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt @@ -633,12 +633,17 @@ class HaoRuiAccessibilityService : AccessibilityService() { val accepted = startZhihuCollectSandbox(index) RunLogger.i(TAG, "知乎采集沙盒受理=$accepted(广播 ACTION_TEST_ZHIHU cardIndex=$index)") } - // 微博类目采集沙盒(debug APK;只读采集:类目词条清单 + 单条内容) + // 微博类目采集沙盒(debug APK;只读采集:类目词条清单 + 单条内容;collectOnlyList=仅清单) "com.haorui.app.ACTION_TEST_WEIBO" -> { val categoryId = intent.getStringExtra("categoryId") ?: "hot" val entryIndex = intent.getStringExtra("entryIndex")?.toIntOrNull() ?: 0 - val accepted = startWeiboCollectSandbox(categoryId, entryIndex) - RunLogger.i(TAG, "微博采集沙盒受理=$accepted(广播 ACTION_TEST_WEIBO category=$categoryId entry=$entryIndex)") + val collectOnlyList = intent.getBooleanExtra("collectOnlyList", false) + val accepted = startWeiboCollectSandbox(categoryId, entryIndex, !collectOnlyList) + RunLogger.i( + TAG, + "微博采集沙盒受理=$accepted(广播 ACTION_TEST_WEIBO category=$categoryId " + + "entry=$entryIndex collectOnlyList=$collectOnlyList)", + ) } // 回归测试发布模式切换(草稿/正式;autoPublish 无 UI 开关,走广播) "com.haorui.app.ACTION_SET_DRAFT" -> setAutoPublish(false) @@ -767,52 +772,55 @@ class HaoRuiAccessibilityService : AccessibilityService() { /** * 仅调试 APK 的微博类目采集沙盒(端到端真机验收)。 - * 先拉类目词条清单(用户勾选清单的数据源),再采单条内容全链;只读、不发布、不启调度器。 + * [collectEntry] false=只拉类目词条清单(用户勾选清单的数据源); + * true=清单之后再采单条内容全链。只读、不发布、不启调度器。 */ - fun startWeiboCollectSandbox(categoryId: String, entryIndex: Int): Boolean = synchronized(stepLifecycleLock) { - if (applicationInfo.flags and android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE == 0 || - !::scheduler.isInitialized || scheduler.isRunning || publisher.isWorking || interactor.isWorking || - frontGuard.creditNavigating || !foregroundCoordinator.isIdle()) return@synchronized false - val category = com.haorui.app.crossplatform.source.weibo.WeiboHotCategories.ALL - .firstOrNull { it.id == categoryId } ?: return@synchronized false - launchForegroundTask { - val ticket = foregroundCoordinator.tryEnterLegacy("WEIBO-SANDBOX") ?: return@launchForegroundTask - try { - val machine = com.haorui.app.crossplatform.source.weibo.WeiboHotListCollector( - com.haorui.app.crossplatform.source.weibo.WeiboDevicePort(this@HaoRuiAccessibilityService), - ) - RunLogger.i(TAG, "微博采集沙盒开始:类目「${category.tabLabel}」") - val list = machine.collectCategoryEntries(category) - RunLogger.i( - TAG, - "微博采集沙盒清单: outcome=${list.outcome} phase=${list.phase} exited=${list.exitedSafely} " + - "条目=${list.entries.size} reason=${list.reason}", - ) - list.entries.take(5).forEachIndexed { i, e -> - RunLogger.i(TAG, "微博采集沙盒清单[$i]: ${e.title} heat=${e.heatRaw ?: "未知"} heatMissing=${e.heatMissing}") - } - val result = machine.collectOne(category, entryIndex) - RunLogger.i( - TAG, - "微博采集沙盒结果: outcome=${result.outcome} phase=${result.phase} " + - "exited=${result.exitedSafely} reason=${result.reason}", - ) - result.observation?.let { obs -> + fun startWeiboCollectSandbox(categoryId: String, entryIndex: Int, collectEntry: Boolean = true): Boolean = + synchronized(stepLifecycleLock) { + if (applicationInfo.flags and android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE == 0 || + !::scheduler.isInitialized || scheduler.isRunning || publisher.isWorking || interactor.isWorking || + frontGuard.creditNavigating || !foregroundCoordinator.isIdle()) return@synchronized false + val category = com.haorui.app.crossplatform.source.weibo.WeiboHotCategories.ALL + .firstOrNull { it.id == categoryId } ?: return@synchronized false + launchForegroundTask { + val ticket = foregroundCoordinator.tryEnterLegacy("WEIBO-SANDBOX") ?: return@launchForegroundTask + try { + val machine = com.haorui.app.crossplatform.source.weibo.WeiboHotListCollector( + com.haorui.app.crossplatform.source.weibo.WeiboDevicePort(this@HaoRuiAccessibilityService), + ) + RunLogger.i(TAG, "微博采集沙盒开始:类目「${category.tabLabel}」") + val list = machine.collectCategoryEntries(category) RunLogger.i( TAG, - "微博采集沙盒观察: 词条=${obs.hotEntry?.title?.take(20)} 作者=${obs.author} " + - "时间=${obs.publishedAtText}/${obs.publishedAtMs} 转发=${obs.repostCount} " + - "评论=${obs.commentCount} 赞=${obs.likeCount} 链接=${obs.sourceUrl} 未知=${obs.unknowns}", + "微博采集沙盒清单: outcome=${list.outcome} phase=${list.phase} exited=${list.exitedSafely} " + + "条目=${list.entries.size} reason=${list.reason}", ) + list.entries.take(5).forEachIndexed { i, e -> + RunLogger.i(TAG, "微博采集沙盒清单[$i]: ${e.title} heat=${e.heatRaw ?: "未知"} heatMissing=${e.heatMissing}") + } + if (!collectEntry) return@launchForegroundTask + val result = machine.collectOne(category, entryIndex) + RunLogger.i( + TAG, + "微博采集沙盒结果: outcome=${result.outcome} phase=${result.phase} " + + "exited=${result.exitedSafely} reason=${result.reason}", + ) + result.observation?.let { obs -> + RunLogger.i( + TAG, + "微博采集沙盒观察: 词条=${obs.hotEntry?.title?.take(20)} 作者=${obs.author} " + + "时间=${obs.publishedAtText}/${obs.publishedAtMs} 转发=${obs.repostCount} " + + "评论=${obs.commentCount} 赞=${obs.likeCount} 链接=${obs.sourceUrl} 未知=${obs.unknowns}", + ) + } + } catch (t: Throwable) { + RunLogger.e(TAG, "微博采集沙盒异常: ${t.javaClass.simpleName}:${t.message}", t) + } finally { + foregroundCoordinator.release(ticket) } - } catch (t: Throwable) { - RunLogger.e(TAG, "微博采集沙盒异常: ${t.javaClass.simpleName}:${t.message}", t) - } finally { - foregroundCoordinator.release(ticket) } + true } - true - } /** 新来源仅可在旧流程的完整安全边界入场,不在详情、编辑或回执中途切换。 */ fun tryEnterSourceForeground(sourceId: String): ForegroundCoordinator.Ticket? { diff --git a/app/src/main/java/com/haorui/app/crossplatform/model/CrossPlatformConfig.kt b/app/src/main/java/com/haorui/app/crossplatform/model/CrossPlatformConfig.kt index ecf154d..7b770d5 100644 --- a/app/src/main/java/com/haorui/app/crossplatform/model/CrossPlatformConfig.kt +++ b/app/src/main/java/com/haorui/app/crossplatform/model/CrossPlatformConfig.kt @@ -1,6 +1,9 @@ package com.haorui.app.crossplatform.model import kotlinx.serialization.Serializable +import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.encodeToString import java.util.Locale /** 新链路独立配置。来源选择不等于来源能力已验收。 */ @@ -30,6 +33,8 @@ data class CrossPlatformConfig( val categories: List = DEFAULT_CATEGORIES, /** 微博热搜类目勾选(WeiboHotCategories.id;空=未选择。旧配置缺字段按空处理保持兼容)。 */ val weiboHotCategories: List = emptyList(), + /** 微博热搜词条勾选(类目 id → 勾选的词条标题;重启不丢,重启后榜单已刷新——按标题在新榜单中匹配,匹配不到保留待核)。 */ + val weiboHotTopics: Map> = emptyMap(), ) { fun normalized(): CrossPlatformConfig { val maximumDuration = videoMaxSeconds.coerceIn(20, 4000) @@ -48,6 +53,9 @@ data class CrossPlatformConfig( fansMax = fansMax.coerceIn(0, 1_000_000_000), categories = categories.map { it.trim() }.filter { it in CATEGORY_CATALOGUE }.distinct(), weiboHotCategories = weiboHotCategories.map { it.trim() }.filter { it in WEIBO_CATEGORY_IDS }.distinct(), + weiboHotTopics = weiboHotTopics.entries + .filter { it.key.trim() in WEIBO_CATEGORY_IDS } + .associate { it.key.trim() to it.value.map { t -> t.trim() }.filter { t -> t.isNotEmpty() }.distinct() }, ) } @@ -96,6 +104,7 @@ object CrossPlatformConfigCodec { "fans_gate_enabled" to c.fansGateEnabled.toString(), "fans_max" to c.fansMax.toString(), "categories" to c.categories.joinToString("|"), "weibo_hot_categories" to c.weiboHotCategories.joinToString("|"), + "weibo_hot_topics" to kotlinx.serialization.json.Json.encodeToString(c.weiboHotTopics), ).mapKeys { (key, _) -> PREFIX + key } } @@ -131,6 +140,11 @@ object CrossPlatformConfigCodec { categories = value("categories")?.split('|') ?: d.categories, weiboHotCategories = value("weibo_hot_categories")?.split('|')?.filter { it.isNotBlank() } ?: d.weiboHotCategories, + weiboHotTopics = value("weibo_hot_topics")?.let { raw -> + runCatching { + kotlinx.serialization.json.Json.decodeFromString>>(raw) + }.getOrDefault(d.weiboHotTopics) + } ?: d.weiboHotTopics, ).normalized() return if (validEncoding) decoded else decoded.copy(enabled = false, mode = CrossPlatformConfig.MODE_DRY_RUN) } diff --git a/app/src/main/java/com/haorui/app/crossplatform/ui/CrossPlatformScreens.kt b/app/src/main/java/com/haorui/app/crossplatform/ui/CrossPlatformScreens.kt index 44bfb7a..f659fc9 100644 --- a/app/src/main/java/com/haorui/app/crossplatform/ui/CrossPlatformScreens.kt +++ b/app/src/main/java/com/haorui/app/crossplatform/ui/CrossPlatformScreens.kt @@ -1,5 +1,6 @@ package com.haorui.app.crossplatform.ui +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.ExperimentalLayoutApi @@ -397,6 +398,8 @@ fun TopicsScreen(config: AppConfig, tasks: List, loaded: Boole ) } Text("分类:${config.crossPlatform.categories.joinToString("、").ifBlank { "未选择" }} · ${config.crossPlatform.maxAgeHours} 小时内", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant) + // 微博热搜词条清单:按已选类目拉取真机榜单 → 勾选词条 → 持久化到配置(勾选后系统按所选词条采集) + WeiboHotTopicPicker(config, onConfigChange) when { loadError != null -> StatusBanner("本地任务读取失败:$loadError", StatusLevel.BAD) !loaded -> EmptyPanel("正在读取本地候选", "请稍候") @@ -408,6 +411,95 @@ fun TopicsScreen(config: AppConfig, tasks: List, loaded: Boole selected?.let { prior -> TaskPreview(tasks.firstOrNull { it.id == prior.id } ?: prior) { selected = null } } } +/** + * 微博热搜词条勾选器(用户口径:用户选什么,系统采什么发什么)。 + * 数据流:按已选类目经广播拉真机榜单(ACTION_TEST_WEIBO collectOnlyList)→ 服务日志落词条 → + * 用户核对日志标题后填入下方勾选—— + * ⚠️ 当前为过渡实现:采集窗口期间 UI 无法直接读服务内存,先以日志中转; + * 自动化直读将在采集执行接线时替换。勾选与持久化是真实的。 + */ +@Composable +private fun WeiboHotTopicPicker(config: AppConfig, onConfigChange: (AppConfig) -> Unit) { + val context = LocalContext.current + var expandedCategory by remember { mutableStateOf(null) } + var newTopicDraft by remember { mutableStateOf("") } + val topics = config.crossPlatform.weiboHotTopics + val categories = com.haorui.app.crossplatform.source.weibo.WeiboHotCategories.ALL + val selectedCategoryIds: Set = config.crossPlatform.weiboHotCategories.toSet() + Section(title = "微博热搜词条勾选", tag = "选什么采什么", + help = "按上方所选类目,把要采集的词条标题加入勾选;勾选持久化,采集执行时按标题在榜单中匹配") { + if (selectedCategoryIds.isEmpty()) { + FieldHelp("请先在上方「微博热搜类目」选择至少一个类型") + } else { + categories.filter { it.id in selectedCategoryIds }.forEach { category -> + val checked = topics[category.id].orEmpty() + // 类目折叠行:点击展开该类目的词条勾选明细 + Row( + modifier = Modifier.fillMaxWidth().padding(vertical = 6.dp) + .clickable { expandedCategory = if (expandedCategory == category.id) null else category.id }, + verticalAlignment = Alignment.CenterVertically, + ) { + Icon(Icons.Filled.ChevronRight, contentDescription = null, + modifier = Modifier.padding(end = 6.dp)) + Text( + text = "${category.tabLabel}(已勾选 ${checked.size} 条)", + style = MaterialTheme.typography.bodyMedium, + fontWeight = if (checked.isNotEmpty()) FontWeight.SemiBold else FontWeight.Normal, + ) + } + if (expandedCategory == category.id) { + checked.forEach { title -> + Row(verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth().padding(start = 20.dp)) { + Text(title, style = MaterialTheme.typography.bodySmall, modifier = Modifier.weight(1f)) + TextButton(onClick = { + val next = checked - title + onConfigChange(config.copy(crossPlatform = config.crossPlatform.copy( + weiboHotTopics = (topics - category.id) + if (next.isEmpty()) emptyMap() + else mapOf(category.id to next), + ))) + }) { Text("移除") } + } + } + Row(verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.fillMaxWidth().padding(start = 20.dp, top = 4.dp)) { + OutlinedTextField( + value = newTopicDraft, + onValueChange = { newTopicDraft = it }, + placeholder = { Text("输入词条标题") }, + singleLine = true, + modifier = Modifier.weight(1f), + ) + Button( + onClick = { + val title = newTopicDraft.trim() + if (title.isNotEmpty()) { + onConfigChange(config.copy(crossPlatform = config.crossPlatform.copy( + weiboHotTopics = topics + mapOf(category.id to (checked + title).distinct()), + ))) + newTopicDraft = "" + } + }, + modifier = Modifier.padding(start = 8.dp), + ) { Text("添加") } + } + } + } + TextButton(onClick = { + // 拉取最新榜单(debug APK;只读拉清单,不采单条)——结果落在服务日志,供核对标题后填入 + selectedCategoryIds.forEach { id -> + val intent = android.content.Intent("com.haorui.app.ACTION_TEST_WEIBO") + .setPackage(context.packageName) + .putExtra("categoryId", id) + .putExtra("collectOnlyList", true) + context.sendBroadcast(intent) + } + }) { Text("拉取所选类目的最新榜单(结果见服务日志)") } + FieldHelp("勾选与持久化为真实配置;自动化按标题匹配新榜单,匹配不到的词条保留待核。") + } + } +} + @OptIn(ExperimentalLayoutApi::class) @Composable fun CrossTasksScreen(tasks: List, loaded: Boolean, loadError: String?, onNavigate: (String) -> Unit) { diff --git a/app/src/test/java/com/haorui/app/crossplatform/CrossPlatformConfigTest.kt b/app/src/test/java/com/haorui/app/crossplatform/CrossPlatformConfigTest.kt index 6eb2d5d..a44e2b4 100644 --- a/app/src/test/java/com/haorui/app/crossplatform/CrossPlatformConfigTest.kt +++ b/app/src/test/java/com/haorui/app/crossplatform/CrossPlatformConfigTest.kt @@ -163,4 +163,36 @@ class CrossPlatformConfigTest { assertEquals(listOf("tech", "hot"), config.weiboHotCategories) assertTrue(CrossPlatformConfig().weiboHotCategories.isEmpty()) } + + @Test fun weiboTopicSelectionSurvivesJsonAndFlatRoundTrip() { + val expected = CrossPlatformConfig( + weiboHotCategories = listOf("hot", "tech"), + weiboHotTopics = mapOf("hot" to listOf("BLG夺冠", "和合共生"), "tech" to listOf("Gemini压谁谁输")), + ) + val fromJson = json.decodeFromString(CrossPlatformConfig.serializer(), + json.encodeToString(CrossPlatformConfig.serializer(), expected)).normalized() + val fromFlat = CrossPlatformConfigCodec.fromFlatMap(CrossPlatformConfigCodec.toFlatMap(expected)) + assertEquals(expected, fromJson) + assertEquals(expected, fromFlat) + } + + @Test fun weiboTopicNormalizationDropsUnknownCategoryKeysAndBlankTitles() { + val config = CrossPlatformConfig( + weiboHotTopics = mapOf( + "hot" to listOf(" 正常词条 ", "", "正常词条", " "), + "未核验类目" to listOf("不该保留"), + ), + ).normalized() + assertEquals(mapOf("hot" to listOf("正常词条")), config.weiboHotTopics) + assertTrue(CrossPlatformConfig().weiboHotTopics.isEmpty()) + } + + @Test fun malformedWeiboTopicJsonFallsBackToEmptyWithoutDisablingModule() { + val config = CrossPlatformConfigCodec.fromFlatMap(mapOf( + "cross_platform_enabled" to "true", + "cross_platform_weibo_hot_topics" to "{不是JSON", + )) + assertTrue(config.enabled) + assertTrue(config.weiboHotTopics.isEmpty()) + } } diff --git a/docs/临时文档.txt b/docs/临时文档.txt index 226b28c..fc28dd3 100644 --- a/docs/临时文档.txt +++ b/docs/临时文档.txt @@ -37,7 +37,7 @@ 1. ~~完成 M0/M1 剩余兼容与异常场景真机验收(粉丝/时间硬阈值、恢复链回归)~~ **已完成(18:26–18:49 真机)**:时间阈值 22 条拒绝、粉丝阈值 6 条拒绝(RETURN_DETAIL 后)、点赞 10 条拒绝;恢复链 wrong_foreground 4 次拉回头条。剩余:不同账号/异常页面验收、恢复链停摆等未诱发项。 2. 清理测试期临时状态:草稿箱遗留测试草稿(`浩瑞草稿回归 e98b05dd`、`ce907ccc` 及旧采集的「俄军…」5 分 26 秒草稿)待用户确认后处置;当日发布计数核对;配置里旧声明"内容为原创"待用户改选。 3. **M2 来源(用户 2026-11-14 晚调整方向)**:知乎已完成(e2e 验收通过,177 项 JVM,能力门控按用户决定暂不放行);**小红书搁置**——用户判定其内容调性不适合头条,e1 取证成果存档备用(发现/热点频道、ID 全混淆、`xhslink.cn/o/` 短链、详情页无可见发布时间、热点推荐“XX万人在看”口径,样本 `.tmp/source-evidence/xhs-e1-*`);**微博提前**(原 M7 提前至 M2):微博热搜 → 头条,已下载微博,取证进行中。 -4. **M2 微博(2026-11-15 e2e 验收通过)**:类目下拉多选(9 类)+持久化、采集机双入口、树/OCR 双源解析、分享链 `weibo.com//<博文ID>` 解析配对全部落地,**203 项 JVM 全过**。**端到端真机验收**:`ACTION_TEST_WEIBO` 全链 `已观察 phase=DONE`——14 条类目清单稳定、时间 OCR、互动数、**分享链接取得并核验**;未知仅剩设计内(排名图片/非数值热度)。37 轮 e2e 迭代修掉 12 个真机缺陷;1 起误关注事故(用户已处置)+1 起误发布事故(前日,用户已处置),教训均已入报告。剩余小尾巴:EXIT 的 backToHotList 从搜索页返回未归位(exited=false,停在可恢复页面,无安全影响)。**下一步:词条清单展示+勾选确认 UI → 采集执行接线 → M4 图文**。M5 视频、M6 强回执、M7 抖音在后。 +4. **M2 微博(2026-11-15 e2e 验收通过)+ M3 勾选链路(已落地)**:类目下拉多选(9 类)+持久化、采集机双入口、树/OCR 双源解析、分享链解析配对,**206 项 JVM 全过**。e2e 真机验收:全链 `已观察 phase=DONE`(14 条清单/时间 OCR/互动数/分享链接取得并核验)。**M3 新落地**:`weiboHotTopics` 配置(类目→标题 Map 持久化)、热点候选页「词条勾选」区块(折叠/添加/移除)、「拉取最新榜单」按钮(广播 collectOnlyList 只读拉清单→服务日志中转)。**剩余:采集执行接线**(勾选词条→collectOne→任务库,替代日志中转的过渡实现)→ M4 图文 → M5 视频 → M6 强回执 → M7 抖音。小尾巴:EXIT 的 backToHotList 从搜索页返回未归位(无安全影响)。 ### 六、关键交付物 / 路径 diff --git a/docs/开发进度.md b/docs/开发进度.md index 3d38d47..20cb3c5 100644 --- a/docs/开发进度.md +++ b/docs/开发进度.md @@ -106,6 +106,7 @@ gradle --offline :app:testDebugUnitTest :app:assembleDebug :app:assembleDebugAnd | 日期 | 事项 | 验证范围 | |---|---|---| +| 2026-11-15 | M3 词条勾选链路落地:`weiboHotTopics` 配置字段(类目→标题 Map,JSON/扁平双通道+归一化清洗)、热点候选页「词条勾选」区块(按类目折叠/添加/移除/持久化)、拉取最新榜单按钮(广播 collectOnlyList 只读拉清单)、沙盒 collectOnlyList 参数 | 206 项 JVM 全过(新增 3 项:JSON/扁平往返、非法键清洗、坏 JSON 回退);APK 构建成功;榜单数据经服务日志中转为过渡实现,自动化直读待采集执行接线 | | 2026-11-15 | **微博 e2e 端到端采集验收通过**(第 37 轮):类目清单 14 条稳定取得 + 单条内容全链(时间 OCR/互动数/**分享链 `weibo.com//<博文ID>` 解析配对**);e2-e4 取证补全(分享面板横滑区/复制链接/搜索框粘贴兜底/双详情布局) | `已观察 phase=DONE`,未知仅剩设计内(排名图片渲染/非数值热度);全程只读无发布;37 轮迭代修掉 12 个真机缺陷;1 起误关注事故(用户已处置),教训入报告 | | 2026-11-15 | 微博分享链取证完成(`weibo.com//<博文ID>` 干净形态)+ 解析器放行;WeiboDevicePort 落地(OCR 双源/搜索框 ACTION_PASTE 兜底/弹窗拒绝项/残留页收拢);ACTION_TEST_WEIBO 沙盒 e2e 17 轮迭代 | 203 项 JVM 全过;e2e 修掉 6 个真机缺陷,卡在发现页→热搜榜导航步(下轮修法已定:bounds 区域校验+精确匹配);全程只读无发布 | | 2026-11-14 | 微博分享面板定位完成(⋯→分享→第3行横滑区「复制链接」,实操 toast「已复制」;「更多」=私信陷阱记录在案) | 复制链接入口与面板全构成已取证;URL 形态读取因工具限流暂停,下轮首项;红线(不点外发/更多/关注屏蔽投诉)已入报告 |