summaryrefslogtreecommitdiff
path: root/.config/mpv/scripts/notify-send.lua
blob: e0c022d26e6d31d1d67c6671496866b5bc80327f (plain)
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
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", unpack(option_args),
        summary, body,
    })
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, {
        -- For some inscrutable reason, GNOME 3.24.2
        -- nondeterministically fails to pick up the notification icon
        -- if either of these two parameters are present.
        --
        -- urgency = "low",
        -- ["app-name"] = "mpv",

        -- ...and this one makes notifications nondeterministically
        -- fail to appear altogether.
        --
        -- 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 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 = file
    local origin = dir

    local metadata = mp.get_property_native("metadata")
    if metadata then
        function tag(name)
            return metadata[string.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)