Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. local mp = require 'mp'
  2.  
  3. -- path to csv file
  4. local csv_filename = 'sheet.csv'
  5. local csv_entry_separator = '\n' -- change it to ',' if you want all the entries in a single line
  6. local allow_repeats = true
  7.  
  8. -- this function returns csv entry that will be written to file
  9. -- edit this function to your liking
  10. --
  11. -- i wrapped it in a function, so that any additional logic will not fuck up anything else
  12. --
  13. -- reference:
  14. -- https://mpv.io/manual/master/#command-interface-filename
  15. -- https://mpv.io/manual/master/#command-interface-path
  16. function get_csv_entry()
  17.     -- reference: https://mpv.io/manual/master/#properties
  18.     local filename = mp.get_property('filename')
  19.     local filesize = mp.get_property('file-size')
  20.    
  21.     filename = filename:gsub('%,', '.') -- replace all ',' with '.' in filename to avoid csv conflicts
  22.    
  23.     csv_entry = filename .. ',' .. filesize
  24.    
  25.     -- could be useful for debugging
  26.     -- mp.osd_message('csv_entry: ' .. tostring(csv_entry))
  27.     return csv_entry
  28. end
  29.  
  30. -- this function determines if the entry should be written to the csv file
  31. -- it takes get_csv_entry result as an argument, feel free to use (or completely ignore) it
  32. --
  33. -- >if it meets certain parameters? like, reaching 80% of the lenght or something like that
  34. -- i have no bloody idea what's that supposed to mean
  35. function check_file(csv_entry)
  36.     -- example logic - only mp4 files will be written to csv
  37.     local filename = mp.get_property('filename')
  38.     if csv_entry ~= '' and filename:find('mp4') then
  39.         return true
  40.     end
  41.    
  42.     return false
  43. end
  44.  
  45. -- ------------------------------------------------------------------------------------------------------------
  46.  
  47. function on_file_loaded()
  48.     local csv_entry = get_csv_entry()
  49.     if not csv_entry or not check_file(csv_entry) then
  50.         -- could be useful for debugging
  51.         -- mp.osd_message('File didn\'t pass verification')
  52.         return -- do nothing if the file verification didn't pass
  53.     end
  54.    
  55.     -- uncomment for debugging
  56.     -- mp.osd_message('File verification passed')
  57.    
  58.     local file_contents = {}
  59.     local is_repeat = false
  60.    
  61.     -- read current file contents if file exists
  62.     local csv_file = io.open(csv_filename, 'r')
  63.     if csv_file then
  64.         for line in csv_file:lines() do
  65.             if not is_repeat and line == csv_entry then
  66.                 is_repeat = true
  67.             end
  68.            
  69.             table.insert(file_contents, line)
  70.         end
  71.        
  72.         csv_file:close()
  73.     end
  74.    
  75.     -- append new entry at the end of the file (unless it's a repeat and repeats are disallowed)
  76.     if allow_repeats or not is_repeat then
  77.         file_contents[#file_contents + 1] = csv_entry
  78.     end
  79.    
  80.     -- sort file contents; you can pass your own comparator if you want to
  81.     -- reference: http://lua-users.org/wiki/TableLibraryTutorial
  82.     table.sort(file_contents)
  83.    
  84.     -- open file for writing
  85.     csv_file = io.open(csv_filename, 'w')
  86.     if not csv_file then
  87.         mp.osd_message('Could not open ' .. tostring(csv_filename) .. ' file for writing')
  88.         return
  89.     end
  90.    
  91.     -- write file entry by entry
  92.     for index, line in pairs(file_contents) do
  93.         if csv_entry_separator == '\n' then
  94.             csv_file:write(line,  csv_entry_separator)
  95.         elseif index > 1 then
  96.             csv_file:write(csv_entry_separator, line)
  97.         else
  98.             csv_file:write(line)
  99.         end
  100.     end
  101.  
  102.     csv_file:close()
  103. end
  104.  
  105. mp.register_event('file-loaded', on_file_loaded)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement