Skip to content

Commit

Permalink
Make it really old Android compatible - again
Browse files Browse the repository at this point in the history
Base64 is not available on such Androids too
  • Loading branch information
gzsombor committed Jan 22, 2025
1 parent c106d24 commit 3be6a7b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 18 deletions.
2 changes: 2 additions & 0 deletions checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
<module name="AvoidStarImport"/>
<module name="IllegalImport"> <!-- defaults to sun.* packages -->
<property name="illegalClasses" value="
java.nio.charset.StandardCharsets,
java.util.Base64,
org.jetbrains.annotations.Nullable,
org.jetbrains.annotations.NotNull,
androidx.annotation.Nullable,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.schabi.newpipe.extractor.services.bandcamp.extractors;

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_API_URL;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
Expand All @@ -21,7 +22,6 @@
import org.schabi.newpipe.extractor.utils.JsonUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -107,7 +107,7 @@ private JsonObject fetchReviewsData(final String trackId, final String token)
.value("token", token)
.value("count", 7)
.array("exclude_fan_ids").end()
.end().done().getBytes(StandardCharsets.UTF_8)).responseBody());
.end().done().getBytes(UTF_8)).responseBody());
} catch (final IOException | ReCaptchaException e) {
throw new ParsingException("Could not fetch reviews", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.schabi.newpipe.extractor.utils.Utils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -31,6 +30,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.Image.HEIGHT_UNKNOWN;
import static org.schabi.newpipe.extractor.Image.WIDTH_UNKNOWN;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
Expand Down Expand Up @@ -125,7 +125,7 @@ public static JsonObject getArtistDetails(final String id) throws ParsingExcepti
.value("band_id", id)
.end()
.done()
.getBytes(StandardCharsets.UTF_8)).responseBody());
.getBytes(UTF_8)).responseBody());
} catch (final IOException | ReCaptchaException | JsonParserException e) {
throw new ParsingException("Could not download band details", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;

import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_API_URL;

public class BandcampFeaturedExtractor extends KioskExtractor<PlaylistInfoItem> {
Expand All @@ -45,7 +45,7 @@ public void onFetchPage(@Nonnull final Downloader downloader)
json = JsonParser.object().from(getDownloader().postWithContentTypeJson(
FEATURED_API_URL,
Collections.emptyMap(),
"{\"platform\":\"\",\"version\":0}".getBytes(StandardCharsets.UTF_8))
"{\"platform\":\"\",\"version\":0}".getBytes(UTF_8))
.responseBody());
} catch (final JsonParserException e) {
throw new ParsingException("Could not parse Bandcamp featured API response", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
import org.schabi.newpipe.extractor.utils.Utils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.io.UnsupportedEncodingException;

import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -130,11 +131,11 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page)
}
} else {
try {
json = JsonParser.object().from(new String(page.getBody(), StandardCharsets.UTF_8));
json = JsonParser.object().from(new String(page.getBody(), UTF_8));
isReply = true;
total = json.getArray(CHILDREN).size();
collectRepliesFrom(collector, json);
} catch (final JsonParserException e) {
} catch (final JsonParserException | UnsupportedEncodingException e) {
throw new ParsingException(
"Could not parse json data for nested comments info", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.nio.charset.StandardCharsets;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Objects;

import static org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeCommentsExtractor.CHILDREN;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.getAvatarsFromOwnerAccountOrVideoChannelObject;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.parseDateFrom;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;

public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
@Nonnull
Expand Down Expand Up @@ -130,8 +131,13 @@ public Page getReplies() throws ParsingException {
// is also structured like a JsonObject.
final JsonObject pageContent = new JsonObject();
pageContent.put(CHILDREN, children);
return new Page(repliesUrl, threadId,
JsonWriter.string(pageContent).getBytes(StandardCharsets.UTF_8));
try {
return new Page(repliesUrl, threadId,
JsonWriter.string(pageContent).getBytes(UTF_8));
} catch (final UnsupportedEncodingException e) {
throw new ParsingException(
"Could not parse data", e);
}
}
return new Page(repliesUrl, threadId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.io.UnsupportedEncodingException;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;

public class ProtoBuilder {
ByteArrayOutputStream byteBuffer;
Expand All @@ -18,8 +18,12 @@ public byte[] toBytes() {
}

public String toUrlencodedBase64() {
final String b64 = Base64.getUrlEncoder().encodeToString(toBytes());
return URLEncoder.encode(b64, StandardCharsets.UTF_8);
try {
final String b64 = encodeUrl(toBytes());
return URLEncoder.encode(b64, UTF_8);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

private void writeVarint(final long val) {
Expand Down Expand Up @@ -56,8 +60,12 @@ public void varint(final int field, final long val) {
}

public void string(final int field, final String string) {
final byte[] strBts = string.getBytes(StandardCharsets.UTF_8);
bytes(field, strBts);
try {
final byte[] strBts = string.getBytes(UTF_8);
bytes(field, strBts);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

public void bytes(final int field, final byte[] bytes) {
Expand All @@ -69,4 +77,59 @@ public void bytes(final int field, final byte[] bytes) {
throw new RuntimeException(e);
}
}

private static final byte[] MAP = new byte[] {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '+', '/'
};

private static final byte[] URL_MAP = new byte[] {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '-', '_'
};

private static String encode(final byte[] in) {
return encode(in, MAP);
}

private static String encodeUrl(final byte[] in) {
return encode(in, URL_MAP);
}

private static String encode(final byte[] in, final byte[] map) {
final int length = (in.length + 2) / 3 * 4;
final byte[] out = new byte[length];
int index = 0;
final int end = in.length - in.length % 3;
for (int i = 0; i < end; i += 3) {
out[index++] = map[(in[i] & 0xff) >> 2];
out[index++] = map[((in[i] & 0x03) << 4) | ((in[i + 1] & 0xff) >> 4)];
out[index++] = map[((in[i + 1] & 0x0f) << 2) | ((in[i + 2] & 0xff) >> 6)];
out[index++] = map[(in[i + 2] & 0x3f)];
}
switch (in.length % 3) {
case 1:
out[index++] = map[(in[end] & 0xff) >> 2];
out[index++] = map[(in[end] & 0x03) << 4];
out[index++] = '=';
out[index++] = '=';
break;
case 2:
out[index++] = map[(in[end] & 0xff) >> 2];
out[index++] = map[((in[end] & 0x03) << 4) | ((in[end + 1] & 0xff) >> 4)];
out[index++] = map[((in[end + 1] & 0x0f) << 2)];
out[index++] = '=';
break;
}
try {
return new String(out, "US-ASCII");
} catch (final UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit 3be6a7b

Please sign in to comment.