99import java .util .HashMap ;
1010import java .util .List ;
1111import 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 ;
1616
1717import static io .github .thoroldvix .api .YtApiV3Endpoint .*;
1818
@@ -32,43 +32,35 @@ class DefaultPlaylistsTranscriptApi implements PlaylistsTranscriptApi {
3232
3333 @ Override
3434 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 <>();
3636 List <String > videoIds = getVideoIds (playlistId , apiKey );
37- ExecutorService executor = Executors .newCachedThreadPool ();
3837
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+ }
4747 return null ;
48- }
49- }));
50- }
51-
52- try {
53- for (Future <TranscriptList > future : futures ) {
54- try {
55- TranscriptList transcriptList = future .get ();
48+ }).thenAccept (transcriptList -> {
5649 if (transcriptList != null ) {
5750 transcriptLists .put (transcriptList .getVideoId (), transcriptList );
5851 }
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 );
6963 }
70- } finally {
71- executor .shutdownNow ();
7264 }
7365
7466 return transcriptLists ;
0 commit comments