1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2025-01-01 20:54:34 +00:00

Fetch Open Graph data

This commit is contained in:
M66B 2022-06-04 08:02:21 +02:00
parent c1e2a71ea2
commit 376e6e352a

View file

@ -108,7 +108,7 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("url", etLink.getText().toString()); args.putString("url", etLink.getText().toString());
new SimpleTask<String>() { new SimpleTask<OpenGraph>() {
@Override @Override
protected void onPreExecute(Bundle args) { protected void onPreExecute(Bundle args) {
btnMetadata.setEnabled(false); btnMetadata.setEnabled(false);
@ -122,10 +122,12 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
} }
@Override @Override
protected String onExecute(Context context, Bundle args) throws Throwable { protected OpenGraph onExecute(Context context, Bundle args) throws Throwable {
String url = args.getString("url"); String url = args.getString("url");
URL base = new URL(url); URL base = new URL(url);
OpenGraph og = new OpenGraph();
HttpURLConnection connection = (HttpURLConnection) base.openConnection(); HttpURLConnection connection = (HttpURLConnection) base.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setReadTimeout(METADATA_READ_TIMEOUT); connection.setReadTimeout(METADATA_READ_TIMEOUT);
@ -150,38 +152,65 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
// <title>... // <title>...
// <meta name="description" content="... // <meta name="description" content="...
// <meta property="og:title" content="... // https://ogp.me/
// <meta property="twitter:title" content="...
Document doc = JsoupEx.parse(connection.getInputStream(), StandardCharsets.UTF_8.name(), url); Document doc = JsoupEx.parse(connection.getInputStream(), StandardCharsets.UTF_8.name(), url);
Element title = doc.select("title").first();
if (title != null && !TextUtils.isEmpty(title.text()))
return title.text();
Element ogTitle = doc.select("meta[property=og:title]").first(); Element ogTitle = doc.select("meta[property=og:title]").first();
if (ogTitle != null && !TextUtils.isEmpty(ogTitle.attr("content"))) if (ogTitle != null)
return ogTitle.attr("content"); og.title = ogTitle.attr("content");
Element twitterTitle = doc.select("meta[property=twitter:title]").first(); // Fallback
if (twitterTitle != null && !TextUtils.isEmpty(twitterTitle.attr("content"))) if (TextUtils.isEmpty(og.title)) {
return twitterTitle.attr("content"); Element title = doc.select("title").first();
if (title != null)
og.title = title.text();
}
Element ogDescription = doc.select("meta[property=og:description]").first();
if (ogDescription != null)
og.description = ogDescription.attr("content");
// Fallback
if (TextUtils.isEmpty(og.description)) {
Element description = doc.select("meta[name=description]").first();
if (description != null)
og.description = description.attr("content");
}
Element ogSiteName = doc.select("meta[property=og:site_name]").first(); Element ogSiteName = doc.select("meta[property=og:site_name]").first();
if (ogSiteName != null && !TextUtils.isEmpty(ogSiteName.attr("content"))) if (ogSiteName != null)
return ogSiteName.attr("content"); og.site_name = ogSiteName.attr("content");
Element description = doc.select("meta[name=description]").first(); Element ogImage = doc.select("meta[property=og:image]").first();
if (description != null && !TextUtils.isEmpty(description.attr("content"))) if (ogImage != null)
return description.attr("content"); og.image = ogImage.attr("content");
return null; Element ogUrl = doc.select("meta[property=og:url]").first();
if (ogUrl != null)
og.url = ogUrl.attr("content");
return og;
} finally { } finally {
connection.disconnect(); connection.disconnect();
} }
} }
@Override @Override
protected void onExecuted(Bundle args, String text) { protected void onExecuted(Bundle args, OpenGraph og) {
if (og == null)
return;
// Canonical URL
if (!TextUtils.isEmpty(og.url))
etLink.setText(og.url);
// Link title
String text = og.title;
if (TextUtils.isEmpty(text))
text = og.description;
if (TextUtils.isEmpty(text))
text = og.site_name;
if (TextUtils.isEmpty(text)) if (TextUtils.isEmpty(text))
etTitle.setText(null); etTitle.setText(null);
else else
@ -227,6 +256,14 @@ public class FragmentDialogInsertLink extends FragmentDialogBase {
.create(); .create();
} }
private static class OpenGraph {
private String title;
private String description;
private String site_name;
private String image;
private String url;
}
static Bundle getArguments(EditText etBody) { static Bundle getArguments(EditText etBody) {
Uri uri = null; Uri uri = null;