Crawler4LoveFootServiceImpl.java 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. package top.lvzhiqiang.service.impl;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.jsoup.Connection;
  4. import org.jsoup.Jsoup;
  5. import org.jsoup.nodes.Document;
  6. import org.jsoup.nodes.Element;
  7. import org.jsoup.select.Elements;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.scheduling.annotation.Async;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.transaction.annotation.Propagation;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import org.springframework.util.StopWatch;
  14. import top.lvzhiqiang.entity.CrawlerLoveFoot;
  15. import top.lvzhiqiang.entity.DicCode;
  16. import top.lvzhiqiang.exception.BusinessException;
  17. import top.lvzhiqiang.mapper.CrawlerLoveFootMapper;
  18. import top.lvzhiqiang.mapper.DicCodeMapper;
  19. import top.lvzhiqiang.mapper.VideoSitePoolMapper;
  20. import top.lvzhiqiang.service.Crawler4LoveFootService;
  21. import top.lvzhiqiang.util.DateUtils;
  22. import top.lvzhiqiang.util.JsoupUtil;
  23. import top.lvzhiqiang.util.StringUtils;
  24. import javax.annotation.Resource;
  25. import java.io.*;
  26. import java.net.InetSocketAddress;
  27. import java.net.Proxy;
  28. import java.net.URLDecoder;
  29. import java.nio.charset.StandardCharsets;
  30. import java.time.LocalDate;
  31. import java.time.LocalDateTime;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.UUID;
  36. import java.util.stream.Collectors;
  37. /**
  38. * Crawler LoveFoot ServiceImpl
  39. *
  40. * @author lvzhiqiang
  41. * 2022/10/17 14:47
  42. */
  43. @Service
  44. @Slf4j
  45. public class Crawler4LoveFootServiceImpl implements Crawler4LoveFootService {
  46. @Resource
  47. private DicCodeMapper dicCodeMapper;
  48. @Resource
  49. private CrawlerLoveFootMapper crawlerLoveFootMapper;
  50. @Resource
  51. private VideoSitePoolMapper videoSitePoolMapper;
  52. @Value("${spring.profiles.active}")
  53. private String env;
  54. Map<String, String> footConstantMap = null;
  55. Map<String, String> javbusConstantMap = null;
  56. Map<String, String> javdbConstantMap = null;
  57. List<String> javbusUrlList = null;
  58. Map<String, String> headerMap = new HashMap<>();
  59. Map<String, String> header2Map = new HashMap<>();
  60. Map<String, String> header3Map = new HashMap<>();
  61. Proxy proxy = null;
  62. public void beforeProxy() {
  63. if (null == proxy) {
  64. if ("dev".equals(env)) {
  65. proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1080));
  66. } else {
  67. proxy = Proxy.NO_PROXY;
  68. }
  69. }
  70. }
  71. @Async
  72. @Override
  73. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  74. public void jsoupLoveFoot4avnoashi(Integer status, Integer isDel, Integer ignoreRetryCount) throws Exception {
  75. log.warn("jsoupFoot4avnoashi 开始:status={},isDel={},ignoreRetryCount={}", status, isDel, ignoreRetryCount);
  76. StopWatch stopWatch = new StopWatch();
  77. stopWatch.start();
  78. if (isDel == 1) {
  79. crawlerLoveFootMapper.deleteAll();
  80. }
  81. List<DicCode> dicCodeList = dicCodeMapper.findAll();
  82. // 获取常量MAP
  83. footConstantMap = dicCodeList.stream()
  84. .filter(x -> "foot".equals(x.getCodeDesc()) && x.getEnv().contains(env))
  85. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  86. javbusConstantMap = dicCodeList.stream()
  87. .filter(x -> x.getType() != null && 1 == x.getType() && x.getEnv().contains(env))
  88. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  89. // 获取javbus防屏蔽地址
  90. javbusUrlList = videoSitePoolMapper.findUrlByTypeAndDeleteFlag(1, 1);
  91. if (javbusUrlList.size() == 0) {
  92. log.warn("javbusUrlList为空");
  93. return;
  94. }
  95. // 代理及TOKEN设置
  96. beforeProxy();
  97. // 解析原始站点
  98. jsoupLoveFoot4avnoashiSub(status, ignoreRetryCount);
  99. log.warn("jsoupFoot4avnoashi 结束:time={}", stopWatch.getTotalTimeSeconds());
  100. }
  101. @Async
  102. @Override
  103. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  104. public void jsoupLoveFoot4jpfoot(Integer status, Integer isDel, Integer ignoreRetryCount) throws Exception {
  105. log.warn("jsoupLoveFoot4jpfoot 开始:status={},isDel={},ignoreRetryCount={}", status, isDel, ignoreRetryCount);
  106. StopWatch stopWatch = new StopWatch();
  107. stopWatch.start();
  108. if (isDel == 1) {
  109. crawlerLoveFootMapper.deleteAll();
  110. }
  111. List<DicCode> dicCodeList = dicCodeMapper.findAll();
  112. // 获取常量MAP
  113. footConstantMap = dicCodeList.stream()
  114. .filter(x -> "foot".equals(x.getCodeDesc()) && x.getEnv().contains(env))
  115. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  116. javbusConstantMap = dicCodeList.stream()
  117. .filter(x -> x.getType() != null && 1 == x.getType() && x.getEnv().contains(env))
  118. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  119. javdbConstantMap = dicCodeList.stream()
  120. .filter(x -> x.getType() != null && 2 == x.getType() && x.getEnv().contains(env))
  121. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  122. // 获取javbus防屏蔽地址
  123. javbusUrlList = videoSitePoolMapper.findUrlByTypeAndDeleteFlag(1, 1);
  124. if (javbusUrlList.size() == 0) {
  125. log.warn("javbusUrlList为空");
  126. return;
  127. }
  128. // 代理及TOKEN设置
  129. beforeProxy();
  130. // 解析原始站点
  131. jsoupLoveFoot4jpfootSub(status, ignoreRetryCount);
  132. log.warn("jsoupLoveFoot4jpfoot 结束:time={}", stopWatch.getTotalTimeSeconds());
  133. }
  134. @Async
  135. @Override
  136. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  137. public void jsoupLoveFoot4CrawingFail(Integer status, Integer ignoreRetryCount, String website, String identificationCode) {
  138. log.warn("jjsoupLoveFoot4CrawingFail 开始");
  139. StopWatch stopWatch = new StopWatch();
  140. stopWatch.start();
  141. // 获取待抓取码列表
  142. List<CrawlerLoveFoot> loveFootList;
  143. if (StringUtils.isNotEmpty(identificationCode)) {
  144. loveFootList = crawlerLoveFootMapper.findByCodeAndType(identificationCode, null, null);
  145. } else {
  146. if (1 == ignoreRetryCount) {
  147. loveFootList = crawlerLoveFootMapper.findInfoByStatus4IgnoreRetryCount(status);
  148. } else {
  149. loveFootList = crawlerLoveFootMapper.findInfoByStatus(status);
  150. }
  151. }
  152. if (loveFootList.size() == 0) {
  153. log.warn("loveFootList为空");
  154. return;
  155. }
  156. log.warn("jsoupLoveFoot4CrawingFail loveFootList size={}", loveFootList.size());
  157. List<DicCode> dicCodeList = dicCodeMapper.findAll();
  158. // 获取常量MAP
  159. javbusConstantMap = dicCodeList.stream()
  160. .filter(x -> x.getType() != null && 1 == x.getType() && x.getEnv().contains(env))
  161. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  162. javdbConstantMap = dicCodeList.stream()
  163. .filter(x -> x.getType() != null && 2 == x.getType() && x.getEnv().contains(env))
  164. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  165. // 获取javbus防屏蔽地址
  166. if ("javbus".equals(website)) {
  167. javbusUrlList = videoSitePoolMapper.findUrlByTypeAndDeleteFlag(1, 1);
  168. if (javbusUrlList.size() == 0) {
  169. log.warn("javbusUrlList为空");
  170. return;
  171. }
  172. }
  173. // 代理及TOKEN设置
  174. beforeProxy();
  175. // 解析原始站点
  176. int successCount = jsoupLoveFoot4CrawingFailSub(loveFootList, website);
  177. log.warn("jjsoupLoveFoot4CrawingFail 结束:totalCount={},successCount={},time={}", loveFootList.size(), successCount, stopWatch.getTotalTimeSeconds());
  178. }
  179. @Async
  180. @Override
  181. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  182. public void jsoupLoveFoot4CrawingFail2(Integer status, Integer ignoreRetryCount, String website, String identificationCode) {
  183. log.warn("jsoupLoveFoot4CrawingFail2 开始");
  184. StopWatch stopWatch = new StopWatch();
  185. stopWatch.start();
  186. // 获取待抓取码列表
  187. List<CrawlerLoveFoot> loveFootList = crawlerLoveFootMapper.findByCodeAndType(identificationCode, null, null);
  188. if (loveFootList.size() == 0) {
  189. log.warn("loveFootList为空");
  190. return;
  191. }
  192. log.warn("jsoupLoveFoot4CrawingFail2 loveFootList size={}", loveFootList.size());
  193. List<DicCode> dicCodeList = dicCodeMapper.findAll();
  194. // 获取常量MAP
  195. javbusConstantMap = dicCodeList.stream()
  196. .filter(x -> x.getType() != null && 1 == x.getType() && x.getEnv().contains(env))
  197. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  198. javdbConstantMap = dicCodeList.stream()
  199. .filter(x -> x.getType() != null && 2 == x.getType() && x.getEnv().contains(env))
  200. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  201. // 获取javbus防屏蔽地址
  202. if ("javbus".equals(website)) {
  203. javbusUrlList = videoSitePoolMapper.findUrlByTypeAndDeleteFlag(1, 1);
  204. if (javbusUrlList.size() == 0) {
  205. log.warn("javbusUrlList为空");
  206. return;
  207. }
  208. }
  209. // 代理及TOKEN设置
  210. beforeProxy();
  211. // 解析原始站点
  212. // 通过关键词获取识别码
  213. CrawlerLoveFoot crawlerLoveFoot = loveFootList.get(0);
  214. try {
  215. String message = parseKeywordsToCode(crawlerLoveFoot, crawlerLoveFoot.getName(), "javdb");
  216. if (StringUtils.isNotEmpty(message)) {
  217. throw new Exception(message);
  218. }
  219. crawlerLoveFootMapper.insertOrUpdate4jpfoot(crawlerLoveFoot);
  220. } catch (Exception e) {
  221. log.error("jsoupLoveFoot4CrawingFail2 detail fail,sourceUrl={}", crawlerLoveFoot.getOrginJpfootUrl(), e);
  222. crawlerLoveFoot.setFailureCause(e.getMessage());
  223. crawlerLoveFootMapper.insertOrUpdate4jpfoot(crawlerLoveFoot);
  224. }
  225. log.warn("jsoupLoveFoot4CrawingFail2 结束:totalCount={},time={}", loveFootList.size(), stopWatch.getTotalTimeSeconds());
  226. }
  227. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  228. public int jsoupLoveFoot4CrawingFailSub(List<CrawlerLoveFoot> loveFootList, String website) {
  229. int successCount = 0;
  230. Document loveFootDetailDocument;
  231. for (CrawlerLoveFoot crawlerLoveFoot : loveFootList) {
  232. Document searchDocument = null;
  233. Document codeDocument;
  234. String message = null;
  235. int retryCount = 0;
  236. if (StringUtils.isEmpty(crawlerLoveFoot.getName()) && StringUtils.isNotEmpty(crawlerLoveFoot.getOrginAvnoashiUrl()) && crawlerLoveFoot.getOrginAvnoashiUrl().contains("avnoashi-1.com")) {
  237. try {
  238. headerMap.put("referer", crawlerLoveFoot.getOrginAvnoashiUrl());
  239. loveFootDetailDocument = JsoupUtil.requestDocument(crawlerLoveFoot.getOrginAvnoashiUrl(), JsoupUtil.HTTP_GET, proxy, null, header2Map, null);
  240. // 获取关键词
  241. String keywords = loveFootDetailDocument.select("div.postContents").select("td:contains(タイトル)").next("td").text();
  242. if (StringUtils.isNotEmpty(keywords)) {
  243. crawlerLoveFoot.setName(keywords);
  244. } else {
  245. crawlerLoveFoot.setName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab");
  246. }
  247. } catch (Exception e) {
  248. crawlerLoveFoot.setName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab");
  249. }
  250. } else if (StringUtils.isEmpty(crawlerLoveFoot.getName()) && StringUtils.isNotEmpty(crawlerLoveFoot.getOrginJpfootUrl()) && crawlerLoveFoot.getOrginJpfootUrl().contains("jp-foot.net")) {
  251. try {
  252. headerMap.put("referer", crawlerLoveFoot.getOrginJpfootUrl());
  253. loveFootDetailDocument = JsoupUtil.requestDocument(crawlerLoveFoot.getOrginJpfootUrl(), JsoupUtil.HTTP_GET, proxy, null, header2Map, null);
  254. // 获取关键词
  255. String keywords = loveFootDetailDocument.select("div.avdetail_detailTop").select("p.avdetail_detailTopTitle").text().trim();
  256. if (StringUtils.isNotEmpty(keywords)) {
  257. crawlerLoveFoot.setName(keywords);
  258. } else {
  259. crawlerLoveFoot.setName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  260. }
  261. } catch (Exception e) {
  262. crawlerLoveFoot.setName("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  263. }
  264. }
  265. while (retryCount <= 3) {
  266. long start = System.currentTimeMillis();
  267. String searchUrl = null;
  268. Elements itembSelects = null;
  269. try {
  270. Thread.sleep(3000);
  271. String javbusCodeUrl = null;
  272. // 获取codeUrl
  273. String codeUrl = null;
  274. String title;
  275. if ("javbus".equals(website)) {
  276. String javbusUrl = javbusUrlList.get((int) (0 + Math.random() * (javbusUrlList.size())));
  277. searchUrl = javbusUrl.concat("/search/").concat(crawlerLoveFoot.getName()).concat("&parent=ce");
  278. try {
  279. searchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  280. } catch (Exception ee) {
  281. String newName = crawlerLoveFoot.getName().substring(crawlerLoveFoot.getName().length() / 2);
  282. searchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  283. try {
  284. searchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  285. } catch (Exception eee) {
  286. newName = newName.substring(newName.length() / 2);
  287. searchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  288. try {
  289. searchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  290. } catch (Exception eeee) {
  291. // throw new BusinessException(30000, "javbus search result null");
  292. }
  293. }
  294. }
  295. if (null == searchDocument) {
  296. String newName = crawlerLoveFoot.getName().replace("●", "");
  297. searchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  298. try {
  299. searchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  300. } catch (Exception ee) {
  301. newName = newName.substring(0, newName.length() / 2);
  302. searchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  303. try {
  304. searchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  305. } catch (Exception eee) {
  306. newName = newName.substring(0, newName.length() / 2);
  307. searchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  308. try {
  309. searchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  310. } catch (Exception eeee) {
  311. // throw new BusinessException(30000, "javbus search result null");
  312. }
  313. }
  314. }
  315. }
  316. if (null == searchDocument) {
  317. if (crawlerLoveFoot.getIdentificationCode().length() < 32) {
  318. javbusCodeUrl = javbusUrl.concat("/").concat(crawlerLoveFoot.getIdentificationCode());
  319. itembSelects = new Elements();
  320. } else {
  321. throw new BusinessException(30000, "javbus search result null");
  322. }
  323. } else {
  324. itembSelects = searchDocument.select("div#waterfall").select("div.item");
  325. if (itembSelects.size() == 0) {
  326. throw new BusinessException(30000, "javbus search result null");
  327. }
  328. if (crawlerLoveFoot.getIdentificationCode().length() < 32) {
  329. javbusCodeUrl = javbusUrl.concat("/").concat(crawlerLoveFoot.getIdentificationCode());
  330. }
  331. }
  332. } else if ("javdb".equals(website)) {
  333. crawlerLoveFoot.setName(crawlerLoveFoot.getName().replace("%", "%"));
  334. searchUrl = javdbConstantMap.get("javdb").concat("search?q=").concat(crawlerLoveFoot.getName()).concat("&f=all");
  335. header3Map.put("referer", searchUrl);
  336. subsearch:
  337. {
  338. searchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, header3Map, null);
  339. itembSelects = searchDocument.select("div.movie-list").select("div.item");
  340. if (itembSelects.size() != 0) {
  341. for (Element itembSelect : itembSelects) {
  342. title = itembSelect.select("a.box").get(0).attr("title");
  343. if (title.contains(crawlerLoveFoot.getName())) {
  344. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  345. break subsearch;
  346. }
  347. String newName = crawlerLoveFoot.getName().replace("●", "さ");
  348. if (title.contains(newName)) {
  349. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  350. crawlerLoveFoot.setName(newName);
  351. break subsearch;
  352. }
  353. newName = crawlerLoveFoot.getName().replace("●", "這");
  354. if (title.contains(newName)) {
  355. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  356. crawlerLoveFoot.setName(newName);
  357. break subsearch;
  358. }
  359. newName = crawlerLoveFoot.getName().replace("○", "〇");
  360. if (title.contains(newName)) {
  361. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  362. crawlerLoveFoot.setName(newName);
  363. break subsearch;
  364. }
  365. }
  366. }
  367. String newName = crawlerLoveFoot.getName().substring(crawlerLoveFoot.getName().length() / 2);
  368. searchUrl = javdbConstantMap.get("javdb").concat("search?q=").concat(newName).concat("&f=all");
  369. searchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, header3Map, null);
  370. itembSelects = searchDocument.select("div.movie-list").select("div.item");
  371. if (itembSelects.size() != 0) {
  372. for (Element itembSelect : itembSelects) {
  373. title = itembSelect.select("a.box").get(0).attr("title");
  374. if (title.contains(crawlerLoveFoot.getName())) {
  375. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  376. break subsearch;
  377. }
  378. newName = crawlerLoveFoot.getName().replace("●", "さ");
  379. if (title.contains(newName)) {
  380. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  381. crawlerLoveFoot.setName(newName);
  382. break subsearch;
  383. }
  384. newName = crawlerLoveFoot.getName().replace("●", "這");
  385. if (title.contains(newName)) {
  386. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  387. crawlerLoveFoot.setName(newName);
  388. break subsearch;
  389. }
  390. newName = crawlerLoveFoot.getName().replace("○", "〇");
  391. if (title.contains(newName)) {
  392. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  393. crawlerLoveFoot.setName(newName);
  394. break subsearch;
  395. }
  396. }
  397. }
  398. }
  399. }
  400. if ("javbus".equals(website)) {
  401. for (Element itembSelect : itembSelects) {
  402. title = itembSelect.select("a.movie-box").get(0).select("div.photo-frame > img").attr("title");
  403. if (title.contains(crawlerLoveFoot.getName())) {
  404. codeUrl = itembSelect.select("a.movie-box").get(0).attr("abs:href");
  405. break;
  406. }
  407. String newName = crawlerLoveFoot.getName().replace("●", "さ");
  408. if (title.contains(newName)) {
  409. codeUrl = itembSelect.select("a.movie-box").get(0).attr("abs:href");
  410. crawlerLoveFoot.setName(newName);
  411. break;
  412. }
  413. String[] newNameArr = crawlerLoveFoot.getName().split("●");
  414. int matchCount = 0;
  415. for (String s : newNameArr) {
  416. if (title.contains(s)) {
  417. matchCount++;
  418. }
  419. }
  420. if (newNameArr.length == matchCount) {
  421. codeUrl = itembSelect.select("a.movie-box").get(0).attr("abs:href");
  422. crawlerLoveFoot.setName(title);
  423. break;
  424. }
  425. }
  426. if (StringUtils.isEmpty(codeUrl) && StringUtils.isNotEmpty(javbusCodeUrl)) {
  427. codeUrl = javbusCodeUrl;
  428. crawlerLoveFoot.setChangeTitleFlag(1);
  429. }
  430. if (StringUtils.isEmpty(codeUrl)) {
  431. throw new BusinessException(30000, "javbus search result mismatch");
  432. }
  433. } else if ("javdb".equals(website)) {
  434. if (StringUtils.isEmpty(codeUrl)) {
  435. throw new BusinessException(30000, "javdb search result mismatch");
  436. }
  437. }
  438. // 解析codeUrl
  439. long picTime = 999;
  440. if ("javbus".equals(website)) {
  441. codeDocument = JsoupUtil.requestDocument(codeUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  442. picTime = parseJavbusCodeDocument(codeDocument, crawlerLoveFoot);
  443. } else if ("javdb".equals(website)) {
  444. codeDocument = JsoupUtil.requestDocument(codeUrl, JsoupUtil.HTTP_GET, proxy, null, header3Map, null);
  445. picTime = parseJavdbCodeDocument(codeDocument, crawlerLoveFoot);
  446. crawlerLoveFoot.setJavdbUrl(codeUrl);
  447. }
  448. crawlerLoveFoot.setRetryCount(retryCount);
  449. crawlerLoveFoot.setType(2);
  450. crawlerLoveFoot.setStatus(3);
  451. log.warn("jsoupLoveFoot4CrawingFailSub parseKeywordsToCode success,num={},keywords={},code={},picTime={},time={}", successCount, crawlerLoveFoot.getName(), crawlerLoveFoot.getIdentificationCode(), picTime, System.currentTimeMillis() - start);
  452. break;
  453. } catch (Exception e) {
  454. ++retryCount;
  455. if (retryCount < 4) {
  456. log.error("jsoupLoveFoot4CrawingFailSub error重试:,retryCount={},time={},javdbSearchUrl={}", retryCount, System.currentTimeMillis() - start, searchUrl, e);
  457. } else if (retryCount == 4) {
  458. message = e.getMessage().length() <= 200 ? e.getMessage() : e.getMessage().substring(0, 200);
  459. }
  460. if (e instanceof BusinessException) {
  461. message = e.getMessage().length() <= 200 ? e.getMessage() : e.getMessage().substring(0, 200);
  462. break;
  463. }
  464. }
  465. }
  466. if (StringUtils.isNotEmpty(message)) {
  467. CrawlerLoveFoot crawlerLoveFoot2 = new CrawlerLoveFoot();
  468. crawlerLoveFoot2.setId(crawlerLoveFoot.getId());
  469. crawlerLoveFoot2.setFailureCause(message);
  470. crawlerLoveFoot2.setRetryCount(retryCount);
  471. crawlerLoveFootMapper.updateInfoById(crawlerLoveFoot2);
  472. } else {
  473. crawlerLoveFoot.setFailureCause("");
  474. Integer exist = crawlerLoveFootMapper.existLoveFootByCode(crawlerLoveFoot.getIdentificationCode());
  475. if (exist != null) {
  476. crawlerLoveFootMapper.updateInfoByCode(crawlerLoveFoot);
  477. } else {
  478. crawlerLoveFootMapper.updateInfoById(crawlerLoveFoot);
  479. }
  480. successCount++;
  481. }
  482. }
  483. return successCount;
  484. }
  485. private long parseJavdbCodeDocument(Document javdbCodeDocument, CrawlerLoveFoot crawlerLoveFoot) throws IOException {
  486. Elements container = javdbCodeDocument.select("section.section > div.container");
  487. if (container.size() == 0) {
  488. throw new BusinessException(30000, "番号无效!");
  489. }
  490. Elements videoDetail = container.select("div.video-detail");
  491. // 名称
  492. // crawlerLoveFoot.setName(videoDetail.select("h2.title").select("strong.current-title").text().trim());
  493. Elements moviePanelInfos = videoDetail.select("nav.movie-panel-info");
  494. Element pEle = moviePanelInfos.get(0);
  495. // 识别码
  496. String iCode = pEle.select("div:contains(番號)").select("span.value").first().text().replace(" ", "").replaceAll("\\s+", "");
  497. crawlerLoveFoot.setIdentificationCode(iCode);
  498. // 发行日期
  499. String issueDate = pEle.select("div:contains(日期)").select("span.value").first().text().replace(" ", "").replaceAll("\\s+", "");
  500. crawlerLoveFoot.setIssueDate(LocalDate.parse(issueDate, DateUtils.dateFormatter));
  501. // 长度
  502. String length = pEle.select("div:contains(時長)").select("span.value").first().text().replace(" ", "").replaceAll("\\s+", "");
  503. crawlerLoveFoot.setLength(length);
  504. // 导演
  505. Elements directorEles = pEle.select("div:contains(導演)").select("span.value");
  506. if (directorEles.size() > 0) {
  507. crawlerLoveFoot.setDirector(directorEles.first().select("a[href]").first().text().replace(" ", "").replaceAll("\\s+", ""));
  508. }
  509. // 制作商/片商
  510. Elements markerEles = pEle.select("div:contains(片商)").select("span.value");
  511. if (markerEles.size() > 0) {
  512. crawlerLoveFoot.setMaker(markerEles.first().select("a[href]").first().text().replace(" ", "").replaceAll("\\s+", ""));
  513. }
  514. // 发行商
  515. Elements issuerEles = pEle.select("div:contains(發行)").select("span.value");
  516. if (issuerEles.size() > 0) {
  517. crawlerLoveFoot.setIssuer(issuerEles.first().select("a[href]").first().text().replace(" ", "").replaceAll("\\s+", ""));
  518. }
  519. // 类别
  520. Elements genresEles = pEle.select("div:contains(類別)").select("span.value");
  521. if (genresEles.size() > 0) {
  522. StringBuffer sb = new StringBuffer();
  523. Elements ahrefEles = genresEles.first().select("a[href]");
  524. for (Element ahrefEle : ahrefEles) {
  525. sb.append(ahrefEle.text().replace(" ", "").replaceAll("\\s+", "")).append(",");
  526. }
  527. if (sb.length() > 0) {
  528. sb = sb.deleteCharAt(sb.length() - 1);
  529. }
  530. crawlerLoveFoot.setGenres(sb.toString());
  531. }
  532. // 演员
  533. Elements castEles = pEle.select("div:contains(演員)").select("span.value");
  534. if (castEles.size() > 0) {
  535. StringBuffer sb = new StringBuffer();
  536. Elements ahrefEles = castEles.first().select("a[href]");
  537. for (Element ahrefEle : ahrefEles) {
  538. sb.append(ahrefEle.text().replace(" ", "").replaceAll("\\s+", "")).append(",");
  539. }
  540. if (sb.length() > 0) {
  541. sb = sb.deleteCharAt(sb.length() - 1);
  542. }
  543. crawlerLoveFoot.setCast(sb.toString());
  544. }
  545. // 图片URL
  546. Elements videoMetaPanel = videoDetail.select("div.column-video-cover");
  547. String href = videoMetaPanel.select("a > img").first().attr("src");
  548. long start = System.currentTimeMillis();
  549. Connection.Response response = Jsoup.connect(href).method(Connection.Method.GET).ignoreContentType(true).timeout(50 * 1000).execute();
  550. String fileName = issueDate.concat(" ").concat(iCode).concat(" ").concat(StringUtils.escapeJavParam(crawlerLoveFoot.getName()));
  551. byte[] imgUrlBytes = fileName.getBytes(StandardCharsets.UTF_8);
  552. if (imgUrlBytes.length > 251) {
  553. byte[] imgUrlDestBytes = new byte[251];
  554. System.arraycopy(imgUrlBytes, 0, imgUrlDestBytes, 0, 251);
  555. fileName = new String(imgUrlDestBytes, StandardCharsets.UTF_8).replace("�", "");
  556. }
  557. fileName = fileName.concat(".jpg");
  558. String machiImgUrl = "足舐/".concat(fileName);
  559. saveFile(response.bodyStream(), javbusConstantMap.get("apics_path").concat(machiImgUrl));
  560. long end = System.currentTimeMillis();
  561. crawlerLoveFoot.setImgUrl(machiImgUrl);
  562. return end - start;
  563. }
  564. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  565. public void jsoupLoveFoot4avnoashiSub(Integer status, Integer ignoreRetryCount) throws Exception {
  566. CrawlerLoveFoot latestLoveFoot = crawlerLoveFootMapper.findLatestInfo4avnoashi();
  567. LocalDate latestDate;
  568. if (latestLoveFoot == null) {
  569. latestDate = LocalDate.of(1970, 1, 1);
  570. } else {
  571. latestDate = latestLoveFoot.getUpdateDate();
  572. }
  573. List<DicCode> dicCodeList = dicCodeMapper.findAll();
  574. javdbConstantMap = dicCodeList.stream()
  575. .filter(x -> x.getType() != null && 2 == x.getType() && x.getEnv().contains(env))
  576. .collect(Collectors.toMap(DicCode::getCodeKey, DicCode::getCodeValue, (key1, key2) -> key1));
  577. String avnoashiUrl = footConstantMap.get("avnoashi_url");
  578. headerMap.put("referer", avnoashiUrl);
  579. header2Map.put("referer", avnoashiUrl.concat("?sort=newer"));
  580. Document loveFootDocument;
  581. Document loveFootDetailDocument;
  582. boolean tiaoguoFlag = true;
  583. int pageNum = 0;
  584. while (true) {
  585. loveFootDocument = JsoupUtil.requestDocument(avnoashiUrl, JsoupUtil.HTTP_GET, proxy, null, headerMap, null);
  586. pageNum++;
  587. if (pageNum > 50) {
  588. break;
  589. }
  590. log.warn("jsoupLoveFoot4avnoashiSub page success:url={}", avnoashiUrl);
  591. Elements sourceSelects = loveFootDocument.select("div.dividerBottom > div.archive").select("div.archive__contents").select("h2");
  592. for (Element sourceSelect : sourceSelects) {
  593. String sourceUrl = sourceSelect.select("a").attr("abs:href");
  594. if (tiaoguoFlag) {
  595. tiaoguoFlag = false;
  596. continue;
  597. }
  598. Integer statusInt = 2;
  599. Integer typeInt = 1;
  600. LocalDate clockDate = null;
  601. LocalDate updateDate = null;
  602. String keywords = null;
  603. try {
  604. loveFootDetailDocument = JsoupUtil.requestDocument(sourceUrl, JsoupUtil.HTTP_GET, proxy, null, header2Map, null);
  605. /*String clockDateStr = loveFootDetailDocument.select("div.viral").select("li.icon-clock").text();
  606. String updateDateStr = loveFootDetailDocument.select("div.viral").select("li.icon-update").text();
  607. clockDate = LocalDate.parse(clockDateStr, DateUtils.dateFormatter3);
  608. updateDate = LocalDate.parse(updateDateStr, DateUtils.dateFormatter3);
  609. if (updateDate.isBefore(latestDate) || updateDate.isEqual(latestDate)) {
  610. break outer;
  611. }*/
  612. // 获取关键词
  613. keywords = loveFootDetailDocument.select("div.postContents").select("td:contains(タイトル)").next("td").text();
  614. if (StringUtils.isNotEmpty(keywords)) {
  615. statusInt = 1;
  616. log.warn("jsoupLoveFoot4avnoashiSub parseDetailToKeywords success,sourceUrl={},keywords={}", sourceUrl, keywords);
  617. } else {
  618. throw new Exception("keywords is null");
  619. }
  620. // 通过关键词获取识别码
  621. CrawlerLoveFoot crawlerLoveFoot = new CrawlerLoveFoot();
  622. crawlerLoveFoot.setClockDate(clockDate);
  623. crawlerLoveFoot.setUpdateDate(updateDate);
  624. crawlerLoveFoot.setOrginAvnoashiUrl(sourceUrl);
  625. crawlerLoveFoot.setType(2);
  626. crawlerLoveFoot.setStatus(3);
  627. crawlerLoveFoot.setCreateTime(LocalDateTime.now());
  628. crawlerLoveFoot.setName(keywords);
  629. Thread.sleep(3000);
  630. String message = parseKeywordsToCode(crawlerLoveFoot, keywords, "javdb");
  631. if (StringUtils.isNotEmpty(message)) {
  632. statusInt = 4;
  633. throw new Exception(message);
  634. }
  635. CrawlerLoveFoot exist = crawlerLoveFootMapper.findLoveFootByOrginAvnoashiUrl(sourceUrl);
  636. if (exist == null || exist.getStatus() != 3) {
  637. crawlerLoveFootMapper.insertOrUpdate4avnoashi(crawlerLoveFoot);
  638. }
  639. } catch (Exception e) {
  640. Integer exist = crawlerLoveFootMapper.existLoveFootByOrginAvnoashiUrl(sourceUrl);
  641. if (exist == null) {
  642. log.error("jsoupLoveFoot4avnoashiSub detail fail,sourceUrl={}", sourceUrl, e);
  643. CrawlerLoveFoot crawlerLoveFoot = new CrawlerLoveFoot();
  644. crawlerLoveFoot.setIdentificationCode(UUID.randomUUID().toString());
  645. crawlerLoveFoot.setOrginAvnoashiUrl(sourceUrl);
  646. crawlerLoveFoot.setClockDate(clockDate);
  647. crawlerLoveFoot.setUpdateDate(updateDate);
  648. crawlerLoveFoot.setName(keywords);
  649. crawlerLoveFoot.setType(typeInt);
  650. crawlerLoveFoot.setStatus(statusInt);
  651. crawlerLoveFoot.setCreateTime(LocalDateTime.now());
  652. crawlerLoveFoot.setFailureCause(e.getMessage());
  653. crawlerLoveFootMapper.insertOrUpdate4avnoashi(crawlerLoveFoot);
  654. }
  655. }
  656. }
  657. // 继续下一页
  658. Elements nextSelects = loveFootDocument.select("ul.pager").select("a:contains(Next)");
  659. if (nextSelects.size() > 0) {
  660. avnoashiUrl = nextSelects.get(0).attr("abs:href");
  661. } else {
  662. break;
  663. }
  664. }
  665. }
  666. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  667. public void jsoupLoveFoot4jpfootSub(Integer status, Integer ignoreRetryCount) throws Exception {
  668. CrawlerLoveFoot latestLoveFoot = crawlerLoveFootMapper.findLatestInfo4jpfoot();
  669. LocalDate latestDate;
  670. if (latestLoveFoot == null) {
  671. latestDate = LocalDate.of(1970, 1, 1);
  672. } else {
  673. latestDate = latestLoveFoot.getUpdateDate();
  674. }
  675. String jpfootUrl = footConstantMap.get("jpfoot_url");
  676. headerMap.put("referer", jpfootUrl);
  677. Document loveFootDocument;
  678. Document loveFootDetailDocument;
  679. outer:
  680. while (true) {
  681. loveFootDocument = JsoupUtil.requestDocument(jpfootUrl, JsoupUtil.HTTP_GET, proxy, null, headerMap, null);
  682. log.warn("jsoupLoveFoot4jpfootSub page success:url={}", jpfootUrl);
  683. Elements sourceSelects = loveFootDocument.select("article.mainContainer > div.av_itemGrid").select("article.av_item");
  684. for (Element sourceSelect : sourceSelects) {
  685. Thread.sleep(1000L);
  686. String sourceUrl = sourceSelect.select("a.av_itemLink").attr("abs:href");
  687. sourceUrl = URLDecoder.decode(sourceUrl, "UTF-8");
  688. Integer statusInt = 2;
  689. Integer typeInt = 1;
  690. LocalDate clockDate = null;
  691. LocalDate updateDate = null;
  692. String keywords = null;
  693. try {
  694. loveFootDetailDocument = JsoupUtil.requestDocument(sourceUrl, JsoupUtil.HTTP_GET, proxy, null, headerMap, null);
  695. String dateStr = loveFootDetailDocument.select("div.avdetail_date").select("span.avdetail_dateText").text();
  696. clockDate = LocalDate.parse(dateStr, DateUtils.dateFormatter4);
  697. updateDate = clockDate;
  698. if (updateDate.isBefore(latestDate) || updateDate.isEqual(latestDate)) {
  699. break outer;
  700. }
  701. // 获取关键词
  702. keywords = loveFootDetailDocument.select("div.avdetail_detailTop").select("p.avdetail_detailTopTitle").text().trim();
  703. if (StringUtils.isNotEmpty(keywords)) {
  704. statusInt = 1;
  705. log.warn("jsoupLoveFoot4jpfootSub parseDetailToKeywords success,sourceUrl={},keywords={}", sourceUrl, keywords);
  706. } else {
  707. throw new Exception("keywords is null");
  708. }
  709. // 通过关键词获取识别码
  710. CrawlerLoveFoot crawlerLoveFoot = new CrawlerLoveFoot();
  711. crawlerLoveFoot.setClockDate(clockDate);
  712. crawlerLoveFoot.setUpdateDate(updateDate);
  713. crawlerLoveFoot.setOrginJpfootUrl(sourceUrl);
  714. crawlerLoveFoot.setType(2);
  715. crawlerLoveFoot.setStatus(3);
  716. crawlerLoveFoot.setCreateTime(LocalDateTime.now());
  717. crawlerLoveFoot.setName(keywords);
  718. Thread.sleep(3000);
  719. String message = parseKeywordsToCode(crawlerLoveFoot, keywords, "javdb");
  720. if (StringUtils.isNotEmpty(message)) {
  721. statusInt = 4;
  722. throw new Exception(message);
  723. }
  724. crawlerLoveFootMapper.insertOrUpdate4jpfoot(crawlerLoveFoot);
  725. } catch (Exception e) {
  726. log.error("jsoupLoveFoot4jpfootSub detail fail,sourceUrl={}", sourceUrl, e);
  727. CrawlerLoveFoot crawlerLoveFoot = new CrawlerLoveFoot();
  728. crawlerLoveFoot.setIdentificationCode(UUID.randomUUID().toString());
  729. crawlerLoveFoot.setOrginJpfootUrl(sourceUrl);
  730. crawlerLoveFoot.setClockDate(clockDate);
  731. crawlerLoveFoot.setUpdateDate(updateDate);
  732. crawlerLoveFoot.setName(keywords);
  733. crawlerLoveFoot.setType(typeInt);
  734. crawlerLoveFoot.setStatus(statusInt);
  735. crawlerLoveFoot.setCreateTime(LocalDateTime.now());
  736. crawlerLoveFoot.setFailureCause(e.getMessage());
  737. crawlerLoveFootMapper.insertOrUpdate4jpfoot(crawlerLoveFoot);
  738. }
  739. }
  740. // 继续下一页
  741. Elements nextSelects = loveFootDocument.select("nav.pagination > div.nav-links").select("a.next");
  742. if (nextSelects.size() > 0) {
  743. jpfootUrl = nextSelects.get(0).attr("abs:href");
  744. } else {
  745. break;
  746. }
  747. }
  748. }
  749. private String parseKeywordsToCode(CrawlerLoveFoot crawlerLoveFoot, String keywords, String website) {
  750. int retryCount = 0;
  751. Document javbusSearchDocument = null;
  752. Document javbusCodeDocument;
  753. Document javdbSearchDocument;
  754. Document javdbCodeDocument;
  755. String message = null;
  756. while (retryCount <= 3) {
  757. long start = System.currentTimeMillis();
  758. Elements itembSelects = null;
  759. try {
  760. Thread.sleep(3000);
  761. // 获取codeUrl
  762. String codeUrl = null;
  763. String title;
  764. if ("javbus".equals(website)) {
  765. String javbusUrl = javbusUrlList.get((int) (0 + Math.random() * (javbusUrlList.size())));
  766. String javbusSearchUrl = javbusUrl.concat("/search/").concat(keywords).concat("&parent=ce");
  767. try {
  768. javbusSearchDocument = JsoupUtil.requestDocument(javbusSearchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  769. } catch (Exception ee) {
  770. String newName = keywords.substring(keywords.length() / 2);
  771. javbusSearchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  772. try {
  773. javbusSearchDocument = JsoupUtil.requestDocument(javbusSearchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  774. } catch (Exception eee) {
  775. newName = newName.substring(newName.length() / 2);
  776. javbusSearchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  777. try {
  778. javbusSearchDocument = JsoupUtil.requestDocument(javbusSearchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  779. } catch (Exception eeee) {
  780. // throw new BusinessException(30000, "javbus search result null");
  781. }
  782. }
  783. }
  784. if (null == javbusSearchDocument) {
  785. String newName = keywords.replace("●", "");
  786. javbusSearchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  787. try {
  788. javbusSearchDocument = JsoupUtil.requestDocument(javbusSearchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  789. } catch (Exception ee) {
  790. newName = newName.substring(0, newName.length() / 2);
  791. javbusSearchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  792. try {
  793. javbusSearchDocument = JsoupUtil.requestDocument(javbusSearchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  794. } catch (Exception eee) {
  795. newName = newName.substring(0, newName.length() / 2);
  796. javbusSearchUrl = javbusUrl.concat("/search/").concat(newName).concat("&parent=ce");
  797. try {
  798. javbusSearchDocument = JsoupUtil.requestDocument(javbusSearchUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  799. } catch (Exception eeee) {
  800. throw new BusinessException(30000, "javbus search result null");
  801. }
  802. }
  803. }
  804. }
  805. itembSelects = javbusSearchDocument.select("div#waterfall").select("div.item");
  806. if (itembSelects.size() == 0) {
  807. throw new BusinessException(30000, "javbus search result null");
  808. }
  809. } else if ("javdb".equals(website)) {
  810. crawlerLoveFoot.setName(crawlerLoveFoot.getName().replace("%", "%").replace("#", "#").replace("?", "?"));
  811. String searchUrl = javdbConstantMap.get("javdb").concat("search?q=").concat(crawlerLoveFoot.getName()).concat("&f=all");
  812. header3Map.put("referer", searchUrl);
  813. subsearch:
  814. {
  815. javdbSearchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, header3Map, null);
  816. itembSelects = javdbSearchDocument.select("div.movie-list").select("div.item");
  817. if (itembSelects.size() != 0) {
  818. for (Element itembSelect : itembSelects) {
  819. title = itembSelect.select("a.box").get(0).attr("title");
  820. if (title.contains(crawlerLoveFoot.getName())) {
  821. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  822. break subsearch;
  823. }
  824. String newName = crawlerLoveFoot.getName().replace("●", "さ");
  825. if (title.contains(newName)) {
  826. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  827. crawlerLoveFoot.setName(newName);
  828. break subsearch;
  829. }
  830. newName = crawlerLoveFoot.getName().replace("●", "這");
  831. if (title.contains(newName)) {
  832. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  833. crawlerLoveFoot.setName(newName);
  834. break subsearch;
  835. }
  836. newName = crawlerLoveFoot.getName().replace("○", "〇");
  837. if (title.contains(newName)) {
  838. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  839. crawlerLoveFoot.setName(newName);
  840. break subsearch;
  841. }
  842. }
  843. }
  844. String newName = crawlerLoveFoot.getName().substring(crawlerLoveFoot.getName().length() / 2);
  845. searchUrl = javdbConstantMap.get("javdb").concat("search?q=").concat(newName).concat("&f=all");
  846. javdbSearchDocument = JsoupUtil.requestDocument(searchUrl, JsoupUtil.HTTP_GET, proxy, null, header3Map, null);
  847. itembSelects = javdbSearchDocument.select("div.movie-list").select("div.item");
  848. if (itembSelects.size() != 0) {
  849. for (Element itembSelect : itembSelects) {
  850. title = itembSelect.select("a.box").get(0).attr("title");
  851. if (title.contains(crawlerLoveFoot.getName())) {
  852. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  853. break subsearch;
  854. }
  855. newName = crawlerLoveFoot.getName().replace("●", "さ");
  856. if (title.contains(newName)) {
  857. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  858. crawlerLoveFoot.setName(newName);
  859. break subsearch;
  860. }
  861. newName = crawlerLoveFoot.getName().replace("●", "這");
  862. if (title.contains(newName)) {
  863. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  864. crawlerLoveFoot.setName(newName);
  865. break subsearch;
  866. }
  867. newName = crawlerLoveFoot.getName().replace("○", "〇");
  868. if (title.contains(newName)) {
  869. codeUrl = itembSelect.select("a.box").get(0).attr("abs:href");
  870. crawlerLoveFoot.setName(newName);
  871. break subsearch;
  872. }
  873. }
  874. }
  875. }
  876. }
  877. if ("javbus".equals(website)) {
  878. for (Element itembSelect : itembSelects) {
  879. title = itembSelect.select("a.movie-box").get(0).select("div.photo-frame > img").attr("title");
  880. if (title.contains(keywords)) {
  881. codeUrl = itembSelect.select("a.movie-box").get(0).attr("abs:href");
  882. break;
  883. }
  884. String newName = keywords.replace("●", "さ");
  885. if (title.contains(newName)) {
  886. codeUrl = itembSelect.select("a.movie-box").get(0).attr("abs:href");
  887. crawlerLoveFoot.setName(newName);
  888. break;
  889. }
  890. String[] newNameArr = keywords.split("●");
  891. int matchCount = 0;
  892. for (String s : newNameArr) {
  893. if (title.contains(s)) {
  894. matchCount++;
  895. }
  896. }
  897. if (newNameArr.length == matchCount) {
  898. codeUrl = itembSelect.select("a.movie-box").get(0).attr("abs:href");
  899. crawlerLoveFoot.setName(title);
  900. break;
  901. }
  902. }
  903. if (StringUtils.isEmpty(codeUrl)) {
  904. throw new BusinessException(30000, "javbus search result mismatch");
  905. }
  906. } else if ("javdb".equals(website)) {
  907. if (StringUtils.isEmpty(codeUrl)) {
  908. throw new BusinessException(30000, "javdb search result mismatch");
  909. }
  910. }
  911. // 解析codeUrl
  912. long picTime = 999;
  913. if ("javbus".equals(website)) {
  914. javbusCodeDocument = JsoupUtil.requestDocument(codeUrl, JsoupUtil.HTTP_GET, proxy, null, null, null);
  915. picTime = parseJavbusCodeDocument(javbusCodeDocument, crawlerLoveFoot);
  916. } else if ("javdb".equals(website)) {
  917. javdbCodeDocument = JsoupUtil.requestDocument(codeUrl, JsoupUtil.HTTP_GET, proxy, null, header3Map, null);
  918. picTime = parseJavdbCodeDocument(javdbCodeDocument, crawlerLoveFoot);
  919. crawlerLoveFoot.setJavdbUrl(codeUrl);
  920. }
  921. crawlerLoveFoot.setRetryCount(retryCount);
  922. log.warn("jsoupLoveFoot4avnoashiSub parseKeywordsToCode success,keywords={},code={},picTime={},time={}", keywords, crawlerLoveFoot.getIdentificationCode(), picTime, System.currentTimeMillis() - start);
  923. break;
  924. } catch (Exception e) {
  925. ++retryCount;
  926. if (retryCount < 4) {
  927. log.error("javbusSearch error重试:,retryCount={},time={},keywords={}", retryCount, System.currentTimeMillis() - start, keywords, e);
  928. } else if (retryCount == 4) {
  929. message = e.getMessage().length() <= 200 ? e.getMessage() : e.getMessage().substring(0, 200);
  930. }
  931. if (e instanceof BusinessException) {
  932. message = e.getMessage().length() <= 200 ? e.getMessage() : e.getMessage().substring(0, 200);
  933. break;
  934. }
  935. }
  936. }
  937. return message;
  938. }
  939. private long parseJavbusCodeDocument(Document document, CrawlerLoveFoot crawlerLoveFoot) throws Exception {
  940. Elements container = document.select("div.container");
  941. if (container.size() == 0) {
  942. throw new BusinessException(30000, "番号无效!");
  943. }
  944. // 名称
  945. String h3 = container.select("h3").first().text();
  946. String[] nameArr = h3.split("\\s+");
  947. if (nameArr.length > 1) {
  948. crawlerLoveFoot.setName(h3.substring(nameArr[0].length()).trim());
  949. } else {
  950. crawlerLoveFoot.setName(nameArr[0]);
  951. }
  952. Elements pEles = container.select("div.info > p");
  953. // 识别码
  954. Element pEle = pEles.get(0);
  955. String iCode = pEle.select("span[style]").first().text();
  956. crawlerLoveFoot.setIdentificationCode(iCode);
  957. // 发行日期
  958. pEle = pEles.get(1);
  959. String issueDate = pEle.text().split(":")[1].replace("\"", "").trim();
  960. crawlerLoveFoot.setIssueDate(LocalDate.parse(issueDate, DateUtils.dateFormatter));
  961. // 长度
  962. pEle = pEles.get(2);
  963. String length = pEle.text().split(":")[1].replace("\"", "").trim();
  964. crawlerLoveFoot.setLength(length);
  965. // 导演
  966. Elements directorEles = container.select("div.info").select("p:contains(導演)");
  967. if (directorEles.size() > 0) {
  968. pEle = directorEles.first().select("a[href]").first();
  969. crawlerLoveFoot.setDirector(pEle.text());
  970. }
  971. // 制作商
  972. Elements markerEles = container.select("div.info").select("p:contains(製作商)");
  973. if (markerEles.size() > 0) {
  974. pEle = markerEles.first().select("a[href]").first();
  975. crawlerLoveFoot.setMaker(pEle.text());
  976. }
  977. // 发行商
  978. Elements issuerEles = container.select("div.info").select("p:contains(發行商)");
  979. if (issuerEles.size() > 0) {
  980. pEle = issuerEles.first().select("a[href]").first();
  981. crawlerLoveFoot.setIssuer(pEle.text());
  982. }
  983. // 类别
  984. Elements genresEles = container.select("div.info").select("p:contains(類別)");
  985. if (genresEles.size() > 0) {
  986. StringBuffer sb = new StringBuffer();
  987. Elements ahrefEles = genresEles.first().nextElementSibling().select("a[href]");
  988. for (Element ahrefEle : ahrefEles) {
  989. sb.append(ahrefEle.text()).append(",");
  990. }
  991. if (sb.length() > 0) {
  992. sb = sb.deleteCharAt(sb.length() - 1);
  993. }
  994. crawlerLoveFoot.setGenres(sb.toString());
  995. }
  996. // 演员
  997. Elements castEles = container.select("div.info").select("p.star-show:contains(演員)");
  998. if (castEles.size() > 0) {
  999. Elements castElesTemp = container.select("div.info:contains(暫無出演者資訊)");
  1000. if (castElesTemp.size() == 0) {
  1001. StringBuffer sb = new StringBuffer();
  1002. Elements ahrefEles = castEles.first().nextElementSibling().nextElementSibling().select("a[href]");
  1003. for (Element ahrefEle : ahrefEles) {
  1004. sb.append(ahrefEle.text()).append(",");
  1005. }
  1006. if (sb.length() > 0) {
  1007. sb = sb.deleteCharAt(sb.length() - 1);
  1008. }
  1009. crawlerLoveFoot.setCast(sb.toString());
  1010. }
  1011. }
  1012. // 图片URL
  1013. String href = container.select("a.bigImage").first().attr("abs:href");
  1014. long start = System.currentTimeMillis();
  1015. Connection.Response response = Jsoup.connect(href).method(Connection.Method.GET).ignoreContentType(true).timeout(50 * 1000).execute();
  1016. String fileName = issueDate.concat(" ").concat(h3).replace("/", "_").replace("[email protected]", "");
  1017. byte[] imgUrlBytes = fileName.getBytes(StandardCharsets.UTF_8);
  1018. if (imgUrlBytes.length > 251) {
  1019. byte[] imgUrlDestBytes = new byte[251];
  1020. System.arraycopy(imgUrlBytes, 0, imgUrlDestBytes, 0, 251);
  1021. fileName = new String(imgUrlDestBytes, StandardCharsets.UTF_8).replace("�", "");
  1022. }
  1023. fileName = fileName.concat(".jpg");
  1024. String machiImgUrl = "足舐/".concat(fileName);
  1025. saveFile(response.bodyStream(), javbusConstantMap.get("apics_path").concat(machiImgUrl));
  1026. long end = System.currentTimeMillis();
  1027. crawlerLoveFoot.setImgUrl(machiImgUrl);
  1028. return end - start;
  1029. }
  1030. /**
  1031. * 保存文件到本地
  1032. *
  1033. * @param bufferedInputStream
  1034. * @param savePath
  1035. */
  1036. private void saveFile(BufferedInputStream bufferedInputStream, String savePath) throws IOException {
  1037. //一次最多读取1k
  1038. byte[] buffer = new byte[1024];
  1039. //实际读取的长度
  1040. int readLenghth;
  1041. //创建的一个写出的缓冲流
  1042. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(savePath)));
  1043. //文件逐步写入本地
  1044. while ((readLenghth = bufferedInputStream.read(buffer, 0, 1024)) != -1) {//先读出来,保存在buffer数组中
  1045. bufferedOutputStream.write(buffer, 0, readLenghth);//再从buffer中取出来保存到本地
  1046. }
  1047. //关闭缓冲流
  1048. bufferedOutputStream.close();
  1049. bufferedInputStream.close();
  1050. }
  1051. public static void main(String[] args) throws UnsupportedEncodingException {
  1052. String s = "リア充反対!彼女の目の前で彼氏を拘束、●す鬼畜痴女";
  1053. String newName = s.substring(s.length() / 2);
  1054. newName = newName.substring(newName.length() / 2);
  1055. System.out.println(newName);
  1056. String s1 = "https://jp-foot.net/av/%e8%bb%9f%e4%bd%93%e5%a5%b3%e5%ad%90%e5%a4%a7%e7%94%9f%e9%bb%92%e5%b7%9d%e3%81%99%e3%81%bf%e3%82%8c%ef%bc%88%e7%a8%b2%e5%b7%9d%e3%81%aa%e3%81%a4%e3%82%81%ef%bc%89%e3%81%ae%e3%82%aa%e3%83%8a%e3%83%8b/";
  1057. System.out.println(s1.length());
  1058. String decode = URLDecoder.decode(s1, "UTF-8");
  1059. System.out.println(decode);
  1060. }
  1061. }