Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mp = require 'mp'
- -- path to csv file
- local csv_filename = 'sheet.csv'
- local csv_entry_separator = '\n' -- change it to ',' if you want all the entries in a single line
- local allow_repeats = true
- -- this function returns csv entry that will be written to file
- -- edit this function to your liking
- --
- -- i wrapped it in a function, so that any additional logic will not fuck up anything else
- --
- -- reference:
- -- https://mpv.io/manual/master/#command-interface-filename
- -- https://mpv.io/manual/master/#command-interface-path
- function get_csv_entry()
- -- reference: https://mpv.io/manual/master/#properties
- local filename = mp.get_property('filename')
- local filesize = mp.get_property('file-size')
- filename = filename:gsub('%,', '.') -- replace all ',' with '.' in filename to avoid csv conflicts
- csv_entry = filename .. ',' .. filesize
- -- could be useful for debugging
- -- mp.osd_message('csv_entry: ' .. tostring(csv_entry))
- return csv_entry
- end
- -- this function determines if the entry should be written to the csv file
- -- it takes get_csv_entry result as an argument, feel free to use (or completely ignore) it
- --
- -- >if it meets certain parameters? like, reaching 80% of the lenght or something like that
- -- i have no bloody idea what's that supposed to mean
- function check_file(csv_entry)
- -- example logic - only mp4 files will be written to csv
- local filename = mp.get_property('filename')
- if csv_entry ~= '' and filename:find('mp4') then
- return true
- end
- return false
- end
- -- ------------------------------------------------------------------------------------------------------------
- function on_file_loaded()
- local csv_entry = get_csv_entry()
- if not csv_entry or not check_file(csv_entry) then
- -- could be useful for debugging
- -- mp.osd_message('File didn\'t pass verification')
- return -- do nothing if the file verification didn't pass
- end
- -- uncomment for debugging
- -- mp.osd_message('File verification passed')
- local file_contents = {}
- local is_repeat = false
- -- read current file contents if file exists
- local csv_file = io.open(csv_filename, 'r')
- if csv_file then
- for line in csv_file:lines() do
- if not is_repeat and line == csv_entry then
- is_repeat = true
- end
- table.insert(file_contents, line)
- end
- csv_file:close()
- end
- -- append new entry at the end of the file (unless it's a repeat and repeats are disallowed)
- if allow_repeats or not is_repeat then
- file_contents[#file_contents + 1] = csv_entry
- end
- -- sort file contents; you can pass your own comparator if you want to
- -- reference: http://lua-users.org/wiki/TableLibraryTutorial
- table.sort(file_contents)
- -- open file for writing
- csv_file = io.open(csv_filename, 'w')
- if not csv_file then
- mp.osd_message('Could not open ' .. tostring(csv_filename) .. ' file for writing')
- return
- end
- -- write file entry by entry
- for index, line in pairs(file_contents) do
- if csv_entry_separator == '\n' then
- csv_file:write(line, csv_entry_separator)
- elseif index > 1 then
- csv_file:write(csv_entry_separator, line)
- else
- csv_file:write(line)
- end
- end
- csv_file:close()
- end
- mp.register_event('file-loaded', on_file_loaded)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement