Skip to content

Commit

Permalink
Clean up ru.vtosters.hooks.music unnecessary constructions suppyAsync…
Browse files Browse the repository at this point in the history
…(() -> { }).get()
  • Loading branch information
sunmisc committed Mar 28, 2024
1 parent 41763a4 commit d2aff9e
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import ru.vtosters.lite.music.cache.delegate.PlaylistCacheDbDelegate;
import ru.vtosters.lite.music.cache.helpers.PlaylistHelper;
import ru.vtosters.lite.music.callback.MusicCallbackBuilder;
import ru.vtosters.lite.music.converter.playlist.PlaylistConverter;
import ru.vtosters.lite.music.downloader.AudioGet;
import ru.vtosters.lite.music.requests.PlaylistGet;
import ru.vtosters.lite.music.requests.AudioGet;
import ru.vtosters.lite.music.downloader.PlaylistDownloader;
import ru.vtosters.lite.music.downloader.ThumbnailPlaylistDownloader;
import ru.vtosters.lite.music.downloader.TrackDownloader;
Expand All @@ -44,7 +44,7 @@ public class AudioDownloader {
public static final String dlpath = Preferences.getBoolValue("dldir", false) ? Environment.DIRECTORY_DOWNLOADS : Environment.DIRECTORY_MUSIC;

public static void downloadPlaylist(Playlist playlist) {
List<MusicTrack> tracks = PlaylistConverter.getPlaylist(playlist);
List<MusicTrack> tracks = PlaylistGet.getPlaylist(playlist);

String playlistName = IOUtils.getValidFileName(playlist.g);

Expand Down Expand Up @@ -83,15 +83,15 @@ public static void cacheTrack(MusicTrack track) {
}

public static void cachePlaylist(Playlist playlist) {
List<MusicTrack> tracks = PlaylistConverter.getPlaylist(playlist);
List<MusicTrack> tracks = PlaylistGet.getPlaylist(playlist);

int notificationId = playlist.g.hashCode();
NotificationCompat.Builder notification = MusicNotificationBuilder.buildPlaylistDownloadNotification(playlist.g, notificationId);

JSONObject thumbs = PlaylistUtils.getThumb(playlist);

if (thumbs != null) {
new ThumbnailPlaylistDownloader().download(null, new Callback() {
new ThumbnailPlaylistDownloader(new Callback() {
@Override
public void onProgress(int progress) {

Expand All @@ -108,12 +108,7 @@ public void onSuccess() {
public void onFailure(Throwable e) {
throw new RuntimeException(e);
}

@Override
public void onSizeReceived(long size, long header) {

}
}, playlist);
}).download(playlist);
} else {
PlaylistCacheDbDelegate.addPlaylist(AndroidUtils.getGlobalContext(), playlist);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
import com.vk.dto.music.Playlist;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import ru.vtosters.lite.music.cache.db.PlaylistCacheDb;
import ru.vtosters.lite.utils.music.MusicCacheStorageUtils;
import ru.vtosters.lite.utils.music.PlaylistUtils;

import java.util.List;

public class PlaylistCacheDbDelegate {
static synchronized private PlaylistCacheDb connectToDb(Context context) {
synchronized (PlaylistCacheDb.class) {
return new PlaylistCacheDb(context);
}
private static PlaylistCacheDb connectToDb(Context context) {
return new PlaylistCacheDb(context);
}

public static void addPlaylist(Context context, Playlist playlist) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public void onFailure(Throwable e) {
notification.setContentText(AndroidUtils.getString(R.string.load_audio_error)).setProgress(0, 0, false);
notificationManager.notify(notificationId, notification.build());
}

@Override
public void onSizeReceived(long size, long header) {

}
};
}

Expand Down Expand Up @@ -85,11 +80,6 @@ public void onFailure(Throwable e) {
notification.setContentText(AndroidUtils.getString(R.string.load_audio_error)).setProgress(0, 0, false);
notificationManager.notify(notificationId, notification.build());
}

@Override
public void onSizeReceived(long size, long header) {

}
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,55 @@
package ru.vtosters.lite.music.downloader;

import com.vk.dto.music.MusicTrack;

import com.vk.dto.music.Playlist;

import java.io.File;
import java.io.IOException;

import ru.vtosters.lite.music.cache.MusicCacheImpl;
import ru.vtosters.lite.music.cache.delegate.PlaylistCacheDbDelegate;
import ru.vtosters.lite.music.interfaces.Callback;
import ru.vtosters.lite.music.interfaces.ITrackDownloader;
import ru.vtosters.lite.music.interfaces.IDownloader;
import ru.vtosters.lite.utils.AndroidUtils;

public final class CachedDownloader implements ITrackDownloader {

private final ITrackDownloader origin;

public CachedDownloader(ITrackDownloader origin) {
this.origin = origin;
public final class CachedDownloader implements IDownloader<MusicTrack> {
private final File to;
private final Playlist playlist;
private final Callback callback;

public CachedDownloader(File to,
Playlist playlist,
Callback callback) {
this.to = to;
this.playlist = playlist;
this.callback = callback;
}

@Override
public void download(MusicTrack track, Callback callback, Playlist playlist) {
origin.download(track, new Callback() {
public void download(MusicTrack track) {
new Mp3Downloader(to, new Callback() {
@Override
public void onProgress(int progress) {
callback.onProgress(progress);
}

@Override
public void onSuccess() {
new ThumbnailTrackDownloader().download(track, new Callback() {
@Override public void onProgress(int progress) {

}

@Override public void onSuccess() {

}

@Override
public void onFailure(Throwable e) {
callback.onFailure(e);
}

@Override public void onSizeReceived(long size, long header) {

}
}, null);

PlaylistCacheDbDelegate.addTrackToPlaylist(AndroidUtils.getGlobalContext(), playlist.v1(), track.y1());
MusicCacheImpl.addTrack(track);
callback.onSuccess();
try {
new ThumbnailTrackDownloader().download(track);
PlaylistCacheDbDelegate.addTrackToPlaylist(AndroidUtils.getGlobalContext(), playlist.v1(), track.y1());
MusicCacheImpl.addTrack(track);
callback.onSuccess();
} catch (IOException e) {
callback.onFailure(e);
throw new RuntimeException(e);
}
}

@Override
public void onFailure(Throwable e) {
callback.onFailure(e);
}

@Override
public void onSizeReceived(long size, long header) {
callback.onSizeReceived(size, header);
}
}, playlist);
}).download(track);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.google.android.exoplayer2.source.hls.playlist.f;
import com.google.android.exoplayer2.source.hls.playlist.f.a;
import com.vk.dto.music.MusicTrack;
import com.vk.dto.music.Playlist;

import java.io.File;
import java.io.IOException;
Expand All @@ -25,41 +24,36 @@
import ru.vtosters.hooks.other.Preferences;
import ru.vtosters.lite.music.converter.ts.MpegDemuxer;
import ru.vtosters.lite.music.interfaces.Callback;
import ru.vtosters.lite.music.interfaces.ITrackDownloader;
import ru.vtosters.lite.music.interfaces.IDownloader;
import ru.vtosters.lite.utils.IOUtils;

public final class Mp3Downloader implements ITrackDownloader {
public final class Mp3Downloader implements IDownloader<MusicTrack> {
private final File outputFile;
private final Callback callback;

public Mp3Downloader(File outputFile) {
public Mp3Downloader(File outputFile, Callback callback) {
this.outputFile = outputFile;
this.callback = callback;
}

@Override
public void download(MusicTrack track, Callback callback, Playlist playlist) {
public void download(MusicTrack track) {
String uri = track.D;

if (Objects.requireNonNull(uri).isEmpty()) {
String msg = "link error: " + track.y1() + ", title: " + Mp3Downloader.getTitle(track);
Log.d("Mp3Downloader", msg);
throw new RuntimeException(msg);
}

if (Objects.requireNonNull(uri).contains("master.m3u8?siren=1")) {
try {
try {
if (uri.contains("master.m3u8?siren=1")) {
String content = IOUtils.readAllLines(new URL(uri).openStream());
String replacement = content.split("\n")[2].trim();
uri = uri.replace("master.m3u8?siren=1", replacement);
} catch (IOException e) {
callback.onFailure(e);
return;
}
}
var tsParser = new com.google.android.exoplayer2.source.hls.playlist.h(e.a(uri));
var baseUri = uri.substring(0, uri.lastIndexOf("/") + 1);

var tsParser = new com.google.android.exoplayer2.source.hls.playlist.h(e.a(uri));
var baseUri = uri.substring(0, uri.lastIndexOf("/") + 1);

try {
var segments = ((f) tsParser.a(Uri.parse(baseUri), IOUtils.openStream(uri))).o;

byte[] buff = getMergedTs(baseUri, segments, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,26 @@

import com.vk.dto.music.MusicTrack;
import com.vk.dto.music.Playlist;
import ru.vtosters.lite.music.interfaces.Callback;

import java.io.File;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import java8.util.concurrent.CompletableFuture;
import ru.vtosters.lite.music.interfaces.Callback;

public class PlaylistDownloader {
public static void downloadPlaylist(List<MusicTrack> playlist, String playlistName, String path, Callback callback) {
var outDir = new File(path);
if (!outDir.exists())
if (outDir.mkdirs()) Log.v("PlaylistDownloader", "Directory created");
else Log.e("PlaylistDownloader", "Directory creation failed");
Callback delegate = new ProgressCallback(callback);
CompletableFuture.allOf(playlist
.stream()
.map(x -> {
Callback.CompletableFutureCallback d = new
Callback.CompletableFutureCallback(delegate);
TrackDownloader.downloadTrack(x, path, d);
return d;
})
.toArray(CompletableFuture[]::new));
final Callback progress = new ProgressCallback(callback);
playlist.forEach(x -> TrackDownloader.downloadTrack(x, path, progress));
}

public static void cachePlaylist(List<MusicTrack> playlist, Callback callback, Playlist playlistId) {
Callback delegate = new ProgressCallback(callback);
CompletableFuture.allOf(playlist
.stream()
.map(x -> {
Callback.CompletableFutureCallback d = new Callback.CompletableFutureCallback(delegate);
TrackDownloader.cacheTrack(x, d, playlistId);
return d;
})
.toArray(CompletableFuture[]::new));
final Callback progress = new ProgressCallback(callback);
playlist.forEach(x -> TrackDownloader.cacheTrack(x, progress, playlistId));
}

public static final class ProgressCallback implements Callback {
Expand All @@ -61,7 +45,5 @@ public void onSuccess() {
public void onFailure(Throwable e) {
origin.onFailure(e);
}

@Override public void onSizeReceived(long size, long header) {}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package ru.vtosters.lite.music.downloader;

import android.util.Log;
import com.vk.dto.music.MusicTrack;
import com.vk.dto.music.Playlist;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import ru.vtosters.lite.music.interfaces.Callback;
import ru.vtosters.lite.music.interfaces.ITrackDownloader;
import ru.vtosters.lite.music.interfaces.IDownloader;
import ru.vtosters.lite.utils.IOUtils;
import ru.vtosters.lite.utils.music.MusicCacheStorageUtils;
import ru.vtosters.lite.utils.music.PlaylistUtils;

import java.io.IOException;
import java.net.URL;
import java.util.List;
public class ThumbnailPlaylistDownloader implements IDownloader<Playlist> {

// define the order of sizes to download
private static final List<Integer> SIZES = List.of(600, 1200, 68, 135, 300, 270);

public class ThumbnailPlaylistDownloader implements ITrackDownloader {
private final Callback callback;

public ThumbnailPlaylistDownloader(Callback callback) {
this.callback = callback;
}

@Override
public void download(MusicTrack track, Callback callback, Playlist playlist) {
public void download(Playlist playlist) {
JSONObject json = PlaylistUtils.getThumb(playlist); // get thumbnail photo

if (json == null) {
Expand All @@ -28,19 +35,18 @@ public void download(MusicTrack track, Callback callback, Playlist playlist) {
}

try {
List<String> sizes = List.of("600", "1200", "68", "135", "300", "270"); // define the order of sizes to download
JSONArray sizesArray = json.getJSONArray("sizes");
for (String size : sizes) {
for (int size : SIZES) {
for (int i = 0; i < sizesArray.length(); i++) {
JSONObject sizeObj = sizesArray.getJSONObject(i);
if (sizeObj.getInt("width") == Integer.parseInt(size)) {
if (sizeObj.getInt("width") == size) {
String url = sizeObj.getString("src");

if (url.isEmpty()) continue;

Log.d("Playlist", "downloading thumb " + url);

downloadThumbnailPlaylist(url, Integer.parseInt(size), playlist.v1());
downloadThumbnailPlaylist(url, size, playlist.v1());
}
}
}
Expand Down
Loading

0 comments on commit d2aff9e

Please sign in to comment.