From 376e6e352ac965da0d8c68c3e44d60e0c526f6d4 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 4 Jun 2022 08:02:21 +0200 Subject: [PATCH] Fetch Open Graph data --- .../email/FragmentDialogInsertLink.java | 77 ++++++++++++++----- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogInsertLink.java b/app/src/main/java/eu/faircode/email/FragmentDialogInsertLink.java index 5730034876..51dab47792 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogInsertLink.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogInsertLink.java @@ -108,7 +108,7 @@ public class FragmentDialogInsertLink extends FragmentDialogBase { Bundle args = new Bundle(); args.putString("url", etLink.getText().toString()); - new SimpleTask() { + new SimpleTask() { @Override protected void onPreExecute(Bundle args) { btnMetadata.setEnabled(false); @@ -122,10 +122,12 @@ public class FragmentDialogInsertLink extends FragmentDialogBase { } @Override - protected String onExecute(Context context, Bundle args) throws Throwable { + protected OpenGraph onExecute(Context context, Bundle args) throws Throwable { String url = args.getString("url"); URL base = new URL(url); + OpenGraph og = new OpenGraph(); + HttpURLConnection connection = (HttpURLConnection) base.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(METADATA_READ_TIMEOUT); @@ -150,38 +152,65 @@ public class FragmentDialogInsertLink extends FragmentDialogBase { // ... // <meta name="description" content="... - // <meta property="og:title" content="... - // <meta property="twitter:title" content="... + // https://ogp.me/ 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(); - if (ogTitle != null && !TextUtils.isEmpty(ogTitle.attr("content"))) - return ogTitle.attr("content"); + if (ogTitle != null) + og.title = ogTitle.attr("content"); + + // Fallback + if (TextUtils.isEmpty(og.title)) { + Element title = doc.select("title").first(); + if (title != null) + og.title = title.text(); + } - Element twitterTitle = doc.select("meta[property=twitter:title]").first(); - if (twitterTitle != null && !TextUtils.isEmpty(twitterTitle.attr("content"))) - return twitterTitle.attr("content"); + 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(); - if (ogSiteName != null && !TextUtils.isEmpty(ogSiteName.attr("content"))) - return ogSiteName.attr("content"); + if (ogSiteName != null) + og.site_name = ogSiteName.attr("content"); + + Element ogImage = doc.select("meta[property=og:image]").first(); + if (ogImage != null) + og.image = ogImage.attr("content"); - Element description = doc.select("meta[name=description]").first(); - if (description != null && !TextUtils.isEmpty(description.attr("content"))) - return description.attr("content"); + Element ogUrl = doc.select("meta[property=og:url]").first(); + if (ogUrl != null) + og.url = ogUrl.attr("content"); - return null; + return og; } finally { connection.disconnect(); } } @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)) etTitle.setText(null); else @@ -227,6 +256,14 @@ public class FragmentDialogInsertLink extends FragmentDialogBase { .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) { Uri uri = null;