MyListTest.java 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. package space.anyi;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import org.jsoup.nodes.Element;
  4. import org.jsoup.select.Elements;
  5. import org.junit.jupiter.api.Test;
  6. import org.jsoup.Jsoup;
  7. import org.jsoup.nodes.Document;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import space.anyi.cleaner.DetailCleaner;
  11. import space.anyi.client.GzCmcClient;
  12. import space.anyi.config.Config;
  13. import space.anyi.db.Db;
  14. import space.anyi.db.Mybatis;
  15. import space.anyi.dto.ChannelAllContentsResponse;
  16. import space.anyi.dto.NewsItem;
  17. import space.anyi.dto.NewsItemData;
  18. import space.anyi.entity.News;
  19. import space.anyi.mapper.NewsMapper;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.net.URI;
  23. import java.net.http.HttpClient;
  24. import java.net.http.HttpRequest;
  25. import java.net.http.HttpResponse;
  26. import java.nio.charset.StandardCharsets;
  27. import java.nio.file.Files;
  28. import java.nio.file.Path;
  29. import java.time.LocalDateTime;
  30. import java.time.format.DateTimeFormatter;
  31. import java.util.ArrayList;
  32. import java.util.Comparator;
  33. import java.util.HashSet;
  34. import java.util.LinkedHashMap;
  35. import java.util.LinkedHashSet;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.Set;
  39. /**
  40. * 20 条数据的完整流程测试。
  41. *
  42. * <p>覆盖两个阶段的整条链路:列表采集 -> 去重入库 -> 详情抓取 -> 清洗 ->
  43. * 记者/编辑解析 -> 更新,用于快速验证全流程与可重复运行。</p>
  44. */
  45. public class MyListTest {
  46. private final static Logger log = LoggerFactory.getLogger(MyListTest.class);
  47. /**
  48. * 执行 20 条的完整采集流程。
  49. *
  50. * @throws Exception 采集过程中未预期的异常
  51. */
  52. @Test
  53. public void fullCollectionFlow() throws Exception {
  54. Config config = new Config();
  55. Mybatis mybatis = new Mybatis(Db.init(config));
  56. GzCmcClient client = new GzCmcClient(config);
  57. NewsMapper mapper = mybatis.getMapper(NewsMapper.class);
  58. try {
  59. // 阶段一: 列表采集 20 条并去重入库
  60. ChannelAllContentsResponse resp = client.search(config.keywords().get(0), config.channelIds().get(0), 1, 20);
  61. List<News> newsList = new ArrayList<>();
  62. for (NewsItem item : resp.getList()) {
  63. NewsItemData data = item.getData();
  64. if (data == null || data.getId() == null) {
  65. continue;
  66. }
  67. News news = new News();
  68. news.setId(data.getId());
  69. news.setTitle(DetailCleaner.titleClean(data.getTitle()));
  70. news.setUrl(data.getUrl());
  71. news.setPublishTime(LocalDateTime.parse(data.getPublishTime(), Config.TIME_FORMATTER));
  72. news.setChannelId(data.getChannelId());
  73. news.setChannelName(data.getChannelName());
  74. news.setEditor(data.getUserName());
  75. newsList.add(news);
  76. }
  77. int inserted = 0;
  78. for (News news : newsList) {
  79. if (mapper.selectById(news.getId()) == null) {
  80. mapper.insert(news);
  81. inserted++;
  82. }
  83. }
  84. log.info("[20条流程] 列表采集={} 新增入库={}", newsList.size(), inserted);
  85. // 阶段二: 逐条抓详情 -> 清洗 -> 解析 -> 更新
  86. int ok = 0;
  87. for (News news : newsList) {
  88. try {
  89. String html = client.fetchDetail(news.getUrl());
  90. String content = DetailCleaner.clean(html);
  91. Document doc = Jsoup.parse(content);
  92. DetailCleaner.PersonInfo person = DetailCleaner.parsePersons(DetailCleaner.articleContentText(doc));
  93. News update = new News();
  94. update.setId(news.getId());
  95. update.setContent(content);
  96. update.setEditor(person.editor());
  97. update.setTextReporters(person.textReporters().isEmpty() ? null : String.join(",", person.textReporters()));
  98. update.setImageReporters(person.imageReporters().isEmpty() ? null : String.join(",", person.imageReporters()));
  99. update.setCorrespondents(person.correspondents().isEmpty() ? null : String.join(",", person.correspondents()));
  100. update.setContentFetchedAt(LocalDateTime.now());
  101. mapper.updateById(update);
  102. ok++;
  103. log.info("[20条流程] {} 编辑={} 文字记者={} 图片记者={} 通讯员={}",
  104. news.getId(), person.editor(), person.textReporters(), person.imageReporters(), person.correspondents());
  105. } catch (Exception e) {
  106. log.error("[20条流程] 失败 id={} url={} err={}", news.getId(), news.getUrl(), e.getMessage());
  107. }
  108. Thread.sleep(config.sleepMs());
  109. }
  110. log.info("[20条流程] 完成: 成功={} 失败={}", ok, newsList.size() - ok);
  111. } finally {
  112. mybatis.close();
  113. }
  114. }
  115. @Test
  116. public void cleanHtmlTest() {
  117. String html = """
  118. <!DOCTYPE html>
  119. <html lang="en" style="font-size: 18px;">
  120. <head>
  121. <meta charset="UTF-8">
  122. <link rel="icon" href="https://oss.gz-cmc.com/default-image/400x400.png" />
  123. <title>畅通版权交易渠道,漫博会推动国产IP出海与海外IP引进落地</title>
  124. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1">
  125. <meta name="keywords" content="">
  126. <meta name="author" content="莫斯其格">
  127. <meta name="description" content="">
  128. <meta property="og:type" content="article">
  129. <meta property="og:title" content="畅通版权交易渠道,漫博会推动国产IP出海与海外IP引进落地">
  130. <meta property="og:description" content="畅通版权交易渠道,漫博会推动国产IP出海与海外IP引进落地">
  131. <meta property="og:url" content>
  132. <meta property="og:image" itemprop="image" content="https://oss.gz-cmc.com/default-image/default-share-image.jpg">
  133. <meta property="article:author" content="莫斯其格">
  134. <meta property="article:published_time" content="2026-07-24">
  135. <style name="launch-app-style">
  136. .download-link {
  137. text-decoration: none;
  138. color: #333 !important;
  139. }
  140. .guide-image {
  141. width: 100vw;
  142. }
  143. </style>
  144. <style name="launch-swiper-style">
  145. .swiper-news-box {
  146. display: flex;
  147. align-items: center;
  148. justify-content: space-between;
  149. padding: 10px;
  150. width: 100vw;
  151. box-sizing: border-box;
  152. height: 62px;
  153. }
  154. .swiper-news-image {
  155. width: 64px;
  156. height: 36px;
  157. object-fit: cover;
  158. border-radius: 5px;
  159. flex-shrink: 0;
  160. }
  161. .swiper-news-title {
  162. font-size: 14px;
  163. text-align: justify;
  164. margin-left: 10px;
  165. line-height: 1.5;
  166. word-break: break-all;
  167. overflow: hidden;
  168. display: -webkit-box;
  169. -webkit-line-clamp: 2;
  170. -webkit-box-orient: vertical;
  171. flex-grow: 1;
  172. }
  173. .swiper-news-button {
  174. width: 65px;
  175. height: 24px;
  176. background: linear-gradient(90deg, #ef4243 0%, #fa7419 100%);
  177. border-radius: 12px;
  178. color: #ffffff;
  179. font-size: 14px;
  180. margin-left: 10px;
  181. flex-shrink: 0;
  182. display: flex;
  183. align-items: center;
  184. justify-content: center;
  185. }
  186. </style>
  187. <style name="like-unlike-style">
  188. .comment-unlike-icon,
  189. .comment-like-icon {
  190. width: 18px;
  191. height: 18px;
  192. cursor: pointer;
  193. }
  194. .sub-comment-unlike-icon,
  195. .sub-comment-like-icon {
  196. width: 16px;
  197. height: 16px;
  198. cursor: pointer;
  199. }
  200. </style>
  201. <style name="comment-reply-style">
  202. .comment-reply {
  203. height: 30px;
  204. border-radius: 15px;
  205. background-color: #e5e5e5;
  206. display: flex;
  207. align-items: center;
  208. width: 100px;
  209. justify-content: center;
  210. font-size: 14px;
  211. padding-left: 5px;
  212. }
  213. </style>
  214. <style name="comment-more-style">
  215. .more-comment {
  216. height: 44px;
  217. background: rgba(248, 110, 29, 0.1);
  218. border-radius: 22px;
  219. border: 1px solid #f04640;
  220. width: 250px;
  221. margin: 15px auto 0 auto;
  222. display: flex;
  223. align-items: center;
  224. justify-content: center;
  225. color: #e60213;
  226. cursor: pointer;
  227. }
  228. </style>
  229. <style name="article-launch-app-style">
  230. .text-launch-app {
  231. width: 100%;
  232. height: 40px;
  233. text-align: center;
  234. background: linear-gradient(90deg, #EF4243 0%, #FA7419 100%);
  235. border-radius: 20px;
  236. font-size: 14px;
  237. color: #fff;
  238. display: flex;
  239. align-items: center;
  240. justify-content: center;
  241. }
  242. .text-huacheng-icon {
  243. width: 20px;
  244. height: 20px;
  245. margin-right: 5px;
  246. }
  247. </style>
  248. <style name="article-operate-style">
  249. .article-operate-list {
  250. display: flex;
  251. align-items: center;
  252. justify-content: space-between;
  253. }
  254. .article-operate-input {
  255. height: 28px;
  256. background: #F5F5F5;
  257. border-radius: 14px;
  258. width: 50%;
  259. padding: 0 15px;
  260. font-size: 12px;
  261. color: #999;
  262. line-height: 28px;
  263. }
  264. .article-operate-comment,
  265. .article-operate-collect,
  266. .article-operate-like,
  267. .article-operate-share {
  268. width: 22px; \s
  269. height: 22px;
  270. }
  271. </style>
  272. <link href="https://oss.gz-cmc.com/news-static/huacheng/2026-06-03/css/86-6b9ab229.css" rel="stylesheet">
  273. <link href="https://oss.gz-cmc.com/news-static/huacheng/2026-06-03/css/textDetail-43f87c2a.css" rel="stylesheet">
  274. </head>
  275. <body>
  276. <div class="container">
  277. <div class="header-guide-box" id="header-guide-box">
  278. <div class="launch-app-btn" name="launch-app-btn" data-style="launch-app-style">
  279. <a name="download-link" class="download-link">
  280. <img name="guide-image" alt="" class="guide-image"></a>
  281. </div>
  282. <img class="close-guide"
  283. name="close-guide"
  284. src="https://oss.gz-cmc.com/news-static/huacheng/2024-03-25/images/guide-close-cc491637.png"
  285. alt="关闭引导">
  286. </div>
  287. <div class="article-title" id="article-title">畅通版权交易渠道,漫博会推动国产IP出海与海外IP引进落地</div>
  288. <div class="not-exist-media-leader">
  289. <div class="article-source-time">
  290. <div class="article-time">2026-07-24 15:43:26</div>
  291. <div class="article-source">广州日报新花城</div>
  292. </div>
  293. </div>
  294. <div class="article-video-fixed">
  295. <img src="https://oss.gz-cmc.com/news-static/huacheng/2025-06-10/images/close-3e4f193c.png"
  296. class="article-video-fixed-close" alt="">
  297. <video id="sticky-player" class="video-js vjs-16-9" webkit-playsinline="true" playsinline="true"
  298. x5-video-player-type="h5" controls="false">
  299. </video>
  300. </div>
  301. <div class="article-content" id="article-content">
  302. <p style="text-align: justify;">7月24日,记者从第十六届中国国际影视动漫版权保护和贸易博览会新闻发布会上获悉,第十六届中国国际动漫博览会将于2026年8月6日至10日在东莞“中国潮玩之都·漫博中心”举行。据悉,本届展会现场设立版权服务工作站,提供版权登记咨询、快速登记、维权指引等一站式服务,为参展企业原创IP、新品设计提供即时版权保护。同时建立展会版权快速维权机制,严厉打击侵权盗版行为,大力营造尊重原创、保护版权的良好展会氛围。</p><p style="text-align: justify;"><span class="insert-img-container" style="display:inline-block;line-height: 1.3em; "><img style="vertical-align: bottom;" src="https://oss.gz-cmc.com/pgcr/root/huacheng/upload/news/image/2026/07/24/640aebfbf44e4649954551fbd309dd04.jpg?x-oss-process=style/content" class="uedito-cusimg" title="微信图片_20260724110823_7734_327" alt="微信图片_20260724110823_7734_327"/><br/></span></p><p style="text-align: justify;">本届展会畅通版权交易渠道,激活版权市场价值。展会进一步打造专业化版权交易对接平台,设置IP产业融合馆,配套举办AI漫剧产业对接及出海合作活动、IP品牌授权精准对接交流活动、文博IP跨界合作发布会、非遗潮玩创新合作发布会等产业活动,推动版权方与制造企业、品牌方、渠道商精准对接,促进版权成果转化落地。展会还将联动中阿合作中心、西班牙商会等数十家海外商会、协会、外事机构,组织跨境采购团,搭建中外版权合作桥梁,推动国产IP出海与海外IP引进落地,构建双向循环的版权贸易格局。</p><p style="text-align: justify;">展会将创新版权金融服务,赋能企业健康发展。展会现场设置版权金融咨询专区,为中小微动漫潮玩企业提供融资对接服务,破解企业发展难点。同时开展版权金融政策宣讲,普及质押融资流程等知识,引导企业用好版权金融工具,将版权资产转化为发展动能。</p><p style="text-align: justify;">文/广州日报新花城记者:莫斯其格 实习生:谭斯文</p><p style="text-align: justify;">图/广州日报新花城记者:杨泽彬</p><p style="text-align: justify;">广州日报新花城编辑:吴嘉丽</p>
  303. </div>
  304. <div class="read-like-number hidden" id="read-like-number">
  305. <div class="read-number">浏览量:<span id="read-number-text"></span></div>
  306. <div class="like-number">点赞量:<span id="like-number-text"></span></div>
  307. </div>
  308. <div class="article-belong-topics"></div>
  309. <div class="article-copyright">
  310. <div class="article-copyright-line"></div>
  311. <div>@新花城 版权所有 转载需经授权</div>
  312. <div class="article-copyright-line"></div>
  313. </div>
  314. <div class="media-list swiper" id="media-list">
  315. <div class="swiper-wrapper">
  316. <a href="https://huacheng.gz-cmc.com/html/mediaDetail.html?siteId=5e88c884e2ed4e7a9a8d5225c299f707&mediaId=3dc715eda4514b24b3031931713c3f18" data-id="3dc715eda4514b24b3031931713c3f18" class="media-box-link swiper-slide">
  317. <div class="media-box">
  318. <img
  319. src="https://oss.gz-cmc.com/default-image/default-avatar.png"
  320. data-src=""
  321. alt="" onerror="this.src='https://oss.gz-cmc.com/default-image/default-avatar.png'" class="media-avatar lazyload">
  322. <div class="media-name">吴嘉丽</div>
  323. <div class="media-more">更多文章</div>
  324. </div>
  325. </a>
  326. <a href="https://huacheng.gz-cmc.com/html/mediaDetail.html?siteId=5e88c884e2ed4e7a9a8d5225c299f707&mediaId=d37d7de896e84e2598b55396aa6143a5" data-id="d37d7de896e84e2598b55396aa6143a5" class="media-box-link swiper-slide">
  327. <div class="media-box">
  328. <img
  329. src="https://oss.gz-cmc.com/default-image/default-avatar.png"
  330. data-src=""
  331. alt="" onerror="this.src='https://oss.gz-cmc.com/default-image/default-avatar.png'" class="media-avatar lazyload">
  332. <div class="media-name">杨泽彬</div>
  333. <div class="media-more">更多文章</div>
  334. </div>
  335. </a>
  336. <a href="https://huacheng.gz-cmc.com/html/mediaDetail.html?siteId=5e88c884e2ed4e7a9a8d5225c299f707&mediaId=ea916e08c213441093623cb4bbd79a99" data-id="ea916e08c213441093623cb4bbd79a99" class="media-box-link swiper-slide">
  337. <div class="media-box">
  338. <img
  339. src="https://oss.gz-cmc.com/default-image/default-avatar.png"
  340. data-src="https://ugcoss.gz-cmc.com/userr/root/huacheng/user/mp/media/icon/2023/11/02/22d2c55b55264b658a65e4d4cb70c20c.jpg"
  341. alt="" onerror="this.src='https://oss.gz-cmc.com/default-image/default-avatar.png'" class="media-avatar lazyload">
  342. <div class="media-name">莫斯其格</div>
  343. <div class="media-more">更多文章</div>
  344. </div>
  345. </a>
  346. </div>
  347. </div>
  348. <div class="article-launch-app-box" id="article-launch-app-box">
  349. <div class="launch-app-btn" name="launch-app-btn" data-style="article-launch-app-style">
  350. <a name="download-link" class="download-link">
  351. <div class="text-launch-app">
  352. <img class="text-huacheng-icon"
  353. src="https://oss.gz-cmc.com/news-static/huacheng/2024-03-25/images/text-huacheng-icon-2f415b25.png"
  354. alt="打开app">打开广州日报新花城,享受流畅体验
  355. </div>
  356. </a>
  357. </div>
  358. </div>
  359. <div class="article-comment" id="article-comment">
  360. <div class="article-empty-line"></div>
  361. <div class="related-title">热门评论</div>
  362. <div id="comment-list" class="comment-list">
  363. <img src="https://oss.gz-cmc.com/news-static/huacheng/2024-03-25/images/comment-empty-5a1fe672.png"
  364. class="comment-empty-img" alt="">
  365. </div>
  366. <div class="more-comment-box" id="more-comment-box">
  367. <div class="launch-app-btn" name="launch-app-btn" data-style="comment-more-style">
  368. <a class="download-link" name="download-link">
  369. <div class="more-comment">查看更多评论</div>
  370. </a>
  371. </div>
  372. </div>
  373. </div>
  374. <div class="related-content" id="related-content">
  375. <div class="article-empty-line"></div>
  376. <div class="related-title">相关推荐</div>
  377. <div id="content-list" class="content-list"></div>
  378. </div>
  379. <div class="related-channel" id="related-channel">
  380. <div class="article-empty-line"></div>
  381. <div class="related-title">相关频道推荐</div>
  382. <div id="channel-list" class="channel-list"></div>
  383. </div>
  384. <div class="article-operate" id="article-operate">
  385. <div class="launch-app-btn" name="launch-app-btn" data-style="article-operate-style">
  386. <a class="download-link" name="download-link">
  387. <div class="article-operate-list">
  388. <div class="article-operate-input">说点什么</div>
  389. <img src="https://oss.gz-cmc.com/news-static/huacheng/2024-03-25/images/text-comment-e8e04213.png"
  390. alt="评论" class="article-operate-comment">
  391. <img src="https://oss.gz-cmc.com/news-static/huacheng/2024-03-25/images/text-collect-b1054ae5.png"
  392. alt="收藏" class="article-operate-collect">
  393. <img src="https://oss.gz-cmc.com/news-static/huacheng/2024-03-25/images/text-like-67f9fd7d.png"
  394. alt="点赞" class="article-operate-like">
  395. <img src="https://oss.gz-cmc.com/news-static/huacheng/2024-03-25/images/text-share-9200469e.png"
  396. alt="分享" class="article-operate-share">
  397. </div>
  398. </a>
  399. </div>
  400. </div>
  401. </div>
  402. <div class="hidden-box" id="hidden-box">
  403. <div hidden name="mainDomain">https://www.gz-cmc.com</div>
  404. <div hidden name="subDomain">https://huacheng.gz-cmc.com</div>
  405. <div hidden name="newsId">70290505ac3f439cbe7a96d971aae131</div>
  406. <div hidden name="channelId">903d342af9af43a59cf7cd9d5342be0b</div>
  407. <div hidden name="siteId">5e88c884e2ed4e7a9a8d5225c299f707</div>
  408. <div hidden name="shareTitle">畅通版权交易渠道,漫博会推动国产IP出海与海外IP引进落地</div>
  409. <div hidden name="shareDesc">分享来自广州日报新花城客户端,请点击打开更多精彩...</div>
  410. <div hidden name="shareImgUrl">https://oss.gz-cmc.com/default-image/default-share-image.jpg</div>
  411. <div hidden name="staticType">1</div>
  412. <div hidden name="contentType">1</div>
  413. <div hidden name="mListpattern">1</div>
  414. </div>
  415. <script defer src="https://oss.gz-cmc.com/news-static/huacheng/2026-06-03/js/681-ddb2f1b4.js"></script>
  416. <script defer src="https://oss.gz-cmc.com/news-static/huacheng/2026-06-03/js/86-f0059e14.js"></script>
  417. <script defer src="https://oss.gz-cmc.com/news-static/huacheng/2026-06-03/js/textDetail-e2c6dbe5.js"></script>
  418. <script defer src="https://oss.gz-cmc.com/news-static/huacheng/config/config.js"></script>
  419. </body>
  420. </html>
  421. """;
  422. Document doc = Jsoup.parse(html);
  423. html = """
  424. <!DOCTYPE html>
  425. <html lang="en" style="font-size: 18px;">
  426. <head></head>
  427. <body>
  428. </body>
  429. </html>
  430. """;
  431. Document result = Jsoup.parse(html);
  432. Elements style = doc.select("link[rel='stylesheet']");
  433. Element head = result.head();
  434. head.insertChildren(0, style);
  435. Element container = doc.select("div.container").first();
  436. Elements keep = container.children().select("div.article-title, div.not-exist-media-leader, div.article-content");
  437. keep.select("video").remove();
  438. result.body().appendChildren(keep);
  439. log.info("{}", result.html());
  440. }
  441. @Test
  442. public void html2pdfTest() throws Exception {
  443. String html = """
  444. <!doctype html>
  445. <html lang="en" style="font-size: 18px;">
  446. <head>
  447. <link href="https://oss.gz-cmc.com/news-static/huacheng/2026-06-03/css/86-6b9ab229.css" rel="stylesheet">
  448. <link href="https://oss.gz-cmc.com/news-static/huacheng/2026-06-03/css/textDetail-43f87c2a.css" rel="stylesheet">
  449. </head>
  450. <body>
  451. <div class="article-title" id="article-title">
  452. 畅通版权交易渠道,漫博会推动国产IP出海与海外IP引进落地
  453. </div>
  454. <div class="not-exist-media-leader">
  455. <div class="article-source-time">
  456. <div class="article-time">
  457. 2026-07-24 15:43:26
  458. </div>
  459. <div class="article-source">
  460. 广州日报新花城
  461. </div>
  462. </div>
  463. </div>
  464. <div class="article-content" id="article-content">
  465. <p style="text-align: justify;">7月24日,记者从第十六届中国国际影视动漫版权保护和贸易博览会新闻发布会上获悉,第十六届中国国际动漫博览会将于2026年8月6日至10日在东莞“中国潮玩之都·漫博中心”举行。据悉,本届展会现场设立版权服务工作站,提供版权登记咨询、快速登记、维权指引等一站式服务,为参展企业原创IP、新品设计提供即时版权保护。同时建立展会版权快速维权机制,严厉打击侵权盗版行为,大力营造尊重原创、保护版权的良好展会氛围。</p>
  466. <p style="text-align: justify;"><span class="insert-img-container" style="display:inline-block;line-height: 1.3em; "><img style="vertical-align: bottom;" src="https://oss.gz-cmc.com/pgcr/root/huacheng/upload/news/image/2026/07/24/640aebfbf44e4649954551fbd309dd04.jpg?x-oss-process=style/content" class="uedito-cusimg" title="微信图片_20260724110823_7734_327" alt="微信图片_20260724110823_7734_327"><br></span></p>
  467. <p style="text-align: justify;">本届展会畅通版权交易渠道,激活版权市场价值。展会进一步打造专业化版权交易对接平台,设置IP产业融合馆,配套举办AI漫剧产业对接及出海合作活动、IP品牌授权精准对接交流活动、文博IP跨界合作发布会、非遗潮玩创新合作发布会等产业活动,推动版权方与制造企业、品牌方、渠道商精准对接,促进版权成果转化落地。展会还将联动中阿合作中心、西班牙商会等数十家海外商会、协会、外事机构,组织跨境采购团,搭建中外版权合作桥梁,推动国产IP出海与海外IP引进落地,构建双向循环的版权贸易格局。</p>
  468. <p style="text-align: justify;">展会将创新版权金融服务,赋能企业健康发展。展会现场设置版权金融咨询专区,为中小微动漫潮玩企业提供融资对接服务,破解企业发展难点。同时开展版权金融政策宣讲,普及质押融资流程等知识,引导企业用好版权金融工具,将版权资产转化为发展动能。</p>
  469. <p style="text-align: justify;">文/广州日报新花城记者:莫斯其格 实习生:谭斯文</p>
  470. <p style="text-align: justify;">图/广州日报新花城记者:杨泽彬</p>
  471. <p style="text-align: justify;">广州日报新花城编辑:吴嘉丽</p>
  472. </div>
  473. </body>
  474. </html>
  475. """;
  476. File outDir = new File("output");
  477. Files.createDirectories(outDir.toPath());
  478. File pdf = new File(outDir, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".pdf");
  479. DetailCleaner.html2pdf(html, pdf.toPath());
  480. log.info("[html2pdf] 生成成功: {}", pdf.getAbsolutePath());
  481. }
  482. @Test
  483. public void html2plaintextTest(){
  484. String html = """
  485. <!doctype html>
  486. <html lang="en" style="font-size: 18px;">
  487. <head>
  488. <link href="https://oss.gz-cmc.com/news-static/huacheng/2026-06-03/css/86-6b9ab229.css" rel="stylesheet">
  489. <link href="https://oss.gz-cmc.com/news-static/huacheng/2026-06-03/css/textDetail-43f87c2a.css" rel="stylesheet">
  490. </head>
  491. <body>
  492. <div class="article-title" id="article-title">
  493. 畅通版权交易渠道,漫博会推动国产IP出海与海外IP引进落地
  494. </div>
  495. <div class="not-exist-media-leader">
  496. <div class="article-source-time">
  497. <div class="article-time">
  498. 2026-07-24 15:43:26
  499. </div>
  500. <div class="article-source">
  501. 广州日报新花城
  502. </div>
  503. </div>
  504. </div>
  505. <div class="article-content" id="article-content">
  506. <p style="text-align: justify;">7月24日,记者从第十六届中国国际影视动漫版权保护和贸易博览会新闻发布会上获悉,第十六届中国国际动漫博览会将于2026年8月6日至10日在东莞“中国潮玩之都·漫博中心”举行。据悉,本届展会现场设立版权服务工作站,提供版权登记咨询、快速登记、维权指引等一站式服务,为参展企业原创IP、新品设计提供即时版权保护。同时建立展会版权快速维权机制,严厉打击侵权盗版行为,大力营造尊重原创、保护版权的良好展会氛围。</p>
  507. <p style="text-align: justify;"><span class="insert-img-container" style="display:inline-block;line-height: 1.3em; "><img style="vertical-align: bottom;" src="https://oss.gz-cmc.com/pgcr/root/huacheng/upload/news/image/2026/07/24/640aebfbf44e4649954551fbd309dd04.jpg?x-oss-process=style/content" class="uedito-cusimg" title="微信图片_20260724110823_7734_327" alt="微信图片_20260724110823_7734_327"><br></span></p>
  508. <p style="text-align: justify;">本届展会畅通版权交易渠道,激活版权市场价值。展会进一步打造专业化版权交易对接平台,设置IP产业融合馆,配套举办AI漫剧产业对接及出海合作活动、IP品牌授权精准对接交流活动、文博IP跨界合作发布会、非遗潮玩创新合作发布会等产业活动,推动版权方与制造企业、品牌方、渠道商精准对接,促进版权成果转化落地。展会还将联动中阿合作中心、西班牙商会等数十家海外商会、协会、外事机构,组织跨境采购团,搭建中外版权合作桥梁,推动国产IP出海与海外IP引进落地,构建双向循环的版权贸易格局。</p>
  509. <p style="text-align: justify;">展会将创新版权金融服务,赋能企业健康发展。展会现场设置版权金融咨询专区,为中小微动漫潮玩企业提供融资对接服务,破解企业发展难点。同时开展版权金融政策宣讲,普及质押融资流程等知识,引导企业用好版权金融工具,将版权资产转化为发展动能。</p>
  510. <p style="text-align: justify;">文/广州日报新花城记者:莫斯其格 实习生:谭斯文</p>
  511. <p style="text-align: justify;">图/广州日报新花城记者:杨泽彬</p>
  512. <p style="text-align: justify;">广州日报新花城编辑:吴嘉丽</p>
  513. </div>
  514. </body>
  515. </html>
  516. """;
  517. String text = Jsoup.parse(html).text();
  518. log.info("{}", text);
  519. }
  520. /**
  521. * 为库中已采集详情(content 非空)的新闻批量生成 PDF。
  522. *
  523. * <p>取发布时间最新的 100 条,逐条渲染到 output/ 目录,单条失败不中断整体。</p>
  524. *
  525. * @throws Exception 数据库初始化等未预期的异常
  526. */
  527. @Test
  528. public void test() throws Exception {
  529. Config config = new Config();
  530. Mybatis mybatis = new Mybatis(Db.init(config));
  531. try {
  532. NewsMapper mapper = mybatis.getMapper(NewsMapper.class);
  533. //获取一百条已采集详情的记录
  534. QueryWrapper<News> wrapper = new QueryWrapper<News>()
  535. .isNotNull("content")
  536. .eq("channel_id","903d342af9af43a59cf7cd9d5342be0b")
  537. .orderByDesc("publish_time")
  538. .last("LIMIT 100");
  539. List<News> pending = mapper.selectList(wrapper);
  540. log.info("获取数据成功:{}条", pending.size());
  541. File outDir = new File("output");
  542. Files.createDirectories(outDir.toPath());
  543. int ok = 0;
  544. for (News news : pending) {
  545. try {
  546. String publicTime = news.getPublishTime().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  547. String editor = news.getEditor() == null ? "" : news.getEditor();
  548. String title = news.getTitle() == null ? "" : news.getTitle();
  549. String fileName = sanitizeFileName(String.format("%s-%s-%s-%s.pdf", publicTime, title, editor, news.getId()));
  550. File file = new File(outDir, fileName);
  551. DetailCleaner.html2pdf(news.getContent(), file.toPath());
  552. ok++;
  553. } catch (Exception e) {
  554. log.error("[生成PDF] 失败 id={} err={}", news.getId(), e.getMessage());
  555. }
  556. }
  557. log.info("[生成PDF] 完成: 成功={} 失败={}", ok, pending.size() - ok);
  558. } finally {
  559. mybatis.close();
  560. }
  561. }
  562. /**
  563. * 清理文件名中的非法字符并截断超长标题。
  564. *
  565. * <p>截断按 UTF-8 字节数计算(文件名上限 255 字节,中文每字占 3 字节),
  566. * 并在字符边界处切断,避免截出乱码。</p>
  567. *
  568. * @param name 原始文件名
  569. * @return 可安全落盘的文件名(最长 200 字节)
  570. */
  571. private static String sanitizeFileName(String name) {
  572. String s = name.replaceAll("[\\\\/:*?\"<>|\\s]", "_");
  573. byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
  574. if (bytes.length <= 200) {
  575. return s;
  576. }
  577. int cut = 200;
  578. while (cut > 0 && (bytes[cut - 1] & 0xC0) == 0x80) {
  579. cut--;
  580. }
  581. return new String(bytes, 0, cut, StandardCharsets.UTF_8);
  582. }
  583. @Test
  584. public void titleCleanTest() throws Exception {
  585. Config config = new Config();
  586. Mybatis mybatis = new Mybatis(Db.init(config));
  587. try {
  588. NewsMapper mapper = mybatis.getMapper(NewsMapper.class);
  589. List<News> all = mapper.selectList(new QueryWrapper<News>().like("title", "<"));
  590. int updated = 0;
  591. for (News news : all) {
  592. String cleaned = DetailCleaner.titleClean(news.getTitle());
  593. if (cleaned != null && !cleaned.equals(news.getTitle())) {
  594. News update = new News();
  595. update.setId(news.getId());
  596. update.setTitle(cleaned);
  597. mapper.updateById(update);
  598. updated++;
  599. log.info("[标题清洗] {} 原:{} 新:{}", news.getId(), news.getTitle(), cleaned);
  600. }
  601. }
  602. log.info("[标题清洗] 完成: 总数={} 更新={}", all.size(), updated);
  603. } finally {
  604. mybatis.close();
  605. }
  606. }
  607. /**
  608. * 统计日期区间中新闻数量和去除包含视频新闻后的数量
  609. * 20260913
  610. */
  611. @Test
  612. public void videoCountTest() throws Exception {
  613. //1.从数据库获取符合日期要求的新闻
  614. //select * from news where (publish_time >= '2024-01-01' and publish_time < '2024-03-01') or (publish_time >= '2024-09-01' and publish_time < '2024-10-01') and content is not null;
  615. Config config = new Config();
  616. Mybatis mybatis = new Mybatis(Db.init(config));
  617. try {
  618. NewsMapper mapper = mybatis.getMapper(NewsMapper.class);
  619. List<News> newsList = mapper.selectList(new QueryWrapper<News>()
  620. .and(w -> w.ge("publish_time", LocalDateTime.of(2024, 1, 1, 0, 0))
  621. .lt("publish_time", LocalDateTime.of(2024, 3, 1, 0, 0)))
  622. .or(w -> w.ge("publish_time", LocalDateTime.of(2024, 9, 1, 0, 0))
  623. .lt("publish_time", LocalDateTime.of(2024, 10, 1, 0, 0))
  624. .isNotNull("content")));
  625. log.info("[视频统计] 获取新闻={}条", newsList.size());
  626. //2.统计包含视频的新闻(通过URL获取新闻的html,包含 video 标签即为包含视频,使用Jsoup解析html进行判断)
  627. GzCmcClient client = new GzCmcClient(config);
  628. Map<String, Boolean> videoMap = new LinkedHashMap<>();
  629. int videoCount = 0;
  630. for (News news : newsList) {
  631. boolean hasVideo = false;
  632. try {
  633. String html = client.fetchDetail(news.getUrl());
  634. hasVideo = !Jsoup.parse(html).select("video").isEmpty();
  635. if (hasVideo) {
  636. videoCount++;
  637. }
  638. log.info("[视频统计] {} 视频={}", news.getId(), hasVideo);
  639. } catch (Exception e) {
  640. log.error("[视频统计] 失败 id={} url={} err={}", news.getId(), news.getUrl(), e.getMessage());
  641. }
  642. videoMap.put(news.getId(), hasVideo);
  643. Thread.sleep(config.sleepMs());
  644. }
  645. //3.输出结果(新闻总数,去掉包含视频新闻的数量)
  646. log.info("[视频统计] 完成: 总数={} 包含视频={} 去除视频后={}",
  647. newsList.size(), videoCount, newsList.size() - videoCount);
  648. //4.统计结果存储到CSV(仅明细: id,是否包含视频)
  649. List<String> lines = new ArrayList<>();
  650. lines.add("id,video");
  651. for (Map.Entry<String, Boolean> e : videoMap.entrySet()) {
  652. lines.add(e.getKey() + "," + e.getValue());
  653. }
  654. File csv = new File("videoCountResult.csv");
  655. Files.write(csv.toPath(), lines, StandardCharsets.UTF_8);
  656. log.info("[视频统计] 已写入: {} (共{}行)", csv.getAbsolutePath(), lines.size());
  657. } finally {
  658. mybatis.close();
  659. }
  660. }
  661. /**
  662. * 二次筛选
  663. * - 标题出现一次关键字或正文出现一个关键词两次
  664. * - 正文中包含视频
  665. * 记录符合要求的id和命中原因(使用Map)
  666. * 输出二次筛选符合的数量
  667. * 20260913
  668. */
  669. @Test
  670. public void secondSelectTest() throws Exception {
  671. /**
  672. * 1.从文件 videoCountResult.csv 中选出包含视频的新闻id(仅video=true的记录)
  673. * 2.从数据库中获取新闻
  674. * 3.进行筛选,正文需要使用Jsoup进行存文本解析,数据库存储的是HTML
  675. * 4.输出符合的记录和数量
  676. * 5.筛选结果存储到result.csv文件中
  677. */
  678. Set<String> titleKeys = Set.of("非遗","非物质文化遗产","非遗保护","遗产保护","传承","保护项目","文化政策","文化多样性","非遗创新","文化传统","非遗价值","非遗传承人","非遗活动");
  679. Set<String> contentKeys = Set.of("非物质文化遗产","非遗保护","遗产保护","传承","保护项目","文化政策","文化多样性","非遗创新","文化传统","非遗价值","非遗传承人","非遗活动");
  680. //1.从CSV中选出包含视频的新闻id
  681. Set<String> videoIds = new HashSet<>();
  682. for (String line : Files.readAllLines(Path.of("videoCountResult.csv"), StandardCharsets.UTF_8)) {
  683. if (line.isBlank() || line.startsWith("id,")) {
  684. continue;
  685. }
  686. String[] cols = line.split(",", -1);
  687. if (cols.length >= 2 && "true".equals(cols[1].trim())) {
  688. videoIds.add(cols[0].trim());
  689. }
  690. }
  691. log.info("[二次筛选] CSV视频新闻={}条", videoIds.size());
  692. //2.从数据库中获取新闻
  693. Config config = new Config();
  694. Mybatis mybatis = new Mybatis(Db.init(config));
  695. try {
  696. NewsMapper mapper = mybatis.getMapper(NewsMapper.class);
  697. List<News> newsList = videoIds.isEmpty() ? List.of() : mapper.selectBatchIds(videoIds);
  698. log.info("[二次筛选] 数据库命中={}条", newsList.size());
  699. //3.进行标题判断和正文判断(正文存储为HTML,需先用Jsoup解析为纯文本)
  700. Map<String, String> matched = new LinkedHashMap<>();
  701. for (News news : newsList) {
  702. String reason = matchReason(news, titleKeys, contentKeys);
  703. if (reason != null) {
  704. matched.put(news.getId(), reason);
  705. }
  706. }
  707. //4.输出符合的记录和数量
  708. matched.forEach((id, reason) -> log.info("[二次筛选] {} 命中={}", id, reason));
  709. log.info("[二次筛选] 符合: {}条", matched.size());
  710. //5.筛选结果存储到result.csv文件中
  711. List<String> lines = new ArrayList<>();
  712. lines.add("id,title,reason");
  713. for (News news : newsList) {
  714. String reason = matched.get(news.getId());
  715. if (reason != null) {
  716. lines.add(csvEscape(news.getId()) + "," + csvEscape(news.getTitle()) + "," + csvEscape(reason));
  717. }
  718. }
  719. File csv = new File("result1.csv");
  720. Files.write(csv.toPath(), lines, StandardCharsets.UTF_8);
  721. log.info("[二次筛选] 已写入: {}", csv.getAbsolutePath());
  722. } finally {
  723. mybatis.close();
  724. }
  725. }
  726. /**
  727. * 判断新闻是否命中二次筛选规则:标题出现任一关键字一次,或正文出现任一关键字两次。
  728. *
  729. * @param news 新闻对象
  730. * @param titleKeys 标题关键字集合
  731. * @param contentKeys 正文关键字集合
  732. * @return 命中原因;未命中返回 null
  733. */
  734. private static String matchReason(News news, Set<String> titleKeys, Set<String> contentKeys) {
  735. String title = news.getTitle() == null ? "" : news.getTitle();
  736. List<String> parts = new ArrayList<>();
  737. for (String key : titleKeys) {
  738. int cnt = countOccurrences(title, key);
  739. if (cnt >= 1) {
  740. parts.add("标题'" + key + "'x" + cnt);
  741. }
  742. }
  743. String content = news.getContent() == null ? "" : DetailCleaner.articleContentText(Jsoup.parse(news.getContent()));
  744. //篇幅控制,字数小于1500剔除
  745. //if (content.length() > 1500) {
  746. // return null;
  747. //}
  748. for (String key : contentKeys) {
  749. int cnt = countOccurrences(content, key);
  750. if (cnt >= 2) {
  751. parts.add("正文'" + key + "'x" + cnt);
  752. }
  753. }
  754. return parts.isEmpty() ? null : String.join(" | ", parts);
  755. }
  756. /**
  757. * 统计关键字在文本中的出现次数(非重叠)。
  758. */
  759. private static int countOccurrences(String text, String key) {
  760. int count = 0;
  761. int idx = 0;
  762. while ((idx = text.indexOf(key, idx)) >= 0) {
  763. count++;
  764. idx += key.length();
  765. }
  766. return count;
  767. }
  768. /**
  769. * CSV 字段转义:含逗号/引号/换行时加引号包裹,内部引号加倍。
  770. */
  771. private static String csvEscape(String field) {
  772. if (field == null) {
  773. return "";
  774. }
  775. String s = field.replace("\"", "\"\"");
  776. return s.contains(",") || s.contains("\"") || s.contains("\n") || s.contains("\r")
  777. ? "\"" + s + "\""
  778. : s;
  779. }
  780. /**
  781. * 整理结果新闻列表和新闻详情pdf(符合要求的新闻存储在result.csv文件中)
  782. * csv格式的表格(序号,id,发布日期,标题,链接,频道名称,作者,篇幅字数)
  783. * 正文通过URL获取;正文含视频时下载到 doc/序号/ 下(1.ext、2.ext...)
  784. * pdf和视频都保存到 doc/序号/ 子目录, pdf命名:序号-发布时间-新闻标题-编辑.pdf
  785. * 20260913
  786. */
  787. @Test
  788. public void resultCollectTest() throws Exception {
  789. Config config = new Config();
  790. Mybatis mybatis = new Mybatis(Db.init(config));
  791. try {
  792. NewsMapper mapper = mybatis.getMapper(NewsMapper.class);
  793. //1.读取 result.csv 中的 id 并查库
  794. List<String> ids = new ArrayList<>();
  795. for (String line : Files.readAllLines(Path.of("result1.csv"), StandardCharsets.UTF_8)) {
  796. if (line.isBlank() || line.startsWith("id,")) {
  797. continue;
  798. }
  799. int comma = line.indexOf(',');
  800. String id = comma < 0 ? line.trim() : line.substring(0, comma).trim();
  801. if (id.startsWith("\"") && id.endsWith("\"") && id.length() >= 2) {
  802. id = id.substring(1, id.length() - 1).replace("\"\"", "\"");
  803. }
  804. ids.add(id);
  805. }
  806. List<News> newsList = ids.isEmpty() ? new ArrayList<News>() : new ArrayList<>(mapper.selectBatchIds(ids));
  807. log.info("[结果整理] result.csv记录={}条 数据库命中={}条", ids.size(), newsList.size());
  808. //按发布时间升序排列,保证序号稳定
  809. newsList.sort(Comparator.comparing(News::getPublishTime, Comparator.nullsLast(Comparator.naturalOrder())));
  810. File docDir = new File("doc1");
  811. Files.createDirectories(docDir.toPath());
  812. GzCmcClient client = new GzCmcClient(config);
  813. HttpClient videoHttp = HttpClient.newHttpClient();
  814. //2.逐条处理: 正文通过URL获取 -> 下载视频 -> 清洗 -> 生成PDF, 并组装csv行
  815. List<String> lines = new ArrayList<>();
  816. lines.add("序号,id,发布日期,标题,链接,频道名称,作者,篇幅字数");
  817. int ok = 0;
  818. int seq = 0;
  819. for (News news : newsList) {
  820. seq++;
  821. try {
  822. String html;
  823. boolean fetched = true;
  824. try {
  825. html = client.fetchDetail(news.getUrl());
  826. } catch (Exception ef) {
  827. fetched = false;
  828. if (news.getContent() != null) {
  829. log.warn("[结果整理] URL抓取失败回退库内content id={} err={}", news.getId(), ef.getMessage());
  830. html = news.getContent();
  831. } else {
  832. log.error("[结果整理] 抓取失败且无库内content id={} url={} err={}", news.getId(), news.getUrl(), ef.getMessage());
  833. lines.add(resultCsvLine(seq, news, 0));
  834. continue;
  835. }
  836. }
  837. //序号子目录(时机: 需要清洗正文时创建)
  838. File seqDir = new File(docDir, String.format("%03d", seq));
  839. //下载正文中的视频到序号目录: 1.ext、2.ext...
  840. //视频采集行为控制
  841. //if (false) {
  842. if (fetched) {
  843. Files.createDirectories(seqDir.toPath());
  844. List<String> videoSrcs = extractVideoSrcs(html);
  845. for (int i = 0; i < videoSrcs.size(); i++) {
  846. try {
  847. String url = videoSrcs.get(i);
  848. Path target = seqDir.toPath().resolve((i + 1) + "." + extOf(url));
  849. int status = downloadVideo(videoHttp, url, target);
  850. log.info("[结果整理] 视频下载 序号={} id={} {}/{} code={} 文件={}", seq, news.getId(), i + 1, videoSrcs.size(), status, target.getFileName());
  851. } catch (Exception e) {
  852. log.error("[结果整理] 视频下载失败 序号={} id={} url={} err={}", seq, news.getId(), videoSrcs.get(i), e.getMessage());
  853. }
  854. }
  855. } else {
  856. log.warn("[结果整理] 回退库内content, 跳过视频 序号={} id={}", seq, news.getId());
  857. }
  858. //清洗正文并统计字数
  859. String content = fetched ? DetailCleaner.clean(html) : html;
  860. String contentText = DetailCleaner.articleContentText(Jsoup.parse(content));
  861. lines.add(resultCsvLine(seq, news, contentText.length()));
  862. //生成PDF到序号目录
  863. Files.createDirectories(seqDir.toPath());
  864. String publicTime = news.getPublishTime().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
  865. String editor = news.getEditor() == null ? "" : news.getEditor();
  866. String title = news.getTitle() == null ? "" : news.getTitle();
  867. String fileName = sanitizeFileName(String.format("%03d-%s-%s-%s.pdf", seq, publicTime, title, editor));
  868. DetailCleaner.html2pdf(content, new File(seqDir, fileName).toPath());
  869. ok++;
  870. log.info("[结果整理] PDF生成 序号={} id={} 文件={}", seq, news.getId(), fileName);
  871. } catch (Exception e) {
  872. log.error("[结果整理] 失败 序号={} id={} err={}", seq, news.getId(), e.getMessage());
  873. }
  874. }
  875. //3.目标csv存储到doc目录
  876. File csv = new File(docDir, "result.csv");
  877. Files.write(csv.toPath(), lines, StandardCharsets.UTF_8);
  878. log.info("[结果整理] 目标csv已写入: {}", csv.getAbsolutePath());
  879. log.info("[结果整理] 完成: 成功={} 失败={}", ok, newsList.size() - ok);
  880. } finally {
  881. mybatis.close();
  882. }
  883. }
  884. /**
  885. * 组装结果csv行: 序号,id,发布日期,标题,链接,频道名称,作者,篇幅字数
  886. */
  887. private static String resultCsvLine(int seq, News news, int wordCount) {
  888. return seq + "," + csvEscape(news.getId()) + ","
  889. + csvEscape(news.getPublishTime() == null ? "" : news.getPublishTime().toLocalDate().toString()) + ","
  890. + csvEscape(news.getTitle()) + "," + csvEscape(news.getUrl()) + ","
  891. + csvEscape(news.getChannelName()) + "," + csvEscape(news.getEditor()) + ","
  892. + wordCount;
  893. }
  894. /**
  895. * 提取正文中视频的下载地址: video[src] 或其子 source[src],按文档顺序去重。
  896. */
  897. private static List<String> extractVideoSrcs(String html) {
  898. Document doc = Jsoup.parse(html);
  899. Set<String> srcs = new LinkedHashSet<>();
  900. for (Element video : doc.select("video")) {
  901. String src = video.attr("src");
  902. if (!src.isBlank()) {
  903. srcs.add(src);
  904. }
  905. for (Element source : video.select("source[src]")) {
  906. String s = source.attr("src");
  907. if (!s.isBlank()) {
  908. srcs.add(s);
  909. }
  910. }
  911. }
  912. return new ArrayList<>(srcs);
  913. }
  914. /**
  915. * 从视频URL路径中提取扩展名,取不到时默认 mp4。
  916. */
  917. private static String extOf(String url) {
  918. try {
  919. String path = URI.create(url).getPath();
  920. int dot = path.lastIndexOf('.');
  921. if (dot >= 0 && dot < path.length() - 1) {
  922. String ext = path.substring(dot + 1);
  923. if (ext.matches("[a-zA-Z0-9]{1,10}")) {
  924. return ext;
  925. }
  926. }
  927. } catch (Exception ignored) {
  928. }
  929. return "mp4";
  930. }
  931. /**
  932. * 参考 mTest: 用 JDK HttpClient 直接下载视频到目标文件。
  933. */
  934. private static int downloadVideo(HttpClient http, String url, Path target) throws IOException, InterruptedException {
  935. HttpRequest request = HttpRequest.newBuilder(URI.create(url)).GET().build();
  936. HttpResponse<Path> resp = http.send(request, HttpResponse.BodyHandlers.ofFile(target));
  937. return resp.statusCode();
  938. }
  939. }