@@ -895,49 +895,40 @@ function open_subtitle_downloader()
895
895
end
896
896
end
897
897
898
- local handle_select , handle_search
899
-
900
- -- Ensures response is valid, and returns its payload, or handles error reporting,
901
- -- and returns `nil`, indicating the consumer should abort response handling.
902
- local function ensure_response_data (success , result , error , check )
903
- local data
904
- if success and result and result .status == 0 then
905
- data = utils .parse_json (result .stdout )
906
- if not data or not check (data ) then
907
- data = (data and data .error == true ) and data or {
908
- error = true ,
909
- message = t (' invalid response json (see console for details)' ),
910
- message_verbose = ' invalid response json: ' .. utils .to_string (result .stdout ),
911
- }
912
- end
913
- else
914
- data = {
915
- error = true ,
916
- message = error or t (' process exited with code %s (see console for details)' , result .status ),
917
- message_verbose = result .stdout .. result .stderr ,
918
- }
919
- end
920
-
921
- if data .error then
922
- local message , message_verbose = data .message or t (' unknown error' ), data .message_verbose or data .message
923
- if message_verbose then msg .error (message_verbose ) end
898
+ local handle_download , handle_search
899
+
900
+ -- Checks if there an error, or data is invalid. If true, reports the error,
901
+ -- updates menu to inform about it, and returns true.
902
+ --- @param error string | nil
903
+ --- @param data any
904
+ --- @param check_is_valid ? fun ( data : any ): boolean
905
+ --- @return boolean abort Whether the further response handling should be aborted.
906
+ local function should_abort (error , data , check_is_valid )
907
+ if error or not data or (not check_is_valid or not check_is_valid (data )) then
924
908
menu :update_items ({
925
909
{
926
- title = message ,
927
- hint = t (' error' ),
910
+ title = t (' Something went wrong.' ),
911
+ align = ' center' ,
912
+ muted = true ,
913
+ italic = true ,
914
+ selectable = false ,
915
+ },
916
+ {
917
+ title = t (' See console for details.' ),
918
+ align = ' center' ,
928
919
muted = true ,
929
920
italic = true ,
930
921
selectable = false ,
931
922
},
932
923
})
933
- return
924
+ msg .error (error or (' Invalid response: ' .. (utils .format_json (data ) or tostring (data ))))
925
+ return true
934
926
end
935
-
936
- return data
927
+ return false
937
928
end
938
929
939
930
--- @param data { kind : ' file' , id : number }|{ kind : ' page' , query : string , page : number }
940
- handle_select = function (data )
931
+ handle_download = function (data )
941
932
if data .kind == ' page' then
942
933
handle_search (data .query , data .page )
943
934
return
@@ -953,25 +944,14 @@ function open_subtitle_downloader()
953
944
end
954
945
end )
955
946
956
- local args = itable_join ({config . ziggy_path , ' download-subtitles' }, credentials , {
947
+ local args = itable_join ({' download-subtitles' }, credentials , {
957
948
' --file-id' , tostring (data .id ),
958
949
' --destination' , destination_directory ,
959
950
})
960
951
961
- mp .command_native_async ({
962
- name = ' subprocess' ,
963
- capture_stderr = true ,
964
- capture_stdout = true ,
965
- playback_only = false ,
966
- args = args ,
967
- }, function (success , result , error )
952
+ call_ziggy_async (args , function (error , data )
968
953
if not menu :is_alive () then return end
969
-
970
- local data = ensure_response_data (success , result , error , function (data )
971
- return type (data .file ) == ' string'
972
- end )
973
-
974
- if not data then return end
954
+ if should_abort (error , data , function (data ) return type (data .file ) == ' string' end ) then return end
975
955
976
956
load_track (' sub' , data .file )
977
957
@@ -1008,7 +988,7 @@ function open_subtitle_downloader()
1008
988
1009
989
menu :update_items ({{icon = ' spinner' , align = ' center' , selectable = false , muted = true }})
1010
990
1011
- local args = itable_join ({config . ziggy_path , ' search-subtitles' }, credentials )
991
+ local args = itable_join ({' search-subtitles' }, credentials )
1012
992
1013
993
local languages = itable_filter (get_languages (), function (lang ) return lang :match (' .json$' ) == nil end )
1014
994
args [# args + 1 ] = ' --languages'
@@ -1027,20 +1007,14 @@ function open_subtitle_downloader()
1027
1007
args [# args + 1 ] = query
1028
1008
end
1029
1009
1030
- mp .command_native_async ({
1031
- name = ' subprocess' ,
1032
- capture_stderr = true ,
1033
- capture_stdout = true ,
1034
- playback_only = false ,
1035
- args = args ,
1036
- }, function (success , result , error )
1010
+ call_ziggy_async (args , function (error , data )
1037
1011
if not menu :is_alive () then return end
1038
1012
1039
- local data = ensure_response_data ( success , result , error , function (data )
1013
+ local function check_is_valid (data )
1040
1014
return type (data .data ) == ' table' and data .page and data .total_pages
1041
- end )
1015
+ end
1042
1016
1043
- if not data then return end
1017
+ if should_abort ( error , data , check_is_valid ) then return end
1044
1018
1045
1019
local subs = itable_filter (data .data , function (sub )
1046
1020
return sub and sub .attributes and sub .attributes .release and type (sub .attributes .files ) == ' table' and
@@ -1131,7 +1105,7 @@ function open_subtitle_downloader()
1131
1105
end
1132
1106
end )
1133
1107
elseif not event .action then
1134
- handle_select (event .value )
1108
+ handle_download (event .value )
1135
1109
end
1136
1110
elseif event .type == ' search' then
1137
1111
handle_search (event .query )
0 commit comments