Skip to content

Commit

Permalink
Merge pull request #95 from HolyParzival/forvo-fix
Browse files Browse the repository at this point in the history
Output forvo audio files to anki media dir instead of calling store_file
  • Loading branch information
tatsumoto-ren authored Apr 19, 2023
2 parents 2bf69c4 + 141a84a commit 376842c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions subs2srs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ local function update_last_note(overwrite)
return h.notify("Couldn't find the target note.", "warn", 2)
end

encoder.set_output_dir(get_anki_media_dir_path())
local anki_media_dir = get_anki_media_dir_path()
encoder.set_output_dir(anki_media_dir)
local snapshot = encoder.snapshot.create_job(sub)
local audio = encoder.audio.create_job(sub, audio_padding())

Expand All @@ -409,6 +410,7 @@ local function update_last_note(overwrite)
local new_data = construct_note_fields(sub['text'], sub['secondary'], snapshot.filename, audio.filename)
local stored_data = ankiconnect.get_note_fields(last_note_id)
if stored_data then
forvo.set_output_dir(anki_media_dir)
new_data = forvo.append(new_data, stored_data)
new_data = update_sentence(new_data, stored_data)
if not overwrite then
Expand Down Expand Up @@ -546,7 +548,7 @@ local main = (function()

cfg_mgr.init(config, profiles)
ankiconnect.init(config, platform)
forvo.init(config, ankiconnect, platform)
forvo.init(config, platform)
encoder.init(config)
secondary_sid.init(config)
ensure_deck()
Expand Down
20 changes: 12 additions & 8 deletions utils/forvo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ local utils = require('mp.utils')
local msg = require('mp.msg')
local h = require('helpers')
local base64 = require('utils.base64')
local self = {}
local self = {
output_dir_path = nil,
}

local function url_encode(url)
-- https://gist.github.com/liukun/f9ce7d6d14fa45fe9b924a3eed5c3d99
Expand Down Expand Up @@ -46,11 +48,9 @@ local function reencode(source_path, dest_path)
end

local function reencode_and_store(source_path, filename)
local reencoded_path = utils.join_path(self.platform.tmp_dir(), 'reencoded_' .. filename)
reencode(source_path, reencoded_path)
local result = self.ankiconnect.store_file(filename, reencoded_path)
os.remove(reencoded_path)
return result
local reencoded_path = utils.join_path(self.output_dir_path, filename)
local result = reencode(source_path, reencoded_path)
return result.status == 0
end

local function curl_save(source_url, save_location)
Expand Down Expand Up @@ -127,13 +127,17 @@ local append = function(new_data, stored_data)
return new_data
end

local function init(config, ankiconnect, platform)
local set_output_dir = function(dir_path)
self.output_dir_path = dir_path
end

local function init(config, platform)
self.config = config
self.ankiconnect = ankiconnect
self.platform = platform
end

return {
append = append,
init = init,
set_output_dir = set_output_dir,
}

0 comments on commit 376842c

Please sign in to comment.