Advertisement
Guest User

Untitled

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