get item image from open graph metadata (be careful, it's slow !)

This commit is contained in:
Shinokuni 2019-01-15 18:33:09 +00:00
parent 9490aa7f20
commit f66e221a5b
1 changed files with 35 additions and 0 deletions

View File

@ -1,11 +1,17 @@
package com.readrops.readropslibrary;
import android.text.LoginFilter;
import android.util.Log;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public final class PageParser {
@ -38,4 +44,33 @@ public final class PageParser {
return type.equals("application/rss+xml") || type.equals("application/atom+xml") || type.equals("application/json");
}
public static String getOGImageLink(String url) {
String imageUrl = null;
try {
long start = System.currentTimeMillis();
Connection.Response response = Jsoup.connect(url).execute();
String body = response.body();
String head = body.substring(body.indexOf("<head>"), body.indexOf("</head>"));
Document document = Jsoup.parse(head);
Element element = document.select("meta[property=og:image]").first();
if (element != null)
imageUrl = element.attributes().get("content");
long end = System.currentTimeMillis();
Log.d("temps de parsing", String.valueOf(end - start));
return imageUrl;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}