Advertisement
Guest User

y dont it work

a guest
Sep 5th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.54 KB | None | 0 0
  1. local utils = require 'mp.utils'
  2. local msg = require 'mp.msg'
  3. local subs = {}
  4.  
  5. -- User Config
  6. local FRONT_FIELD = "Vocabulary"
  7. local SENTENCE_AUDIO_FIELD = "Audio"
  8. local SENTENCE_FIELD = "Expression"
  9. local IMAGE_FIELD = "Snapshot"
  10. local prefix = os.getenv("HOMEPATH") .. '/AppData/Roaming/Anki2/Japanese/collection.media/'
  11.  
  12.  function trim(s)
  13.    return string.gsub(s, "^%s*(.-)%s*$", "%1")
  14.  end
  15.  
  16.  function get_name(s, e)
  17.     return  mp.get_property("filename"):gsub('%W','').. tostring(s) .. tostring(e)
  18.  end
  19.  
  20.   function clip(text)
  21.   local res = utils.subprocess({ args = {
  22.                                    'powershell', '-NoProfile', '-Command', string.format([[& {
  23.                                      Trap {
  24.                                      Write-Error -ErrorRecord $_
  25.                                      Exit 1
  26.                                      }
  27.                                      Add-Type -AssemblyName PresentationCore
  28.                                      [System.Windows.Clipboard]::SetText('%s')
  29.                                      }]], text)
  30.                               } })
  31.  end
  32.  
  33.  function create_audio(s, e)
  34.  
  35.      if s == nil or e == nil then
  36.          return
  37.      end
  38.  
  39.      local t = tostring(e - s)
  40.      local source = mp.get_property("path")
  41.      local destination = prefix .. get_name(s, e) .. '.mp3'
  42.      local cmd = "ffmpeg -y -i \"" .. source .. "\" -ss " .. tostring(s)
  43.          .. " -t " .. t .. " -q:a 0 -map a \"".. destination.. "\" 2> null"
  44.      os.execute(cmd)
  45.  end
  46.  
  47.  function get_clipboard()
  48.      local command = "powershell.exe -command get-clipboard"
  49.      local handle = io.popen(command)
  50.      local result = handle:read("*a")
  51.      handle:close()
  52.      return result
  53.  end
  54.  
  55.  
  56.  function set_clipboard(name, sub)
  57.      if sub and mp.get_property_number('sub-start') then
  58.          local sub_delay = mp.get_property_native("sub-delay")
  59.          clip(sub)
  60.          subs[sub] = { mp.get_property_number('sub-start') + sub_delay, mp.get_property_number('sub-end') + sub_delay }
  61.      end
  62.  end
  63.  
  64.  
  65.   function create_screenshot(s, e)
  66.     local img = prefix .. get_name(s,e) .. '.jpg'
  67.     mp.commandv("raw", "no-osd", "screenshot-to-file", img)
  68.     os.execute("mogrify -resize 640x480 -quality 85 -format jpg \"" .. img .."\"")
  69.  end
  70.  
  71.  function get_extract()
  72.     local lines = get_clipboard()
  73.     local e = 0
  74.     local s = 0
  75.      for line in lines:gmatch("([^\n]*)\n?") do
  76.      line = trim(line)
  77.      if subs[line]~= nil then
  78.             msg.info(lines)
  79.         if subs[line][1] ~= nil and subs[line][2] ~= nil then
  80.                 msg.info(lines)
  81.           if s == 0 then
  82.             s = subs[line][1]
  83.           else
  84.             s = math.min(s, subs[line][1])
  85.           end
  86.           e = math.max(e, subs[line][2])
  87.         end
  88.      end
  89.      end
  90.     if e ~= 0 then
  91.         local ifield = create_screenshot(s, e)
  92.         local afield = create_audio(s, e)
  93.         local tfield = string.gsub(string.gsub(lines,"\n", "<br />"), "\r", "")
  94.         ifield = '<img src=\"'.. get_name(s,e) ..'.jpg\">'
  95.         afield = "[sound:".. get_name(s,e) .. ".mp3]"
  96.         add_to_last_added(ifield, afield, tfield)
  97.     end
  98.  end
  99.  
  100. function get(request)
  101.     local args = {
  102.         "curl",
  103.         "localhost:8765",
  104.         "-X",
  105.         "GET",
  106.         "-d",
  107.         request
  108.     }
  109.  
  110.     local result =
  111.       utils.subprocess(
  112.       {
  113.         args = args,
  114.         cancellable = true
  115.       }
  116.     )
  117.  
  118.     return utils.parse_json(result.stdout)
  119. end
  120.  
  121. function add_to_last_added(ifield, afield, tfield)
  122.     local noteid = math.max(unpack(get('{"action": "findNotes", "params": {"query": "tag:yomichan"}, "version": 6}')["result"]))
  123.     local note = get('{"action": "notesInfo", "params": {"notes": ['.. noteid.. ']}, "version": 6}')
  124.  
  125.     ifield = string.gsub(ifield, "\"", "\\\"")
  126.     afield = string.gsub(afield, "\"", "\\\"")
  127.     tfield = string.gsub(tfield, "\"", "\\\"")
  128.  
  129.     if note ~= nil then
  130.         get('{"action": "updateNoteFields", "version": 6, "params": {"note": {"id": ' .. noteid.. ', "fields": {"' .. SENTENCE_AUDIO_FIELD .. '": "' .. afield .. '", "' .. SENTENCE_FIELD .. '": "' .. tfield .. '", "' .. IMAGE_FIELD .. '": "' .. ifield .. '" } } } }')
  131.        
  132.         mp.osd_message("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"], 3)
  133.         msg.info("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"])
  134.     end
  135. end
  136.  
  137. function ex()
  138.         pcall(get_extract)
  139. end
  140.  
  141. mp.observe_property("sub-text", 'string', set_clipboard)
  142. mp.add_key_binding("ctrl+v", ex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement