admin
2021-08-27 8fee151ffae0c3818694b7318583814bf92663e2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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.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;
 
public class HanmiApiUtil {
 
    public static HanmiShow parseShowDetail(String url) throws Exception {
        HanmiShow show = new HanmiShow();
        show.setUrl(url);
        return parseShowDetail(show);
    }
 
    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();
 
        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 year = titleItem.getElementsByClass("item-year").get(0).ownText();
 
 
        //节目信息
        Element videoBox = root.getElementsByClass("video_box").get(0);
 
        String picture = videoBox.getElementsByClass("video_img").get(0).getElementsByTag("img").attr("src");
 
        Element videoInfo = videoBox.getElementsByClass("video_info").get(0);
        String videoInfoStr = videoInfo.html();
        String[] sts = videoInfoStr.split("<br>");
        Map<String, String> infos = new HashMap<>();
        for (String st : sts) {
            Document d = Jsoup.parse(st);
            String value = d.text();
            if (value.indexOf(":") > -1)
                infos.put(value.substring(0, value.indexOf(":")).trim(), value.substring(value.indexOf(":") + 1).trim());
        }
 
        //剧集列表
        Element eposide = root.getElementsByClass("video_list_li").get(0);
        Elements eposides = eposide.getElementsByTag("a");
        List<HanmiShowEpisode> episodeList = new ArrayList<>();
        for (int i = 0; i < eposides.size(); i++) {
            String href = eposides.get(i).attr("href");
            String tag = eposides.get(i).ownText();
            HanmiShowEpisode ep = new HanmiShowEpisode();
            ep.setOrderBy(i + 1);
            ep.setPlayUrl("https://www.hmtv.me" + href);
            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("/", ","));
        if (infos.get("导演") != null)
            show.setDirector(infos.get("导演"));
        if (infos.get("类型") != null)
            show.setCategorys(infos.get("类型"));
        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.setId(show.getUrl().replace("https://www.hmtv.me/show/", "").trim());
        show.setEpisodeList(episodeList);
        show.setUrl(show.getUrl());
        show.setDesc(desc.trim());
        return show;
    }
 
 
    public static List<HanmiShow> parseList(String listUrl) throws IOException {
        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();
        Element root = doc.getElementsByClass("m-movies").get(0);
        Elements items = root.getElementsByClass("u-movie");
        for (int i = 0; i < items.size(); i++) {
            Element item = items.get(i);
            HanmiShow show = new HanmiShow();
            show.setUrl(item.getElementsByTag("a").get(0).attr("href"));
            show.setTag(item.getElementsByClass("zhuangtai").get(0).text());
            String score = item.getElementsByClass("pingfen").get(0).text();
            if (score != null) {
                score = score.replace("分", "");
                show.setScore(score);
            }
            show.setTitle(item.getElementsByTag("h2").get(0).getElementsByTag("a").get(0).ownText());
            list.add(show);
        }
 
        return list;
    }
 
    public static List<HanmiShow> parseDetailList(List<HanmiShow> showList) throws Exception {
        List<HanmiShow> list = new ArrayList<>();
        for (HanmiShow show : showList) {
            try {
                list.add(parseShowDetail(show));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }
 
    public static void main(String[] args) throws Exception {
        parseDetailList(parseList("https://www.hmtv.me/hanju"));
    }
 
}