9
9
import java .util .HashMap ;
10
10
import java .util .List ;
11
11
import java .util .Map ;
12
- import java .util .concurrent .ExecutionException ;
13
- import java .util .concurrent .ExecutorService ;
14
- import java .util .concurrent .Executors ;
15
- import java .util .concurrent . Future ;
12
+ import java .util .concurrent .CompletableFuture ;
13
+ import java .util .concurrent .CompletionException ;
14
+ import java .util .concurrent .ConcurrentHashMap ;
15
+ import java .util .stream . Collectors ;
16
16
17
17
import static io .github .thoroldvix .api .YtApiV3Endpoint .*;
18
18
@@ -32,43 +32,35 @@ class DefaultPlaylistsTranscriptApi implements PlaylistsTranscriptApi {
32
32
33
33
@ Override
34
34
public Map <String , TranscriptList > listTranscriptsForPlaylist (String playlistId , String apiKey , String cookiesPath , boolean continueOnError ) throws TranscriptRetrievalException {
35
- Map <String , TranscriptList > transcriptLists = new HashMap <>();
35
+ Map <String , TranscriptList > transcriptLists = new ConcurrentHashMap <>();
36
36
List <String > videoIds = getVideoIds (playlistId , apiKey );
37
- ExecutorService executor = Executors .newCachedThreadPool ();
38
37
39
- List <Future <TranscriptList >> futures = new ArrayList <>();
40
-
41
- for (String videoId : videoIds ) {
42
- futures .add (executor .submit (() -> {
43
- try {
44
- return getTranscriptList (videoId , cookiesPath );
45
- } catch (TranscriptRetrievalException e ) {
46
- if (!continueOnError ) throw e ;
38
+ List <CompletableFuture <Void >> futures = videoIds .stream ()
39
+ .map (videoId -> CompletableFuture .supplyAsync (() -> {
40
+ try {
41
+ return getTranscriptList (videoId , cookiesPath );
42
+ } catch (TranscriptRetrievalException e ) {
43
+ if (!continueOnError ) {
44
+ throw new CompletionException (e );
45
+ }
46
+ }
47
47
return null ;
48
- }
49
- }));
50
- }
51
-
52
- try {
53
- for (Future <TranscriptList > future : futures ) {
54
- try {
55
- TranscriptList transcriptList = future .get ();
48
+ }).thenAccept (transcriptList -> {
56
49
if (transcriptList != null ) {
57
50
transcriptLists .put (transcriptList .getVideoId (), transcriptList );
58
51
}
59
- } catch (ExecutionException e ) {
60
- if (!continueOnError ) {
61
- executor .shutdownNow ();
62
- throw new TranscriptRetrievalException ("Failed to retrieve transcripts for playlist: " + playlistId , e );
63
- }
64
- } catch (InterruptedException e ) {
65
- Thread .currentThread ().interrupt ();
66
- executor .shutdownNow ();
67
- throw new TranscriptRetrievalException ("Failed to retrieve transcripts for playlist: " + playlistId , e );
68
- }
52
+ }))
53
+ .collect (Collectors .toList ());
54
+
55
+ try {
56
+ CompletableFuture <Void > allOf = CompletableFuture .allOf (futures .toArray (new CompletableFuture [0 ]));
57
+ allOf .join ();
58
+ } catch (CompletionException e ) {
59
+ if (e .getCause () instanceof TranscriptRetrievalException ) {
60
+ throw (TranscriptRetrievalException ) e .getCause ();
61
+ } else {
62
+ throw new TranscriptRetrievalException ("Failed to retrieve transcripts for playlist: " + playlistId , e );
69
63
}
70
- } finally {
71
- executor .shutdownNow ();
72
64
}
73
65
74
66
return transcriptLists ;
0 commit comments