Advertisement
Guest User

ankiwindowslinuxluascript

a guest
Sep 2nd, 2020
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. -- -- Open clipboard inserter
  2. -- -- Wait for unknown word and add it to anki through yomichan
  3. -- -- Select all the subtitle lines to add to the card.
  4. -- -- Ctrl + c [make sure it copies with paragraphs included, otherwise it wont work]
  5. -- -- Change back to MPV and Ctrl + v
  6. -- -- Done. The lines, their respective Audio and the current paused image
  7. -- will be added to the back of the card
  8. -- -- It's not too bad. You would need the prefix path to whatever it is on Windows. The clipboard command would need to be to changed to the windows equivalent (whatever that is). You would need ffmpeg and curl in your path. I'm not 100% that "\" works as an escape character in windows cmd in this way (maybe it does), so possibly you would need to change that. I don't see anything else though.
  9. -- -- djt anon: i managed to get it to copy the lines. not sure what the original clip() funtion was for tho. not sure why it doesn't update the fields or run the ffmpeg command tho lol
  10.  
  11.  
  12.  
  13. local utils = require 'mp.utils'
  14. local msg = require 'mp.msg'
  15. local subs = {}
  16.  
  17. -- User Config
  18. local FRONT_FIELD = "Front"
  19. local SENTENCE_AUDIO_FIELD = "Audio"
  20. local SENTENCE_FIELD = "Sentence"
  21. local IMAGE_FIELD = "Image"
  22. local prefix = os.getenv("HOMEPATH") .. '/AppData/Roaming/Anki2/Japanese/collection.media/'
  23.  
  24.  function trim(s)
  25.    return string.gsub(s, "^%s*(.-)%s*$", "%1")
  26.  end
  27.  
  28.  function get_name(s, e)
  29.     return  mp.get_property("filename"):gsub('%W','').. tostring(s) .. tostring(e)
  30.  end
  31.  
  32.   function clip()
  33.       local res = utils.subprocess({ args = {
  34.                                    'powershell', '-NoProfile', '-Command', string.format([[& {
  35.                                      Trap {
  36.                                      Write-Error -ErrorRecord $_
  37.                                      Exit 1
  38.                                      }
  39.                                      Add-Type -AssemblyName PresentationCore
  40.                                      [System.Windows.Clipboard]::SetText('%s')
  41.                                      }]], mp.get_property("sub-text"))
  42.                               } })
  43.  end
  44.  
  45.  function create_audio(s, e)
  46.  
  47.      if s == nil or e == nil then
  48.          return
  49.      end
  50.  
  51.      local t = tostring(e - s)
  52.      local source = mp.get_property("path")
  53.      local destination = prefix .. get_name(s, e) .. '.mp3'
  54.      local cmd = "ffmpeg -y -i \"" .. source .. "\" -ss " .. tostring(s)
  55.          .. " -t " .. t .. " -q:a 0 -map a \"".. destination.. "\" 2> /dev/null &"
  56.      os.execute(cmd)
  57.  end
  58.  
  59.  function get_clipboard()
  60.      local command = 'powershell.exe -command "get-clipboard"'
  61.      local handle = io.popen(command)
  62.      local result = handle:read("*a")
  63.      handle:close()
  64.      return result
  65.  end
  66.  
  67.  
  68.  function set_clipboard(name, sub)
  69.      if sub and mp.get_property_number('sub-start') then
  70.          local sub_delay = mp.get_property_native("sub-delay")
  71.          clip(sub)
  72.          subs[sub] = { mp.get_property_number('sub-start') + sub_delay, mp.get_property_number('sub-end') + sub_delay }
  73.      end
  74.  end
  75.  
  76.  
  77.   function create_screenshot(s, e)
  78.     local img = prefix .. get_name(s,e) .. '.jpg'
  79.     mp.commandv("raw", "no-osd", "screenshot-to-file", img)
  80.     os.execute("mogrify -resize 640x480 -quality 85 -format jpg \"" .. img .."\"")
  81.  end
  82.  
  83.  function get_extract()
  84.     local lines = get_clipboard()
  85.     local e = 0
  86.     local s = 0
  87.      for line in lines:gmatch("([^\n]*)\n?") do
  88.      line = trim(line)
  89.      if subs[line]~= nil then
  90.             msg.info(lines)
  91.         if subs[line][1] ~= nil and subs[line][2] ~= nil then
  92.                 msg.info(lines)
  93.           if s == 0 then
  94.             s = subs[line][1]
  95.           else
  96.             s = math.min(s, subs[line][1])
  97.           end
  98.           e = math.max(e, subs[line][2])
  99.         end
  100.      end
  101.      end
  102.     if e ~= 0 then
  103.         local ifield = create_screenshot(s, e)
  104.         local afield = create_audio(s, e)
  105.         local tfield = string.gsub(string.gsub(lines,"\n", "<br />"), "\r", "")
  106.         ifield = '<img src=\"'.. get_name(s,e) ..'.jpg\">'
  107.         afield = "[sound:".. get_name(s,e) .. ".mp3]"
  108.         add_to_last_added(ifield, afield, tfield)
  109.     end
  110.  end
  111.  
  112. function get(request)
  113.     local args = {
  114.         "curl",
  115.         "localhost:8765",
  116.         "-X",
  117.         "GET",
  118.         "-d",
  119.         request
  120.     }
  121.  
  122.     local result =
  123.       utils.subprocess(
  124.       {
  125.         args = args,
  126.         cancellable = true
  127.       }
  128.     )
  129.  
  130.     return utils.parse_json(result.stdout)
  131. end
  132.  
  133. function add_to_last_added(ifield, afield, tfield)
  134.     local noteid = math.max(unpack(get('{"action": "findNotes", "params": {"query": "added:1"}, "version": 6}')["result"]))
  135.     local note = get('{"action": "notesInfo", "params": {"notes": ['.. noteid.. ']}, "version": 6}')
  136.  
  137.     ifield = string.gsub(ifield, "\"", "\\\"")
  138.     afield = string.gsub(afield, "\"", "\\\"")
  139.     tfield = string.gsub(tfield, "\"", "\\\"")
  140.  
  141.     if note ~= nil then
  142.         get('{"action": "updateNoteFields", "version": 6, "params": {"note": {"id": ' .. noteid.. ', "fields": {"' .. SENTENCE_AUDIO_FIELD .. '": "' .. afield .. '", "' .. SENTENCE_FIELD .. '": "' .. tfield .. '", "' .. IMAGE_FIELD .. '": "' .. ifield .. '" } } } }')
  143.         mp.osd_message("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"], 3)
  144.         msg.info("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"])
  145.     end
  146. end
  147.  
  148. function ex()
  149.         pcall(get_extract)
  150. end
  151.  
  152. mp.observe_property("sub-text", 'string', set_clipboard)
  153. mp.add_key_binding("ctrl+v", ex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement