lain/widgets/contrib/gpmdp.lua

96 lines
3.0 KiB
Lua
Raw Normal View History

2016-06-22 23:21:01 +02:00
--[[
2016-06-25 14:27:20 +02:00
Licensed under GNU General Public License v2
* (c) 2016, Alexandre Terrien
2016-06-22 23:21:01 +02:00
--]]
2016-06-25 14:27:20 +02:00
local helpers = require("lain.helpers")
local json = require("lain.util.dkjson")
local pread = require("awful.util").pread
local naughty = require("naughty")
local wibox = require("wibox")
local mouse = mouse
local os = { getenv = os.getenv }
2016-06-22 23:21:01 +02:00
local setmetatable = setmetatable
-- Google Play Music Desktop infos
-- lain.widget.contrib.gpmdp
local gpmdp = {}
local function worker(args)
local args = args or {}
local timeout = args.timeout or 2
local notify = args.notify or "off"
local followmouse = args.followmouse or false
local file_location = args.file_location or
2016-06-25 14:27:20 +02:00
os.getenv("HOME") .. "/.config/Google Play Music Desktop Player/json_store/playback.json"
2016-06-22 23:21:01 +02:00
local settings = args.settings or function() end
gpmdp.widget = wibox.widget.textbox('')
gpmdp_notification_preset = {
title = "Now playing",
timeout = 6
}
2016-06-25 14:27:20 +02:00
helpers.set_map("gpmdp_current", nil)
2016-06-22 23:21:01 +02:00
function gpmdp.update()
file, err = io.open(file_location, "r")
if not file
then
2016-06-25 14:27:20 +02:00
gpm_now = { running = false, playing = false }
2016-06-22 23:21:01 +02:00
else
dict, pos, err = json.decode(file:read "*a", 1, nil)
file:close()
gpm_now = {}
2016-06-25 14:27:20 +02:00
gpm_now.artist = dict.song.artist
gpm_now.album = dict.song.album
gpm_now.title = dict.song.title
gpm_now.cover_url = dict.song.albumArt
gpm_now.playing = dict.playing
2016-06-22 23:21:01 +02:00
end
2016-06-25 14:27:20 +02:00
2016-06-22 23:21:01 +02:00
if (pread("pidof 'Google Play Music Desktop Player'") ~= '') then
gpm_now.running = true
else
gpm_now.running = false
end
2016-06-25 14:27:20 +02:00
gpmdp_notification_preset.text = string.format("%s (%s) - %s", gpm_now.artist, gpm_now.album, gpm_now.title)
2016-06-22 23:21:01 +02:00
widget = gpmdp.widget
settings()
if gpm_now.playing
then
2016-06-25 14:27:20 +02:00
if notify == "on" and gpm_now.title ~= helpers.get_map("gpmdp_current")
2016-06-22 23:21:01 +02:00
then
2016-06-25 14:27:20 +02:00
helpers.set_map("gpmdp_current", gpm_now.title)
os.execute("curl " .. gpm_now.cover_url .. " -o /tmp/gpmcover.png")
2016-06-22 23:21:01 +02:00
if followmouse then
gpmdp_notification_preset.screen = mouse.screen
end
gpmdp.id = naughty.notify({
preset = gpmdp_notification_preset,
2016-06-25 14:27:20 +02:00
icon = "/tmp/gpmcover.png",
2016-06-22 23:21:01 +02:00
replaces_id = gpmdp.id,
}).id
end
2016-06-25 14:27:20 +02:00
elseif not gpm_now.running
2016-06-22 23:21:01 +02:00
then
2016-06-25 14:27:20 +02:00
helpers.set_map("gpmdp_current", nil)
2016-06-22 23:21:01 +02:00
end
end
helpers.newtimer("gpmdp", timeout, gpmdp.update)
return setmetatable(gpmdp, { __index = gpmdp.widget })
end
return setmetatable(gpmdp, { __call = function(_, ...) return worker(...) end })