Skip to content

Commit

Permalink
- Added a fix for invalid WP image URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Eukon05 committed Nov 10, 2024
1 parent ed5706e commit 5f20735
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class WpAdapter {

private static final String ARTICLE_DETAILS_QUERY = """
{
"query": "query Collections { collections(productId: \\"5973184000386177\\") { article(id: \\"%s\\") { created tags { slug } } }}"
"query": "query Collections { collections(productId: \\"5973184000386177\\") { article(id: \\"%s\\") { created tags { slug } image { height width } } }}"
}""";

private static final Gson GSON = new Gson();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
import java.util.List;

final class WpArticleMapper {
private static final String IMAGE_URL_TEMPLATE = "https://i.wpimg.pl/O/%dx%d/%s";
private static final String IMAGE = "image";

private WpArticleMapper() {
}

static Article mapFromJson(JsonObject articleJson, JsonObject articleDetailsJson, ArticleSourceInfo sourceInfo) {
String title = articleJson.get("title").getAsString();
String id = articleJson.get("contentId").getAsString();
String url = articleJson.get("url").getAsString();
String imageUrl = articleJson.get("image").getAsString();
String imageUrl = articleJson.get(IMAGE).getAsString().replace("https://", "");

int imageHeight = articleDetailsJson.getAsJsonObject(IMAGE).get("height").getAsInt();
int imageWidth = articleDetailsJson.getAsJsonObject(IMAGE).get("width").getAsInt();

String finalImageUrl = String.format(IMAGE_URL_TEMPLATE, imageWidth, imageHeight, imageUrl);

long createdAt = articleDetailsJson.get("created").getAsLong();
List<String> tags = articleDetailsJson.getAsJsonArray("tags")
Expand All @@ -27,6 +35,6 @@ static Article mapFromJson(JsonObject articleJson, JsonObject articleDetailsJson
.map(JsonElement::getAsString)
.toList();

return new Article(id, sourceInfo.name(), title, url, imageUrl, Instant.ofEpochSecond(createdAt), tags);
return new Article(id, sourceInfo.name(), title, url, finalImageUrl, Instant.ofEpochSecond(createdAt), tags);
}
}

0 comments on commit 5f20735

Please sign in to comment.