package cn.reghao.tnb.search.app.comment.spider.task;

import cn.reghao.jutil.jdk.http.WebRequest;
import cn.reghao.jutil.jdk.http.WebResponse;
import cn.reghao.tnb.search.app.comment.db.mongo.RichTextMongo;
import cn.reghao.tnb.search.app.comment.model.po.RichText;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.List;

/**
 * @author reghao
 * @date 2025-07-30 15:44:54
 */
@Slf4j
public class GetNewsDetailTask implements Runnable {
    private final WebRequest webRequest;
    private final RichTextMongo richTextMongo;

    public GetNewsDetailTask(WebRequest webRequest, RichTextMongo richTextMongo) {
        this.webRequest = webRequest;
        this.richTextMongo = richTextMongo;
    }

    @Override
    public void run() {
        List<RichText> richTextList = richTextMongo.findByContentIsNull();
        for (RichText richText : richTextList) {
            try {
                String pageUrl = richText.getOriginalUrl();
                WebResponse webResponse = webRequest.get(pageUrl);
                if (webResponse.getStatusCode() != 200) {
                    log.error("get {} failed", pageUrl);
                    continue;
                }

                String body = webResponse.getBody();
                String html = parsePaperNewsDetail(body);
                if (html != null) {
                    richText.setContentType("html");
                    richText.setContent(html);
                    richTextMongo.update(richText);
                }
                Thread.sleep(1_000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private String parsePaperNewsDetail(String body) {
        Document document = Jsoup.parse(body);
        Elements elements = document.select(".index_wrapper__L_zqV");
        return elements.size() > 0 ? elements.html() : null;
    }
}
