Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
21 changes: 11 additions & 10 deletions mpv-discordRPC/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ local o = {
-- value <= 15 second, because discord-rpc updates every 15 seconds.
playlist_info = "yes",
-- Valid value to set `playlist_info`: (yes|no)
hide_url = "no",
-- Valid value to set `hide_url`: (yes|no)
loop_info = "yes",
-- Valid value to set `loop_info`: (yes|no)
cover_art = "yes",
Expand Down Expand Up @@ -171,12 +169,9 @@ local function main()
local stream = mp.get_property("stream-path")
if url ~= nil then
-- checking protocol: http, https
if string.match(url, "^https?://.*") ~= nil and o.hide_url == "no" then
if string.match(url, "^https?://.*") ~= nil then
largeImageKey = "mpv_stream"
largeImageText = url
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 @@ -244,15 +239,20 @@ local function main()
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/testfile.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/testfile.txt',"w")
file:write(command)
file:close()
end
end
end
Expand Down Expand Up @@ -282,4 +282,5 @@ mp.add_key_binding(o.key_toggle, "active-toggle", function()
{repeatable=false})

-- run `main` function
io.popen('python3 "/media/isaac/Games/Home/isaac/.config/mpv/scripts/mpv-discordRPC/python-pypresence.py" &')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use hard coded file path.
just use python instead of python3 explicitly

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I used python instead of python3 it gave me errors about pypresense not existing, and when I installed it with pip it gave me invalid syntax errors. I guess that could just be a Linux thing

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/testfile.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/testfile.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")