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

imap: asynchronous
[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) then
48             local gpm_now = { running = false, playing = false }
49         else
50             dict, pos, err = json.decode(table.concat(filelines), 1, nil)
51             local gpm_now = {}
52             gpm_now.artist    = dict.song.artist
53             gpm_now.album     = dict.song.album
54             gpm_now.title     = dict.song.title
55             gpm_now.cover_url = dict.song.albumArt
56             gpm_now.playing   = dict.playing
57         end
58
59         if pread("pidof 'Google Play Music Desktop Player'") ~= '' then
60             gpm_now.running = true
61         else
62             gpm_now.running = false
63         end
64
65         gpmdp_notification_preset.text = string.format("%s (%s) - %s", gpm_now.artist, gpm_now.album, gpm_now.title)
66         widget = gpmdp.widget
67         settings()
68
69         if gpm_now.playing then
70             if notify == "on" and gpm_now.title ~= helpers.get_map("gpmdp_current") then
71                 helpers.set_map("gpmdp_current", gpm_now.title)
72                 os.execute(string.format("curl %d -o /tmp/gpmcover.png", gpm_now.cover_url))
73
74                 if followtag then gpmdp_notification_preset.screen = focused() end
75
76                 gpmdp.id = naughty.notify({
77                     preset = gpmdp_notification_preset,
78                     icon = "/tmp/gpmcover.png",
79                     replaces_id = gpmdp.id,
80                 }).id
81             end
82         elseif not gpm_now.running
83         then
84             helpers.set_map("gpmdp_current", nil)
85         end
86     end
87
88     helpers.newtimer("gpmdp", timeout, gpmdp.update)
89
90     return setmetatable(gpmdp, { __index = gpmdp.widget })
91 end
92
93 return setmetatable(gpmdp, { __call = function(_, ...) return worker(...) end })