Guest User

Untitled

a guest
Apr 25th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1.  
  2. -- -- Open clipboard inserter
  3. -- -- Wait for unknown word and add it to anki through yomichan
  4. -- -- Select all the subtitle lines to add to the card.
  5. -- -- Ctrl + c [make sure it copies with paragraphs included, otherwise it wont work]
  6. -- -- Change back to MPV and Ctrl + v
  7. -- -- Done. The lines, their respective Audio and the current paused image
  8. -- will be added to the back of the card
  9.  
  10. local utils = require 'mp.utils'
  11. local msg = require 'mp.msg'
  12. local subs = {}
  13.  
  14. -- User Config
  15. local BACK_FIELD = "Back"
  16. local FRONT_FIELD = "Front"
  17. local prefix = os.getenv("HOME") .. '/.local/share/Anki2/User 1/collection.media/'
  18.  
  19. function trim(s)
  20. return string.gsub(s, "^%s*(.-)%s*$", "%1")
  21. end
  22.  
  23. function get_name(s, e)
  24. return mp.get_property("filename"):gsub('%W','').. tostring(s) .. tostring(e)
  25. end
  26.  
  27. function clip(text)
  28. os.execute("echo \"" .. text .. "\" | xclip -selection clipboard &")
  29. end
  30.  
  31. function create_audio(s, e)
  32.  
  33. if s == nil or e == nil then
  34. return
  35. end
  36.  
  37. local t = tostring(e - s)
  38. local source = mp.get_property("path")
  39. local destination = prefix .. get_name(s, e) .. '.mp3'
  40. local cmd = "ffmpeg -y -i \"" .. source .. "\" -ss " .. tostring(s)
  41. .. " -t " .. t .. " -q:a 0 -map a \"".. destination.. "\" 2> /dev/null &"
  42. os.execute(cmd)
  43. end
  44.  
  45. function get_clipboard()
  46. local command = 'xclip -o'
  47. local handle = io.popen(command)
  48. local result = handle:read("*a")
  49. handle:close()
  50. return result
  51. end
  52.  
  53.  
  54. function set_clipboard(name, sub)
  55. if sub and mp.get_property_number('sub-start') then
  56. local sub_delay = mp.get_property_native("sub-delay")
  57. clip(sub)
  58. subs[sub] = { mp.get_property_number('sub-start') + sub_delay, mp.get_property_number('sub-end') + sub_delay }
  59. end
  60. end
  61.  
  62.  
  63. function create_screenshot(s, e)
  64. local img = prefix .. get_name(s,e) .. '.jpg'
  65. mp.commandv("raw", "no-osd", "screenshot-to-file", img)
  66. os.execute("mogrify -resize 640x480 -quality 50 -format jpg \"" .. img .."\"")
  67. end
  68.  
  69. function get_extract()
  70. local lines = get_clipboard()
  71. local e = 0
  72. local s = 0
  73. for line in lines:gmatch("([^\n]*)\n?") do
  74. line = trim(line)
  75. if subs[line]~= nil then
  76. msg.info(lines)
  77. if subs[line][1] ~= nil and subs[line][2] ~= nil then
  78. msg.info(lines)
  79. if s == 0 then
  80. s = subs[line][1]
  81. else
  82. s = math.min(s, subs[line][1])
  83. end
  84. e = math.max(e, subs[line][2])
  85. end
  86. end
  87. end
  88. if e ~= 0 then
  89. local ifield = create_screenshot(s, e)
  90. local afield = create_audio(s, e)
  91. local tfield = "<center><p>".. string.gsub(string.gsub(lines,"\n", "<p>"), "\r", "")
  92. ifield = '<p><img src=\"'.. get_name(s,e) ..'.jpg\">'
  93. afield = "<p>[sound:".. get_name(s,e) .. ".mp3]</center>"
  94. local field = tfield .. ifield.. afield
  95. add_to_last_added(field)
  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(fields)
  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. if note ~= nil then
  125. local back = note["result"][1]["fields"][BACK_FIELD]["value"]
  126. back = string.gsub(back .. fields, "\"", "\\\"")
  127. get('{"action": "updateNoteFields", "version": 6, "params": {"note": {"id": ' .. noteid.. ', "fields": { "'.. BACK_FIELD..'": "'.. back .. '" } } } }')
  128.  
  129. mp.osd_message("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"], 3)
  130. msg.info("Updated note: " .. note["result"][1]["fields"][FRONT_FIELD]["value"])
  131. end
  132. end
  133.  
  134. function ex()
  135. pcall(get_extract)
  136. end
  137.  
  138. mp.observe_property("sub-text", 'string', set_clipboard)
  139. mp.add_key_binding("ctrl+v", ex)
Advertisement
Add Comment
Please, Sign In to add comment