替换rss解析器为xml解析
This commit is contained in:
parent
a366e15765
commit
dcb56e8873
5
pom.xml
5
pom.xml
@ -137,6 +137,11 @@
|
||||
<artifactId>snail</artifactId>
|
||||
<version>1.17.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dom4j</groupId>
|
||||
<artifactId>dom4j</artifactId>
|
||||
<version>2.1.4</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -12,7 +12,7 @@ import org.springframework.context.annotation.Import;
|
||||
@Import(DmhyRssDownloadManager.class)
|
||||
@SpringBootApplication
|
||||
public class NasApplication {
|
||||
public static final String version = "1.3";
|
||||
public static final String version = "1.4";
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(NasApplication.class, args);
|
||||
|
@ -4,10 +4,7 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import com.yutou.nas.interfaces.DownloadInterface;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@ -17,7 +14,11 @@ public class HttpTools {
|
||||
private static final int HttpRequestIndex = 3;
|
||||
|
||||
public static String get(String url) {
|
||||
return https_get(url, null);
|
||||
return https_get(url, false, null);
|
||||
}
|
||||
|
||||
public static String https_get(String url, Map<String, String> header) {
|
||||
return https_get(url, false, header);
|
||||
}
|
||||
|
||||
public static String post(final String url, final byte[] body) {
|
||||
@ -28,16 +29,26 @@ public class HttpTools {
|
||||
return new HttpTools().http_syncDownload(url, saveName);
|
||||
}
|
||||
|
||||
public static String https_get(String url, Map<String, String> header) {
|
||||
public static String https_get(String url, boolean isProxy, Map<String, String> header) {
|
||||
try {
|
||||
URLConnection connection;
|
||||
Proxy proxy = null;
|
||||
if (isProxy) {
|
||||
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ConfigTools.load(ConfigTools.CONFIG,"proxy.address",String.class),
|
||||
ConfigTools.load(ConfigTools.CONFIG,"proxy.port",Integer.class)));
|
||||
}
|
||||
if (proxy == null) {
|
||||
connection = new URL(url).openConnection();
|
||||
} else {
|
||||
connection = new URL(url).openConnection(proxy);
|
||||
}
|
||||
connection.setRequestProperty("User-Agent", getUa());
|
||||
if (header != null) {
|
||||
for (String key : header.keySet()) {
|
||||
connection.addRequestProperty(key, header.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
connection.connect();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
StringBuilder str = new StringBuilder();
|
||||
@ -101,6 +112,7 @@ public class HttpTools {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String http_post_form(String url, byte[] body, int index, Map<String, String> headers) {
|
||||
String tmp;
|
||||
StringBuilder str = new StringBuilder();
|
||||
|
@ -1,23 +1,79 @@
|
||||
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 url = "https://api.rss2json.com/v1/api.json?rss_url=%s&api_key=wtfm5pebya13pnl8rtu51wfgfpte0mb9sap1foll&count=500";
|
||||
String xml = HttpTools.https_get(xmlUrl, true, null);
|
||||
SAXReader reader = new SAXReader();
|
||||
JSONObject json = new JSONObject();
|
||||
try {
|
||||
String ret = HttpTools.get(String.format(url, URLEncoder.encode(xmlUrl, "UTF-8")));
|
||||
if(ret!=null){
|
||||
return JSON.parseObject(ret, JSONObject.class);
|
||||
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);
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
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());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user