Dodana obsuga komend z stdin
This commit is contained in:
parent
1705647ddd
commit
4d48787d6c
22
sync_mpc.py
22
sync_mpc.py
@ -1,22 +1,32 @@
|
||||
#coding:utf8
|
||||
|
||||
import sys
|
||||
import thread
|
||||
import sys, os
|
||||
|
||||
from twisted.internet import reactor
|
||||
|
||||
from syncplay import client
|
||||
from syncplay.players import mpc
|
||||
|
||||
def stdin_thread(sock):
|
||||
try:
|
||||
fd = sys.stdin.fileno()
|
||||
while True:
|
||||
data = os.read(fd, 1024)
|
||||
if not data:
|
||||
break
|
||||
sock.execute_command(data.rstrip('\n\r'))
|
||||
except:
|
||||
pass
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = sys.argv[1:]
|
||||
host = args.pop(0)
|
||||
name = args.pop(0)
|
||||
host = args.pop(0)
|
||||
name = args.pop(0)
|
||||
if ':' in host:
|
||||
host, port = host.split(':', 1)
|
||||
port = int(port)
|
||||
else:
|
||||
port = 8999
|
||||
|
||||
manager = client.Manager(host, port, name, lambda m: mpc.run_mpc(m))
|
||||
thread.start_new_thread(stdin_thread, (manager,))
|
||||
manager.start()
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#coding:utf8
|
||||
|
||||
import time
|
||||
import re
|
||||
|
||||
from twisted.internet import reactor
|
||||
from twisted.internet.protocol import ClientFactory
|
||||
@ -169,7 +170,8 @@ class Manager(object):
|
||||
self.last_player_update = None
|
||||
self.player_speed_fix = False
|
||||
self.player_filename = None
|
||||
|
||||
self.player_position_before_last_seek = 0
|
||||
|
||||
self.seek_sent_wait = False
|
||||
self.status_ask_sent = 0
|
||||
self.status_ask_received = 0
|
||||
@ -278,7 +280,23 @@ class Manager(object):
|
||||
if self.protocol and self.player_filename:
|
||||
self.protocol.send_playing(self.player_filename)
|
||||
|
||||
|
||||
def execute_command(self, data):
|
||||
RE_SEEK = re.compile("^s ?(\d+)(:(\d{1,2}))?$")
|
||||
m = RE_SEEK.match(data)
|
||||
if m :
|
||||
minutes, seconds = m.group(1), m.group(3)
|
||||
minutes = int(minutes) * 60
|
||||
if seconds <> None:
|
||||
seconds = int(seconds)
|
||||
else:
|
||||
seconds = 0
|
||||
self.player_position_before_last_seek = self.player_position
|
||||
self.counter += 1
|
||||
self.protocol.send_seek(self.counter, time.time(), minutes+seconds)
|
||||
elif data == "r":
|
||||
self.counter += 1
|
||||
self.protocol.send_seek(self.counter, time.time(), self.player_position_before_last_seek)
|
||||
|
||||
def update_player_status(self, paused, position):
|
||||
self.status_ask_received += 1
|
||||
if self.status_ask_received < self.status_ask_sent:
|
||||
@ -376,6 +394,7 @@ class Manager(object):
|
||||
self.global_position = position
|
||||
self.last_global_update = curtime
|
||||
if self.player:
|
||||
self.player_position_before_last_seek = self.player_position
|
||||
self.player.set_position(position)
|
||||
self.ask_player()
|
||||
|
||||
|
||||
@ -72,7 +72,8 @@ class MPCHCPlayer(object):
|
||||
|
||||
self.pinged = False
|
||||
self.tmp_filename = None
|
||||
|
||||
self.tmp_position = None
|
||||
|
||||
self.agent = Agent(reactor)
|
||||
|
||||
def drop(self):
|
||||
@ -98,14 +99,17 @@ class MPCHCPlayer(object):
|
||||
|
||||
def status_response(self, status, headers, body):
|
||||
m = RE_MPC_STATUS.match(body)
|
||||
if not m:
|
||||
return
|
||||
filename, paused, position = m.group(1), m.group(2), m.group(3)
|
||||
|
||||
paused = PLAYING_STATUSES.get(paused)
|
||||
if m:
|
||||
filename, paused, position = m.group(1), m.group(2), m.group(3)
|
||||
paused = PLAYING_STATUSES.get(paused)
|
||||
else:
|
||||
filename, paused, position = self.tmp_filename, True, self.tmp_position
|
||||
if paused is None:
|
||||
return
|
||||
position = float(position)/1000
|
||||
paused = True #assume stopped or changing episodes!
|
||||
position = self.tmp_position
|
||||
else:
|
||||
position = float(position)/1000
|
||||
self.tmp_position = position
|
||||
if self.pinged:
|
||||
self.manager.update_player_status(paused, position)
|
||||
else:
|
||||
@ -138,7 +142,6 @@ class MPCHCPlayer(object):
|
||||
print 'Failed to connect to MPC-HC web interface'
|
||||
self.manager.stop()
|
||||
|
||||
|
||||
def run_mpc(manager, host=None):
|
||||
mpc = MPCHCPlayer(manager, host)
|
||||
mpc.make_ping()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user