]> git.madduck.net Git - etc/awesome.git/blob - widgets/contrib/gpmdp.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge pull request #206 from Kirogo/master
[etc/awesome.git] / widgets / contrib / gpmdp.lua
1
2 --[[
3
4         Licensed under GNU General Public License v2
5         * (c) 2016, Alexandre Terrien
6
7 --]]
8
9 local helpers       = require("lain.helpers")
10 local json          = require("lain.util.dkjson")
11
12 local pread         = require("awful.util").pread
13
14 local naughty       = require("naughty")
15 local wibox         = require("wibox")
16
17 local mouse         = mouse
18
19 local setmetatable = setmetatable
20
21 -- Google Play Music Desktop infos
22 -- lain.widget.contrib.gpmdp
23 local gpmdp = {}
24
25 local function worker(args)
26     local args          = args or {}
27     local timeout       = args.timeout or 2
28     local notify        = args.notify or "off"
29     local followmouse   = args.followmouse or false
30     local file_location = args.file_location or
31         os.getenv("HOME") .. "/.config/Google Play Music Desktop Player/json_store/playback.json"
32     local settings      = args.settings or function() end
33
34     gpmdp.widget = wibox.widget.textbox('')
35
36     gpmdp_notification_preset = {
37         title   = "Now playing",
38         timeout = 6
39     }
40
41     helpers.set_map("gpmpd_current", nil)
42
43     function gpmdp.update()
44         file, err = io.open(file_location, "r")
45         if not file
46         then
47             gpm_now = {
48                 running = false,
49                 playing = false
50             }
51         else
52             dict, pos, err = json.decode(file:read "*a", 1, nil)
53             file:close()
54             gpm_now = {}
55             gpm_now.artist      = dict.song.artist
56             gpm_now.album       = dict.song.album
57             gpm_now.title       = dict.song.title
58             gpm_now.cover_url   = dict.song.albumArt
59             gpm_now.playing     = dict.playing
60         end
61         if (pread("pidof 'Google Play Music Desktop Player'") ~= '') then
62             gpm_now.running = true
63         else
64             gpm_now.running = false
65         end
66
67         gpmdp_notification_preset.text = string.format(
68                                         "%s (%s) - %s",
69                                         gpm_now.artist,
70                                         gpm_now.album,
71                                         gpm_now.title)
72         widget = gpmdp.widget
73         settings()
74
75         if gpm_now.playing
76         then
77             if notify == "on" and gpm_now.title ~= helpers.get_map("gpmpd_current")
78             then
79                 helpers.set_map("gpmpd_current", gpm_now.title)
80
81                 current_icon = "/tmp/gpmcover.png"
82                 os.execute("curl " .. gpm_now.cover_url .. " -o " .. current_icon)
83
84                 if followmouse then
85                     gpmdp_notification_preset.screen = mouse.screen
86                 end
87
88                 gpmdp.id = naughty.notify({
89                     preset = gpmdp_notification_preset,
90                     icon = current_icon,
91                     replaces_id = gpmdp.id,
92                 }).id
93             end
94         elseif (not gpm_now.running)
95         then
96             helpers.set_map("current mpd track", nil)
97         end
98     end
99
100     helpers.newtimer("gpmdp", timeout, gpmdp.update)
101
102     return setmetatable(gpmdp, { __index = gpmdp.widget })
103 end
104
105 return setmetatable(gpmdp, { __call = function(_, ...) return worker(...) end })