From efb5818a65bea72866bb7b2c1491ace04d668091 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 18 May 2021 22:11:16 -0400 Subject: [PATCH] post shortcode converter --- .../java/awais/instagrabber/utils/TextUtils.java | 13 +++++++++++++ .../awais/instagrabber/utils/TextUtilsTest.java | 11 ++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/awais/instagrabber/utils/TextUtils.java b/app/src/main/java/awais/instagrabber/utils/TextUtils.java index 448c2a90..5df3ac08 100644 --- a/app/src/main/java/awais/instagrabber/utils/TextUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/TextUtils.java @@ -10,11 +10,13 @@ import android.util.Patterns; import androidx.annotation.NonNull; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.regex.Matcher; +import java.util.stream.Collectors; public final class TextUtils { // extracted from String class @@ -106,4 +108,15 @@ public final class TextUtils { } return urls; } + + // https://github.com/notslang/instagram-id-to-url-segment + public static long shortcodeToId(final String shortcode) { + long result = 0L; + for (int i = 0; i < shortcode.length() && i < 11; i++){ + final char c = shortcode.charAt(i); + final int k = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".indexOf(c); + result = result * 64 + k; + } + return result; + } } diff --git a/app/src/test/java/awais/instagrabber/utils/TextUtilsTest.java b/app/src/test/java/awais/instagrabber/utils/TextUtilsTest.java index cf69eb07..00d3f71d 100644 --- a/app/src/test/java/awais/instagrabber/utils/TextUtilsTest.java +++ b/app/src/test/java/awais/instagrabber/utils/TextUtilsTest.java @@ -1,10 +1,11 @@ package awais.instagrabber.utils; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; class TextUtilsTest { - @org.junit.jupiter.api.Test + @Test void testMillisToTimeString() { String timeString = TextUtils.millisToTimeString(18000000); Assertions.assertEquals("05:00:00", timeString); @@ -15,4 +16,12 @@ class TextUtilsTest { timeString = TextUtils.millisToTimeString(300000, true); Assertions.assertEquals("00:05:00", timeString); } + + @Test + void testShortcodeConversion() { + long conversion = TextUtils.shortcodeToId("CA0YnOonSfS"); + Assertions.assertEquals(2320587956892280786L, conversion); + conversion = TextUtils.shortcodeToId("B_7n8mblwx6gv1ZaNvA5ZhAs2qslMnRiMMYW1c0"); + Assertions.assertEquals(2304611322577751162L, conversion); + } } \ No newline at end of file