Browse Source

post shortcode converter

renovate/org.robolectric-robolectric-4.x
Austin Huang 4 years ago
parent
commit
efb5818a65
No known key found for this signature in database GPG Key ID: 84C23AA04587A91F
  1. 13
      app/src/main/java/awais/instagrabber/utils/TextUtils.java
  2. 11
      app/src/test/java/awais/instagrabber/utils/TextUtilsTest.java

13
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;
}
}

11
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);
}
}
Loading…
Cancel
Save