-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhistory.lua
166 lines (121 loc) · 3.62 KB
/
history.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
--[[
https://github.com/stax76/mpv-scripts
All this script does is writing to a log file.
It writes:
1. The date and time: 10.09.2022 19:50
2. How many minutes were played: 3
3. Filename and location: D:\Samples\Big Buck Bunny.mkv
This is how a log line looks:
10.09.2022 19:50 3 D:\Samples\Big Buck Bunny.mkv
Conf options:
exclude=list of folders to be excluded, separated via semicolon
storage_path=~~/history.log
minimal_play_time=60
minimal_play_time=60 means it logs only files that were played for longer than 60 seconds
Similar or related scripts:
https://github.com/yuukidach/mpv-scripts/blob/master/history-bookmark.lua
https://gist.github.com/Abject-Web/3f4f0e85dad73303b9dd1ef1f55c3147
https://gist.github.com/garoto/e0eb539b210ee077c980e01fb2daef4a
https://github.com/Eisa01/mpv-scripts#simplehistory
https://github.com/hacel/recent
]]--
----- string
function is_empty(input)
if input == nil or input == "" then
return true
end
end
function trim(input)
if is_empty(input) then
return ""
end
return input:match "^%s*(.-)%s*$"
end
function contains(input, find)
if not is_empty(input) and not is_empty(find) then
return input:find(find, 1, true)
end
end
function replace(str, what, with)
if is_empty(str) then return "" end
if is_empty(what) then return str end
if with == nil then with = "" end
what = string.gsub(what, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1")
with = string.gsub(with, "[%%]", "%%%%")
return string.gsub(str, what, with)
end
function split(input, sep)
local tbl = {}
if not is_empty(input) then
for str in string.gmatch(input, "([^" .. sep .. "]+)") do
table.insert(tbl, str)
end
end
return tbl
end
function pad_left(input, len, char)
if input == nil then
input = ""
end
if char == nil then
char = ' '
end
return string.rep(char, len - #input) .. input
end
----- math
function round(value)
return value >= 0 and math.floor(value + 0.5) or math.ceil(value - 0.5)
end
----- file
function file_exists(path)
local f = io.open(path, "r")
if f ~= nil then
io.close(f)
return true
end
end
function file_append(path, content)
local h = assert(io.open(path, "ab"))
h:write(content)
h:close()
end
----- mpv
local msg = require "mp.msg"
----- history
time = 0 -- number of seconds since epoch
path = ""
local o = {
exclude = "",
storage_path = "~~/history.log",
minimal_play_time = 60,
}
opt = require "mp.options"
opt.read_options(o)
o.storage_path = mp.command_native({"expand-path", o.storage_path})
function discard()
for _, v in pairs(split(o.exclude, ";")) do
local p = replace(path, "/", "\\")
v = replace(trim(v), "/", "\\")
if contains(p, v) then
return true
end
end
end
function history()
local seconds = round(os.time() - time)
if not file_exists(o.storage_path) then
msg.error("Log file does not exist: " .. o.storage_path)
elseif not is_empty(path) and seconds > o.minimal_play_time and not discard() then
local minutes = round(seconds / 60)
local line = os.date("%d.%m.%Y %H:%M ") ..
pad_left(tostring(minutes), 3) .. " " .. path .. "\n"
file_append(o.storage_path, line)
end
path = mp.get_property("path")
if contains(path, "://") then
path = mp.get_property("media-title")
end
time = os.time()
end
mp.register_event("shutdown", history)
mp.register_event("file-loaded", history)