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

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