-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnotify-send.lua
95 lines (76 loc) · 2.63 KB
/
notify-send.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
local utils = require "mp.utils"
local cover_filenames = { "cover.png", "cover.jpg", "cover.jpeg",
"folder.jpg", "folder.png", "folder.jpeg",
"AlbumArtwork.png", "AlbumArtwork.jpg", "AlbumArtwork.jpeg" }
function notify(summary, body, options)
local option_args = {}
for key, value in pairs(options or {}) do
table.insert(option_args, string.format("--%s=%s", key, value))
end
return mp.command_native({
"run", "notify-send",
summary, body,
unpack(option_args)
})
end
function escape_pango_markup(str)
return string.gsub(str, "([\"'<>&])", function (char)
return string.format("&#%d;", string.byte(char))
end)
end
function notify_media(title, origin, thumbnail)
return notify(escape_pango_markup(title), origin, {
urgency = "low",
["app-name"] = "mpv",
hint = "string:desktop-entry:mpv",
icon = thumbnail or "mpv",
})
end
function file_exists(path)
local info, _ = utils.file_info(path)
return info ~= nil
end
function find_cover(dir)
-- make dir an absolute path
if dir[1] ~= "/" then
dir = utils.join_path(utils.getcwd(), dir)
end
for _, file in ipairs(cover_filenames) do
local path = utils.join_path(dir, file)
if file_exists(path) then
return path
end
end
return nil
end
function first_upper(str)
return (string.gsub(string.gsub(str, "^%l", string.upper), "_%l", string.upper))
end
function notify_current_media()
local path = mp.get_property_native("path")
local dir, file = utils.split_path(path)
-- TODO: handle embedded covers and videos?
-- potential options: mpv's take_screenshot, ffprobe/ffmpeg, ...
-- hooking off existing desktop thumbnails would be good too
local thumbnail = find_cover(dir)
local title = mp.get_property_native("media-title") or file
local origin = dir
local metadata = mp.get_property_native("metadata")
if metadata then
function tag(name)
return metadata[string.upper(name)] or metadata[first_upper(name)] or metadata[name]
end
title = tag("title") or title
origin = tag("artist_credit") or tag("artist") or ""
local album = tag("album")
if album then
origin = string.format("%s — %s", origin, album)
end
local year = tag("original_year") or tag("year")
if year then
origin = string.format("%s (%s)", origin, year)
end
end
return notify_media(title, origin, thumbnail)
end
mp.register_event("file-loaded", notify_current_media)