]> 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 branch 'master' of https://github.com/trap000d/lain into trap000d-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 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 = {}
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.widget = wibox.widget.textbox()
34
35     gpmdp_notification_preset = {
36         title   = "Now playing",
37         timeout = 6
38     }
39
40     helpers.set_map("gpmdp_current", nil)
41
42     function gpmdp.update()
43         local filelines = helpers.lines_from(file_location)
44
45         if not next(filelines) then
46             local gpm_now = { running = false, playing = false }
47         else
48             dict, pos, err = json.decode(table.concat(filelines), 1, nil)
49             local 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 then
68             if notify == "on" and gpm_now.title ~= helpers.get_map("gpmdp_current") then
69                 helpers.set_map("gpmdp_current", gpm_now.title)
70
71                 if followtag then gpmdp_notification_preset.screen = focused() end
72
73                 helpers.async(string.format("curl %d -o /tmp/gpmcover.png", gpm_now.cover_url),
74                 function(f)
75                     gpmdp.id = naughty.notify({
76                         preset = gpmdp_notification_preset,
77                         icon = "/tmp/gpmcover.png",
78                         replaces_id = gpmdp.id
79                     }).id
80                 end)
81             end
82         elseif not gpm_now.running then
83             helpers.set_map("gpmdp_current", nil)
84         end
85     end
86
87     gpmdp.timer = helpers.newtimer("gpmdp", timeout, gpmdp.update, true, true)
88
89     return gpmdp
90 end
91
92 return setmetatable(gpmdp, { __call = function(_, ...) return worker(...) end })