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
// URL LINK 크롤링하는 메소드
Disposable backgroundTask;
    private void linkCrawling(String url) {
        // 크롤링한 메타데이터 정보를 담아 반환할 Hashmap
        HashMap<StringString> map = new HashMap<>();
 
        backgroundTask = Observable.fromCallable(() -> {
            try {
                Connection con = Jsoup.connect(url);
                Document doc = con.get();
                Elements ogTags = doc.select("meta[property^=og:]");
                if (ogTags.size() <= 0) {
                    return map; // 아무것도 안 들은 map 반환
                }
                // 필요한 OGTag
                for (int i = 0; i < ogTags.size(); i++) {
                    Element tag = ogTags.get(i);
                    String text = tag.attr("property");
                    if ("og:url".equals(text)) {
                        map.put("url", tag.attr("content"));
 
                    } else if ("og:title".equals(text)) {
                        map.put("title", tag.attr("content"));
 
                    } else if ("og:image".equals(text)) {
                        map.put("image", tag.attr("content"));
 
                    } else if ("og:description".equals(text)) {
                        map.put("description", tag.attr("content"));
                    }
                }
                return map;
 
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
 
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<HashMap<StringString>>() {
            @Override
            public void accept(HashMap<StringString> map) {
 
                backgroundTask.dispose();
            }
        });
cs

+ Recent posts