Fix Jsoup 403 error (fixes #71)

This commit is contained in:
Shinokuni 2020-07-13 22:03:43 +02:00
parent 442c257593
commit 0122400c85
2 changed files with 23 additions and 13 deletions

View File

@ -5,9 +5,9 @@ import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.readropslibrary.utils.HttpManager;
import com.readrops.readropslibrary.utils.LibUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
@ -18,6 +18,9 @@ import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import okhttp3.Request;
import okhttp3.Response;
public final class HtmlParser {
private static final String TAG = HtmlParser.class.getSimpleName();
@ -81,7 +84,7 @@ public final class HtmlParser {
}
@Nullable
public static String getFaviconLink(@NonNull String url) throws IOException {
public static String getFaviconLink(@NonNull String url) {
String favUrl = null;
String head = getHTMLHeadFromUrl(url);
@ -102,20 +105,28 @@ public final class HtmlParser {
}
@Nullable
private static String getHTMLHeadFromUrl(@NonNull String url) throws IOException {
private static String getHTMLHeadFromUrl(@NonNull String url) {
long start = System.currentTimeMillis();
Connection.Response response = Jsoup.connect(url).execute();
if (response.contentType().contains(LibUtils.HTML_CONTENT_TYPE)) {
String body = response.body();
String head = body.substring(body.indexOf("<head"), body.indexOf("</head>"));
try {
Response response = HttpManager.getInstance().getOkHttpClient()
.newCall(new Request.Builder().url(url).build()).execute();
long end = System.currentTimeMillis();
Log.d(TAG, "parsing time : " + (end - start));
if (response.header("Content-Type").contains(LibUtils.HTML_CONTENT_TYPE)) {
String body = response.body().string();
String head = body.substring(body.indexOf("<head"), body.indexOf("</head>"));
return head;
} else
long end = System.currentTimeMillis();
Log.d(TAG, "parsing time : " + (end - start));
return head;
} else {
return null;
}
} catch (Exception e) {
return null;
}
}
public static String getDescImageLink(String description, String url) {

View File

@ -6,7 +6,6 @@ import com.readrops.app.utils.ParsingResult;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -28,7 +27,7 @@ public class HtmlParserTest {
}
@Test
public void getFaviconLinkTest() throws IOException {
public void getFaviconLinkTest() {
String url = "https://github.com/readrops/Readrops";
assertEquals("https://github.com/fluidicon.png", HtmlParser.getFaviconLink(url));