]> git.madduck.net Git - etc/awesome.git/blob - widget/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:

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