| | |
| | | package com.yeshi.buwan.videos.hanmi; |
| | | |
| | | import com.yeshi.buwan.util.StringUtil; |
| | | import com.yeshi.buwan.videos.hanmi.entity.HanmiShow; |
| | | import com.yeshi.buwan.videos.hanmi.entity.HanmiShowEpisode; |
| | | import org.jsoup.Connection; |
| | | import org.jsoup.Jsoup; |
| | | import org.jsoup.nodes.Document; |
| | | import org.jsoup.nodes.Element; |
| | | import org.jsoup.select.Elements; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | public class HanmiApiUtil { |
| | | |
| | |
| | | return parseShowDetail(show); |
| | | } |
| | | |
| | | private static Map<String, String> getHeaders() { |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("sec-fetch-dest", "document"); |
| | | headers.put("sec-fetch-mode", "navigate"); |
| | | headers.put("sec-fetch-site", "same-origin"); |
| | | headers.put("sec-fetch-user", "?1"); |
| | | headers.put("upgrade-insecure-requests", "1"); |
| | | return headers; |
| | | } |
| | | |
| | | private static Document getDoc(String link, Map<String, String> headres) throws IOException { |
| | | Connection connection = Jsoup.connect(link).timeout(60000); |
| | | if (headres != null) |
| | | for (Iterator<String> its = headres.keySet().iterator(); its.hasNext(); ) { |
| | | String key = its.next(); |
| | | connection = connection.header(key, headres.get(key)); |
| | | } |
| | | Document doc = connection |
| | | .userAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1").get(); |
| | | return doc; |
| | | } |
| | | |
| | | public static HanmiShow parseShowDetail(HanmiShow show) throws Exception { |
| | | if (show.getUrl() == null || !show.getUrl().startsWith("https://www.hmtv.me/show/")) { |
| | | throw new Exception("链接不合法"); |
| | | } |
| | | Document doc = Jsoup.connect(show.getUrl()).timeout(60000).referrer("https://www.hmtv.me/hanju").userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36").get(); |
| | | Document doc = getDoc(show.getUrl(), getHeaders()); |
| | | |
| | | Element root = doc.getElementsByClass("video-content").get(0); |
| | | Element titleItem = root.getElementsByClass("article-title").get(0); |
| | | |
| | | //标题 |
| | | String title = titleItem.getElementsByClass("item-title").get(0).ownText(); |
| | | String title = null; |
| | | try { |
| | | title = titleItem.getElementsByClass("item-title").get(0).ownText(); |
| | | } catch (IndexOutOfBoundsException e) { |
| | | } |
| | | |
| | | String year = titleItem.getElementsByClass("item-year").get(0).ownText(); |
| | | |
| | | String year = null; |
| | | try { |
| | | year = titleItem.getElementsByClass("item-year").get(0).ownText(); |
| | | } catch (IndexOutOfBoundsException e) { |
| | | } |
| | | |
| | | //节目信息 |
| | | Element videoBox = root.getElementsByClass("video_box").get(0); |
| | |
| | | Element eposide = root.getElementsByClass("video_list_li").get(0); |
| | | Elements eposides = eposide.getElementsByTag("a"); |
| | | List<HanmiShowEpisode> episodeList = new ArrayList<>(); |
| | | |
| | | |
| | | //电影 |
| | | if (show.getType() != null && show.getType().contains("影")) { |
| | | int index = 0; |
| | | // for (int i = 0; i < eposides.size(); i++) { |
| | | // String tag = eposides.get(i).ownText(); |
| | | // if (tag.contains("HD")) { |
| | | // index = i; |
| | | // break; |
| | | // } |
| | | // } |
| | | String href = eposides.get(index).attr("href"); |
| | | HanmiShowEpisode ep = new HanmiShowEpisode(); |
| | | ep.setOrderBy(0); |
| | | ep.setPlayUrl("https://www.hmtv.me" + href); |
| | | ep.setTag(show.getTitle() != null ? show.getTitle() : title); |
| | | episodeList.add(ep); |
| | | } else { |
| | | for (int i = 0; i < eposides.size(); i++) { |
| | | String href = eposides.get(i).attr("href"); |
| | | String tag = eposides.get(i).ownText(); |
| | |
| | | ep.setTag(tag); |
| | | episodeList.add(ep); |
| | | } |
| | | } |
| | | |
| | | //简介 |
| | | String desc = root.getElementsByClass("jianjie").get(0).text(); |
| | |
| | | |
| | | if (show.getTitle() == null) |
| | | show.setTitle(title.split(" ")[0]); |
| | | show.setYear(year.replace("(", "").replace(")", "")); |
| | | |
| | | show.setPicture(picture); |
| | | if (infos.get("主演") != null) |
| | | show.setActors(infos.get("主演").replace("/", ",")); |
| | | show.setActors(infos.get("主演"). |
| | | |
| | | replace("/", ",")); |
| | | if (infos.get("导演") != null) |
| | | show.setDirector(infos.get("导演")); |
| | | if (infos.get("类型") != null) |
| | |
| | | if (infos.get("国家/地区") != null) |
| | | show.setArea(infos.get("国家/地区")); |
| | | if (infos.get("首播") != null) |
| | | show.setRelaseDate(infos.get("首播").substring(0, infos.get("首播").indexOf("(") > -1 ? infos.get("首播").indexOf("(") : infos.get("首播").length())); |
| | | show.setRelaseDate(infos.get("首播"). |
| | | |
| | | show.setId(show.getUrl().replace("https://www.hmtv.me/show/", "").trim()); |
| | | substring(0, infos.get("首播"). |
| | | |
| | | indexOf("(") > -1 ? infos.get("首播"). |
| | | |
| | | indexOf("(") : infos.get("首播"). |
| | | |
| | | length())); |
| | | if (infos.get("上映日期") != null) |
| | | show.setRelaseDate(infos.get("上映日期"). |
| | | |
| | | substring(0, infos.get("上映日期"). |
| | | |
| | | indexOf("(") > -1 ? infos.get("上映日期"). |
| | | |
| | | indexOf("(") : infos.get("上映日期"). |
| | | |
| | | length())); |
| | | |
| | | |
| | | if (year == null && show.getRelaseDate() != null) { |
| | | year = show.getRelaseDate().split("-")[0]; |
| | | } |
| | | |
| | | show.setYear(year.replace("(", ""). |
| | | replace(")", "")); |
| | | |
| | | show.setId(show.getUrl(). |
| | | |
| | | replace("https://www.hmtv.me/show/", ""). |
| | | |
| | | trim()); |
| | | show.setEpisodeList(episodeList); |
| | | show.setUrl(show.getUrl()); |
| | | show.setDesc(desc.trim()); |
| | |
| | | |
| | | |
| | | public static List<HanmiShow> parseList(String listUrl) throws IOException { |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("sec-fetch-dest", "document"); |
| | | headers.put("sec-fetch-mode", "navigate"); |
| | | headers.put("sec-fetch-site", "same-origin"); |
| | | headers.put("sec-fetch-user", "?1"); |
| | | headers.put("upgrade-insecure-requests", "1"); |
| | | |
| | | |
| | | List<HanmiShow> list = new ArrayList<>(); |
| | | Document doc = Jsoup.connect(listUrl).timeout(60000).referrer("https://www.hmtv.me/hanju").userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36").get(); |
| | | Document doc = getDoc(listUrl, headers); |
| | | String type = doc.getElementsByClass("list-content").get(0).getElementsByClass("title").get(0).getElementsByTag("strong").text(); |
| | | |
| | | Element root = doc.getElementsByClass("m-movies").get(0); |
| | | Elements items = root.getElementsByClass("u-movie"); |
| | | for (int i = 0; i < items.size(); i++) { |
| | |
| | | show.setScore(score); |
| | | } |
| | | show.setTitle(item.getElementsByTag("h2").get(0).getElementsByTag("a").get(0).ownText()); |
| | | show.setType(type); |
| | | show.setId(show.getUrl(). |
| | | replace("https://www.hmtv.me/show/", ""). |
| | | trim()); |
| | | list.add(show); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据链接获取剧集 |
| | | * |
| | | * @param playUrl |
| | | * @return |
| | | */ |
| | | public static List<HanmiShowEpisode> getShowEpisodesFromPlayUrl(String playUrl) throws IOException { |
| | | List<HanmiShowEpisode> episodeList = new ArrayList<>(); |
| | | Document doc = getDoc(playUrl, null); |
| | | Element els = doc.getElementById("playnav"); |
| | | Elements items = els.getElementsByTag("li"); |
| | | int playIndex = -1; |
| | | for (int i = 0; i < items.size(); i++) { |
| | | String name = items.get(i).text(); |
| | | if (name.contains("HM")) { |
| | | playIndex = i; |
| | | break; |
| | | } |
| | | } |
| | | if (playIndex < 0) |
| | | return null; |
| | | Element tab = doc.getElementById("playcontainer").getElementsByClass("tab").get(playIndex); |
| | | Elements es = tab.getElementsByTag("a"); |
| | | |
| | | for (int i = 0; i < es.size(); i++) { |
| | | HanmiShowEpisode episode = new HanmiShowEpisode(); |
| | | String href = "https://www.hmtv.me" + es.get(i).attr("href"); |
| | | String name = es.get(i).text(); |
| | | episode.setTag(name); |
| | | episode.setPlayUrl(href); |
| | | episode.setOrderBy(i + 1); |
| | | episodeList.add(episode); |
| | | } |
| | | return episodeList; |
| | | } |
| | | |
| | | |
| | | public static List<HanmiShow> parseDetailList(List<HanmiShow> showList) throws Exception { |
| | | List<HanmiShow> list = new ArrayList<>(); |
| | |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | parseDetailList(parseList("https://www.hmtv.me/hanju")); |
| | | List<HanmiShowEpisode> list = getShowEpisodesFromPlayUrl("https://www.hmtv.me/vplay/MTExNS0xLTA=.html"); |
| | | System.out.println(list); |
| | | } |
| | | |
| | | } |