From 3d421629d9d92f2920842e028163121ef797b531 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sun, 30 Jul 2023 23:44:46 +0200 Subject: [PATCH] Fix twitter and pixiv --- .gitea/workflows/apk.yml | 2 +- .../sharewithtitle/HttpThreadHandler.java | 22 +++++++++++++++++- .../sharewithtitle/MainActivity.java | 23 +++++++++++++++---- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/.gitea/workflows/apk.yml b/.gitea/workflows/apk.yml index c459da3..c94bc25 100644 --- a/.gitea/workflows/apk.yml +++ b/.gitea/workflows/apk.yml @@ -11,7 +11,7 @@ jobs: go-version: '>=1.20.1' - uses: actions/checkout@v3 - run: curl -sSL -u "$USERNAME:$PASSWORD" -o ShareWithTitle/$STORE_FILE https://cloud.crystalyx.net/remote.php/dav/files/$USERNAME/Bordel/$STORE_FILE - - run: sh gradlew build bundleRelease assembleRelease --no-daemon + - run: sh gradlew lint build bundle assemble --no-daemon - uses: https://gitea.com/actions/release-action@main if: startsWith(gitea.ref, 'refs/tags') with: diff --git a/ShareWithTitle/src/main/java/net/crystalyx/sharewithtitle/HttpThreadHandler.java b/ShareWithTitle/src/main/java/net/crystalyx/sharewithtitle/HttpThreadHandler.java index c110b2e..42eb980 100644 --- a/ShareWithTitle/src/main/java/net/crystalyx/sharewithtitle/HttpThreadHandler.java +++ b/ShareWithTitle/src/main/java/net/crystalyx/sharewithtitle/HttpThreadHandler.java @@ -1,6 +1,8 @@ package net.crystalyx.sharewithtitle; import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; import java.io.IOException; import java.net.URL; @@ -17,7 +19,25 @@ public class HttpThreadHandler implements Runnable { @Override public void run() { try { - title = Jsoup.connect(url.toString()).get().title(); + Document doc = Jsoup.connect(url.toString()).get(); + String titleMeta = doc.title(); + Element descriptionMeta = doc.select("meta[name=description]").first(); + Element titleElement = doc.select("meta[property=og:title]").first(); + Element descriptionElement = doc.select("meta[property=og:description]").first(); + + if (titleElement != null) { + title = titleElement.attr("content"); + if (descriptionElement != null) { + title += "\n" + descriptionElement.attr("content"); + } else if (descriptionMeta != null) { + title += "\n" + descriptionMeta.attr("content"); + } + } else { + title = titleMeta; + if (descriptionMeta != null) { + title += "\n" + descriptionMeta.attr("content"); + } + } } catch (IOException e) { errorMessage = e.getMessage(); } diff --git a/ShareWithTitle/src/main/java/net/crystalyx/sharewithtitle/MainActivity.java b/ShareWithTitle/src/main/java/net/crystalyx/sharewithtitle/MainActivity.java index 71ea03d..c49fbf3 100644 --- a/ShareWithTitle/src/main/java/net/crystalyx/sharewithtitle/MainActivity.java +++ b/ShareWithTitle/src/main/java/net/crystalyx/sharewithtitle/MainActivity.java @@ -25,16 +25,31 @@ public class MainActivity extends Activity { } private void handleUrl(String textUrl) { - URL url; + URL originalUrl; + URL fixedUrl; try { - url = new URL(textUrl); + originalUrl = new URL(textUrl); + + switch (originalUrl.getHost()) { + case "twitter.com": + case "t.co": + case "x.com": + fixedUrl = new URL(originalUrl.getProtocol(), "fxtwitter.com", originalUrl.getPort(), originalUrl.getFile()); + break; + case "pixiv.net": + fixedUrl = new URL(originalUrl.getProtocol(), "ppxiv.net", originalUrl.getPort(), originalUrl.getFile()); + break; + default: + fixedUrl = originalUrl; + break; + } } catch (MalformedURLException e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); return; } - HttpThreadHandler handler = new HttpThreadHandler(url); + HttpThreadHandler handler = new HttpThreadHandler(fixedUrl); Thread job = new Thread(handler); job.start(); @@ -50,7 +65,7 @@ public class MainActivity extends Activity { if (title != null) { Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setType("text/plain"); - sharingIntent.putExtra(Intent.EXTRA_TEXT, title + "\n\n" + url); + sharingIntent.putExtra(Intent.EXTRA_TEXT, title + "\n\n" + originalUrl); startActivity(Intent.createChooser(sharingIntent, "Share via")); } else if (errorMessage != null) { Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();