package com.yutou.nas.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jcraft.jsch.jce.ARCFOUR;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

public class RssXMLtoJson {
    public static JSONObject toJSON(String xmlUrl) {
        String xml = HttpTools.https_get(xmlUrl, true, null);
        SAXReader reader = new SAXReader();
        JSONObject json = new JSONObject();
        try {
            json.put("status", "ok");
            JSONArray array = new JSONArray();
            BufferedReader br = new BufferedReader(new StringReader(xml));
            Document document = reader.read(br);
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.element("channel").elements("item");
            for (Element element : elements) {
                JSONObject item = new JSONObject();
                JSONObject enclosure = new JSONObject();
                String title = element.element("title").content().get(0).getText();
                String magnet = element.element("enclosure").attribute("url").getValue();
                String time = element.element("pubDate").content().get(0).getText();
                enclosure.put("link", magnet);
                item.put("title", title);
                item.put("enclosure", enclosure);
                item.put("time", time);
                array.add(item);
            }
            json.put("items", array);
        } catch (Exception e) {
            json.put("status", e.getMessage());
        }
        return json;
    }

    public static void main(String[] args) {
        String xmlUrl = "https://share.dmhy.org/topics/rss/rss.xml?keyword=%E5%96%B5%E8%90%8C%E5%A5%B6%E8%8C%B6%E5%B1%8B%20%E8%8D%AF%E5%B1%8B%E5%B0%91%E5%A5%B3%E7%9A%84%E5%91%A2%E5%96%83%20%E7%AE%80%E6%97%A5";
        String xml = HttpTools.https_get(xmlUrl, true, null);

        SAXReader reader = new SAXReader();
        JSONObject json = new JSONObject();
        try {
            json.put("status", "ok");
            JSONArray array = new JSONArray();
            BufferedReader br = new BufferedReader(new StringReader(xml));
            Document document = reader.read(br);
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.element("channel").elements("item");
            for (Element element : elements) {
                JSONObject item = new JSONObject();
                JSONObject enclosure = new JSONObject();
                String title = element.element("title").content().get(0).getText();
                String magnet = element.element("enclosure").attribute("url").getValue();
                String time = element.element("pubDate").content().get(0).getText();
                enclosure.put("link", magnet);
                item.put("title", title);
                item.put("enclosure", enclosure);
                item.put("time", time);
                array.add(item);
            }
            json.put("items", array);
            System.out.println(rootElement);
        } catch (Exception e) {
            json.put("status", e.getMessage());
        }
    }
}
