]> 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:

ee40a8d108fa6c5f42d21bf6bccd7f596e15edda
[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 focused      = require("awful.screen").focused
13 local pread        = require("awful.util").pread
14 local naughty      = require("naughty")
15 local wibox        = require("wibox")
16
17 local next         = next
18 local os           = { getenv = os.getenv }
19 local setmetatable = setmetatable
20 local table        = table
21
22 -- Google Play Music Desktop infos
23 -- lain.widget.contrib.gpmdp
24 local gpmdp = {}
25
26 local function worker(args)
27     local args          = args or {}
28     local timeout       = args.timeout or 2
29     local notify        = args.notify or "off"
30     local followtag     = args.followtag or false
31     local file_location = args.file_location or
32                           os.getenv("HOME") .. "/.config/Google Play Music Desktop Player/json_store/playback.json"
33     local settings      = args.settings or function() end
34
35     gpmdp.widget = wibox.widget.textbox('')
36
37     gpmdp_notification_preset = {
38         title   = "Now playing",
39         timeout = 6
40     }
41
42     helpers.set_map("gpmdp_current", nil)
43
44     function gpmdp.update()
45         local filelines = helpers.lines_from(file_location)
46
47         if not next(filelines)
48         then
49             local gpm_now = { running = false, playing = false }
50         else
51             dict, pos, err = json.decode(table.concat(filelines), 1, nil)
52             local gpm_now = {}
53             gpm_now.artist    = dict.song.artist
54             gpm_now.album     = dict.song.album
55             gpm_now.title     = dict.song.title
56             gpm_now.cover_url = dict.song.albumArt
57             gpm_now.playing   = dict.playing
58         end
59
60         if pread("pidof 'Google Play Music Desktop Player'") ~= '' then
61             gpm_now.running = true
62         else
63             gpm_now.running = false
64         end
65
66         gpmdp_notification_preset.text = string.format("%s (%s) - %s", gpm_now.artist, gpm_now.album, gpm_now.title)
67         widget = gpmdp.widget
68         settings()
69
70         if gpm_now.playing
71         then
72             if notify == "on" and gpm_now.title ~= helpers.get_map("gpmdp_current")
73             then
74                 helpers.set_map("gpmdp_current", gpm_now.title)
75                 os.execute(string.format("curl %d -o /tmp/gpmcover.png", gpm_now.cover_url))
76
77                 if followtag then
78                     gpmdp_notification_preset.screen = focused()
79                 end
80
81                 gpmdp.id = naughty.notify({
82                     preset = gpmdp_notification_preset,
83                     icon = "/tmp/gpmcover.png",
84                     replaces_id = gpmdp.id,
85                 }).id
86             end
87         elseif not gpm_now.running
88         then
89             helpers.set_map("gpmdp_current", nil)
90         end
91     end
92
93     helpers.newtimer("gpmdp", timeout, gpmdp.update)
94
95     return setmetatable(gpmdp, { __index = gpmdp.widget })
96 end
97
98 return setmetatable(gpmdp, { __call = function(_, ...) return worker(...) end })