Removes JSoup lib

This commit is contained in:
stom79 2017-12-10 18:37:58 +01:00
parent 75e5cea714
commit 451fa6ad3f
3 changed files with 79 additions and 22 deletions

View File

@ -40,7 +40,6 @@ dependencies {
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
implementation 'com.evernote:android-job:1.2.1'
implementation 'com.github.chrisbanes:PhotoView:2.0.0'
implementation 'org.jsoup:jsoup:1.10.3'
implementation 'com.github.stom79:country-picker-android:1.2.0'
implementation 'com.github.stom79:mytransl:1.2'
playstoreImplementation 'io.github.kobakei:ratethisapp:1.2.0'

View File

@ -18,12 +18,13 @@ import android.os.AsyncTask;
import android.os.Build;
import android.util.Patterns;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import fr.gouv.etalab.mastodon.client.HttpsConnection;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMetaDataInterface;
@ -47,11 +48,9 @@ public class RetrieveMetaDataAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";
String potentialUrl = "";
try {
Matcher matcher;
if (url.startsWith("www."))
url = "http://" + url;
@ -67,22 +66,27 @@ public class RetrieveMetaDataAsyncTask extends AsyncTask<Void, Void, Void> {
}
// If we actually have a URL then make use of it.
if (potentialUrl.length() > 0) {
Document document = Jsoup.connect(potentialUrl).userAgent(userAgent).get();
Elements metaOgTitle = document.select("meta[property=og:title]");
if (metaOgTitle != null) {
title = metaOgTitle.attr("content");
} else {
title = document.title();
}
Elements metaOgDescription = document.select("meta[property=og:description]");
if (metaOgDescription != null) {
description = metaOgDescription.attr("content");
} else {
description = "";
}
Elements metaOgImage = document.select("meta[property=og:image]");
if (metaOgImage != null) {
image = metaOgImage.attr("content");
Pattern titlePattern = Pattern.compile("meta\\s+property=[\"']og:title[\"']\\s+content=[\"'](.*)[\"']");
Pattern descriptionPattern = Pattern.compile("meta\\s+property=[\"']og:description[\"']\\s+content=[\"'](.*)[\"']");
Pattern imagePattern = Pattern.compile("meta\\s+property=[\"']og:image[\"']\\s+content=[\"'](.*)[\"']");
try {
String response = new HttpsConnection().get(potentialUrl);
Matcher matcherTitle = titlePattern.matcher(response);
Matcher matcherDescription = descriptionPattern.matcher(response);
Matcher matcherImage = imagePattern.matcher(response);
while (matcherTitle.find())
title = matcherTitle.group(1);
while (matcherDescription.find())
description = matcherDescription.group(1);
while (matcherImage.find())
image = matcherImage.group(1);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
}
}
} catch (IOException | IndexOutOfBoundsException e) {

View File

@ -74,6 +74,7 @@ public class HttpsConnection {
this.context = context;
}
@SuppressWarnings("ConstantConditions")
public String get(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
@ -125,6 +126,59 @@ public class HttpsConnection {
public String get(String urlConnection) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
HttpsURLConnection httpsURLConnection;
HttpURLConnection httpURLConnection;
StringBuilder sb = new StringBuilder();
if( urlConnection.startsWith("https://")) {
URL url = new URL(urlConnection);
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setConnectTimeout(30 * 1000);
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setRequestMethod("GET");
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
Reader in = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0; )
sb.append((char) c);
httpsURLConnection.disconnect();
in.close();
return sb.toString();
} else {
Reader in = new BufferedReader(new InputStreamReader(httpsURLConnection.getErrorStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0; )
sb.append((char) c);
httpsURLConnection.disconnect();
throw new HttpsConnectionException(httpsURLConnection.getResponseCode(), sb.toString());
}
}else{
URL url = new URL(urlConnection);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(30 * 1000);
httpURLConnection.setRequestProperty("http.keepAlive", "false");
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
httpURLConnection.setRequestMethod("GET");
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
Reader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0; )
sb.append((char) c);
httpURLConnection.disconnect();
in.close();
return sb.toString();
} else {
Reader in = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0; )
sb.append((char) c);
httpURLConnection.disconnect();
throw new HttpsConnectionException(httpURLConnection.getResponseCode(), sb.toString());
}
}
}
public String post(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
URL url = new URL(urlConnection);
Map<String,Object> params = new LinkedHashMap<>();