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

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