Skip to content

Commit c98695f

Browse files
committed
[SoundCloud] Fix extraction of non-JPG images
Default image qualities were removed in image URLs with the jpg extension, causing the addition of the image suffix to full non-JPG images URLs and so to invalid image URLs. Only the image quality name with its leading "-" character and the "." character after the name is now removed and replaced by a string format replaced itself with the image quality name for each quality. As the image suffixes do not contain the image extension, the name of image qualities lists has been adapted with these changes and some related comments have been also improved.
1 parent ac00459 commit c98695f

1 file changed

Lines changed: 29 additions & 28 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public final class SoundcloudParsingHelper {
5252
// and researches on images used by the websites
5353
// CHECKSTYLE:ON
5454
/*
55-
SoundCloud avatars and artworks are almost squares
55+
SoundCloud avatars and artworks are almost always squares.
5656
5757
When we get non-square pictures, all these images variants are still squares, except the
5858
original and the crop versions provides images which are respecting aspect ratios.
@@ -62,28 +62,28 @@ public final class SoundcloudParsingHelper {
6262
uploaded with a lower size than these variants: in this case, these variants return an upscaled
6363
version of the original image.
6464
*/
65-
private static final List<ImageSuffix> ALBUMS_AND_ARTWORKS_URL_SUFFIXES_AND_RESOLUTIONS =
66-
List.of(new ImageSuffix("mini.jpg", 16, 16, LOW),
67-
new ImageSuffix("t20x20.jpg", 20, 20, LOW),
68-
new ImageSuffix("small.jpg", 32, 32, LOW),
69-
new ImageSuffix("badge.jpg", 47, 47, LOW),
70-
new ImageSuffix("t50x50.jpg", 50, 50, LOW),
71-
new ImageSuffix("t60x60.jpg", 60, 60, LOW),
65+
private static final List<ImageSuffix> ALBUMS_AND_ARTWORKS_IMAGE_SUFFIXES =
66+
List.of(new ImageSuffix("mini", 16, 16, LOW),
67+
new ImageSuffix("t20x20", 20, 20, LOW),
68+
new ImageSuffix("small", 32, 32, LOW),
69+
new ImageSuffix("badge", 47, 47, LOW),
70+
new ImageSuffix("t50x50", 50, 50, LOW),
71+
new ImageSuffix("t60x60", 60, 60, LOW),
7272
// Seems to work also on avatars, even if it is written to be not the case in
7373
// the old API docs
74-
new ImageSuffix("t67x67.jpg", 67, 67, LOW),
75-
new ImageSuffix("t80x80.jpg", 80, 80, LOW),
76-
new ImageSuffix("large.jpg", 100, 100, LOW),
77-
new ImageSuffix("t120x120.jpg", 120, 120, LOW),
78-
new ImageSuffix("t200x200.jpg", 200, 200, MEDIUM),
79-
new ImageSuffix("t240x240.jpg", 240, 240, MEDIUM),
80-
new ImageSuffix("t250x250.jpg", 250, 250, MEDIUM),
81-
new ImageSuffix("t300x300.jpg", 300, 300, MEDIUM),
82-
new ImageSuffix("t500x500.jpg", 500, 500, MEDIUM));
83-
84-
private static final List<ImageSuffix> VISUALS_URL_SUFFIXES_AND_RESOLUTIONS =
85-
List.of(new ImageSuffix("t1240x260.jpg", 1240, 260, MEDIUM),
86-
new ImageSuffix("t2480x520.jpg", 2480, 520, MEDIUM));
74+
new ImageSuffix("t67x67", 67, 67, LOW),
75+
new ImageSuffix("t80x80", 80, 80, LOW),
76+
new ImageSuffix("large", 100, 100, LOW),
77+
new ImageSuffix("t120x120", 120, 120, LOW),
78+
new ImageSuffix("t200x200", 200, 200, MEDIUM),
79+
new ImageSuffix("t240x240", 240, 240, MEDIUM),
80+
new ImageSuffix("t250x250", 250, 250, MEDIUM),
81+
new ImageSuffix("t300x300", 300, 300, MEDIUM),
82+
new ImageSuffix("t500x500", 500, 500, MEDIUM));
83+
84+
private static final List<ImageSuffix> VISUALS_IMAGE_SUFFIXES =
85+
List.of(new ImageSuffix("t1240x260", 1240, 260, MEDIUM),
86+
new ImageSuffix("t2480x520", 2480, 520, MEDIUM));
8787

8888
private static String clientId;
8989
public static final String SOUNDCLOUD_API_V2_URL = "https://api-v2.soundcloud.com/";
@@ -435,9 +435,9 @@ public static List<Image> getAllImagesFromArtworkOrAvatarUrl(
435435

436436
return getAllImagesFromImageUrlReturned(
437437
// Artwork and avatars are originally returned with the "large" resolution, which
438-
// is 100x100
439-
originalArtworkOrAvatarUrl.replace("large.jpg", ""),
440-
ALBUMS_AND_ARTWORKS_URL_SUFFIXES_AND_RESOLUTIONS);
438+
// is 100px wide
439+
originalArtworkOrAvatarUrl.replace("-large.", "-%s."),
440+
ALBUMS_AND_ARTWORKS_IMAGE_SUFFIXES);
441441
}
442442

443443
@Nonnull
@@ -450,15 +450,16 @@ public static List<Image> getAllImagesFromVisualUrl(
450450
return getAllImagesFromImageUrlReturned(
451451
// Images are originally returned with the "original" resolution, which may be
452452
// huge so don't include it for size purposes
453-
originalVisualUrl.replace("original.jpg", ""),
454-
VISUALS_URL_SUFFIXES_AND_RESOLUTIONS);
453+
originalVisualUrl.replace("-original.", "-%s."),
454+
VISUALS_IMAGE_SUFFIXES);
455455
}
456456

457457
private static List<Image> getAllImagesFromImageUrlReturned(
458-
@Nonnull final String baseImageUrl,
458+
@Nonnull final String baseImageUrlFormat,
459459
@Nonnull final List<ImageSuffix> imageSuffixes) {
460460
return imageSuffixes.stream()
461-
.map(imageSuffix -> new Image(baseImageUrl + imageSuffix.getSuffix(),
461+
.map(imageSuffix -> new Image(
462+
String.format(baseImageUrlFormat, imageSuffix.getSuffix()),
462463
imageSuffix.getHeight(), imageSuffix.getWidth(),
463464
imageSuffix.getResolutionLevel()))
464465
.collect(Collectors.toUnmodifiableList());

0 commit comments

Comments
 (0)