Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions mpv-discordRPC/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ local function main()
if string.match(url, "^https?://.*") ~= nil and o.hide_url == "no" then
largeImageKey = "mpv_stream"
largeImageText = url
elseif o.hide_url == "yes" then
largeImageKey = "mpv_stream"
-- will use mpv_version set previously
elseif o.hide_url == "yes" then
largeImageKey = "mpv_stream"
-- will use mpv_version set previously
end
-- checking site: YouTube, Crunchyroll, SoundCloud, LISTEN.moe
if string.match(url, "www.youtube.com/watch%?v=([a-zA-Z0-9-_]+)&?.*$") ~= nil or string.match(url, "youtu.be/([a-zA-Z0-9-_]+)&?.*$") ~= nil then
Expand Down Expand Up @@ -234,25 +234,22 @@ local function main()
RPC.shutdown()
end
elseif tostring(o.rpc_wrapper) == "python-pypresence" then
-- set python path
local pythonPath
local lib
pythonPath = mp.get_script_directory() .. "/" .. o.rpc_wrapper .. ".py"
lib = package.cpath:match("%p[\\|/]?%p(%a+)")
if lib == "dll" then
pythonPath = pythonPath:gsub("/","\\\\")
end
-- run Rich Presence with pypresence
local todo = idle and "idle" or "not-idle"
local command = ('python "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"'):format(pythonPath, todo, presence.state, presence.details, math.floor(startTime), math.floor(timeUp), presence.largeImageKey, presence.largeImageText, presence.smallImageKey, presence.smallImageText, o.periodic_timer)
local command = ('%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s'):format(pythonPath, todo, presence.state, presence.details, math.floor(startTime), math.floor(timeUp), presence.largeImageKey, presence.largeImageText, presence.smallImageKey, presence.smallImageText, o.periodic_timer)
mp.register_event('shutdown', function()
todo = "shutdown"
command = ('python "%s" "%s"'):format(pythonPath, todo)
io.popen(command)
command = ('%s\n%s'):format(pythonPath, todo)
file = io.open('/dev/shm/RPC.txt',"w")
io.popen('')
file:write(command)
file:close()
os.exit()
end)
if o.active == "yes" then
io.popen(command)
file = io.open('/dev/shm/RPC.txt',"w")
file:write(command)
file:close()
end
end
end
Expand Down Expand Up @@ -281,5 +278,14 @@ mp.add_key_binding(o.key_toggle, "active-toggle", function()
end,
{repeatable=false})

-- set python path
local lib
local pythonPath
pythonPath = mp.get_script_directory() .. "/" .. o.rpc_wrapper .. ".py"
lib = package.cpath:match("%p[\\|/]?%p(%a+)")
if lib == "dll" then
pythonPath = pythonPath:gsub("/","\\\\")
end
-- run `main` function
io.popen('python3 "%s" &'):format(pythonPath)
mp.add_periodic_timer(o.periodic_timer, main)
70 changes: 28 additions & 42 deletions mpv-discordRPC/python-pypresence.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
#!/usr/bin/env python3

import signal
from pathlib import Path
import sys
import time
import os
import pypresence

home = str(Path.home())
print(home)
print("start")
client_id = "448016723057049601"
pid = 9999

todo = str(sys.argv[1]) if len(sys.argv) > 1 else "shutdown"
state = str(sys.argv[2]) if len(sys.argv) > 2 else "state = (Idle)"
details = str(sys.argv[3]) if len(sys.argv) > 3 else "details = No file"
start = int(sys.argv[4]) if len(sys.argv) > 4 else int(time.time())
end = int(sys.argv[5]) if len(sys.argv) > 5 else int(time.time() + 60)
large_image = str(sys.argv[6]) if len(sys.argv) > 6 else "mpv"
large_text = str(sys.argv[7]) if len(sys.argv) > 7 else "large_text = mpv Media Player"
small_image = str(sys.argv[8]) if len(sys.argv) > 8 else "player_stop"
small_text = str(sys.argv[9]) if len(sys.argv) > 9 else "small_text = Idle"
periodic_timer = int(sys.argv[10]) if len(sys.argv) > 10 else 15

TimeoutHandler = Exception

def timeout_handler(num, stack):
raise TimeoutHandler

try:
RPC = pypresence.Presence(client_id)
RPC.connect()
#print("pypresence: RPC connected")
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(periodic_timer)
RPC = pypresence.Presence(client_id)
RPC.connect()
print("pypresence: RPC connected")
while True:
time.sleep(4)
pid = 9999
file = open("/dev/shm/RPC.txt").read().splitlines()
todo = str(file[1]) if len(file) > 1 else "shutdown"
state = str(file[2]) if len(file) > 2 else "state = (Idle)"
details = str(file[3]) if len(file) > 3 else "details = No file"
start = int(file[4]) if len(file) > 4 else int(time.time())
end = int(file[5]) if len(file) > 5 else int(time.time() + 60)
large_image = str(file[6]) if len(file) > 6 else "mpv"
large_text = str(file[7]) if len(file) > 7 else "large_text = mpv Media Player"
small_image = str(file[8]) if len(file) > 8 else "player_stop"
small_text = str(file[9]) if len(file) > 9 else "small_text = Idle"
periodic_timer = int(file[10]) if len(file) > 10 else 15
if todo == "shutdown":
RPC.clear()
#print("pypresence: RPC cleared")
print("pypresence: RPC cleared")
RPC.close()
#print("pypresence: RPC closed")
print("pypresence: RPC closed")
os.system('rm -rf "/dev/shm/RPC.txt"')
sys.exit()
elif todo == "idle":
RPC.update(state=state, details=details, start=start, large_image=large_image, large_text=large_text, small_image=small_image, small_text=small_text)
#print("pypresence: RPC updated.")
time.sleep(periodic_timer)
print("pypresence: RPC updated.")
elif todo == "not-idle":
RPC.update(state=state, details=details, end=end, large_image=large_image, large_text=large_text, small_image=small_image, small_text=small_text)
#print("pypresence: RPC updated")
time.sleep(periodic_timer)
except (KeyboardInterrupt, SystemExit):
#print(" interupt: KeyboardInterrupt|SystemExit)")
sys.exit()
except (BrokenPipeError, IOError):
#print(" interupt: BrokenPipeError|IOError")
sys.exit()
except (TimeoutHandler):
#print(" interupt: TimeoutHandler")
sys.exit()
except:
sys.exit()
print("pypresence: RPC updated")