lain/widgets/contrib/gpmdp.lua

99 lines
3.1 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
--]]
local helpers = require("lain.helpers")
local json = require("lain.util.dkjson")
2016-06-22 23:21:01 +02:00
local focused = require("awful.screen").focused
local pread = require("awful.util").pread
local naughty = require("naughty")
local wibox = require("wibox")
local next = next
local os = { getenv = os.getenv }
2016-06-22 23:21:01 +02:00
local setmetatable = setmetatable
local table = table
2016-06-22 23:21:01 +02:00
-- 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 followtag = args.followtag or false
2016-06-22 23:21:01 +02:00
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()
local filelines = helpers.lines_from(file_location)
if not next(filelines)
2016-06-22 23:21:01 +02:00
then
local gpm_now = { running = false, playing = false }
2016-06-22 23:21:01 +02:00
else
dict, pos, err = json.decode(table.concat(filelines), 1, nil)
local 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
if pread("pidof 'Google Play Music Desktop Player'") ~= '' then
2016-06-22 23:21:01 +02:00
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(string.format("curl %d -o /tmp/gpmcover.png", gpm_now.cover_url))
2016-06-22 23:21:01 +02:00
if followtag then
gpmdp_notification_preset.screen = focused()
2016-06-22 23:21:01 +02:00
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 })