I really should have put this in here ages ago. Also maybe someday I'll rewrite it in lua.

This commit is contained in:
2025-12-06 01:56:58 +11:00
parent 5d175321bd
commit ef5681af1e
2 changed files with 84 additions and 0 deletions

View File

@@ -37,6 +37,7 @@ source ~/tinker/tinyscripts/creds_getter.sh
function lyrics { python ~/tinker/lyrics-fetcher/getlyrics.py $@; }
function pulsemagix { ~/pulsemagix.sh $*; }
alias search=~/tinker/tinyscripts/search.sh
alias mpvc=~/tinker/tinyscripts/mpvcontrol.py
alias gitmk="git push -o repo.private=false -u master master"
alias wpfx=~/tinker/tinyscripts/wpfx.sh
function update { scp -r * root@jasmine:"$(cat .update)"; }

83
mpvcontrol.py Executable file
View File

@@ -0,0 +1,83 @@
#!/bin/python
import socket
sock=socket.socket
import json as j
import random as ra
seed=round(ra.random()*(2**20)-2**19)*3000
root='/home/wisknort/Music/'
snipper=len(root)
def _connect():
s=sock(family=socket.AF_UNIX)
s.connect('/tmp/musicbot')
return s
def get_req(command,conn=None):
global seed
idd=seed
seed+=1
conn.send(j.dumps({"command":command,"request_id":idd}).encode('utf-8')+b'\n')
# This is the hard part, but I think I know how to do it.
attempt=''
while attempt=='':
curr=conn.recv(1)
out=[]
while curr!=b'\n':
out.append(curr)
curr=conn.recv(1)
stash=b''.join(out).decode('utf-8')
try: attempt=j.loads(stash)
except j.decoder.JSONDecodeError: continue # Don't need half-baked results
if 'request_id' not in attempt or attempt['request_id']!=idd: attempt='' # Wrong result, try again. It must be after we sent the command. But how far...
return attempt
def pick(name=''):
s=_connect()
tracks=get_req(['get_property','playlist'],s)['data']
position=0
for n in range(len(tracks)): tracks[n]['id']=n # This is so we can accurately reference them later...
# Also that field wasn't doing anything useful anyway.
now_playing=get_req(['get_property','playlist-playing-pos'],s)['data']
if name: tracks=list(filter(lambda x:name.lower() in x['filename'].lower(),tracks))
else:
position=now_playing-now_playing%10
sel=''
p=True
while True:
if p:
for n in range(min(len(tracks)-position,10)):
print(f'{n}:',('> ' if tracks[position+n]['id']==now_playing else '')+(tracks[position+n]['filename'][snipper:]))
sel=input('Play which track? Or (n)ext/(p)rev page or (q)uit ')
if sel in ['p','n']:
position+={'p':-10,'n':10}[sel]
if position>len(tracks): position=0
if position<0: position=len(tracks)-len(tracks)%10
p=True
continue
if sel=='q': return
try: sel=int(sel); break
except ValueError: p=False
s.send(j.dumps({"command":["playlist-play-index",tracks[position+sel]['id']]}).encode('utf-8')+b'\n')
def repeat(*args):
s=_connect()
s.send(b'{"command":["cycle-values","loop","inf","no"]}\n')
def seq(*args):
s=_connect()
peth='/home/wisknort/.config/mpv/scripts-storage/shfc'
with open(peth) as b:
swop=int(b.read()[0])
if swop: s.send(b'{"command":["playlist-unshuffle"]}\n')
else: s.send(b'{"command":["playlist-shuffle"]}\n')
swop=(swop+1)%2
with open(peth,'w') as b:
b.write(str(swop))
if __name__=='__main__':
import sys
action=sys.argv[1]
print('executing',action)
{'pick':pick,'rep':repeat,'seq':seq}[action](' '.join(sys.argv[2:]))