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
|
#coding:utf8
|
||||||
|
import thread
|
||||||
import sys
|
import sys, os
|
||||||
|
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
|
|
||||||
from syncplay import client
|
from syncplay import client
|
||||||
from syncplay.players import mpc
|
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__':
|
if __name__ == '__main__':
|
||||||
args = sys.argv[1:]
|
args = sys.argv[1:]
|
||||||
host = args.pop(0)
|
host = args.pop(0)
|
||||||
name = args.pop(0)
|
name = args.pop(0)
|
||||||
if ':' in host:
|
if ':' in host:
|
||||||
host, port = host.split(':', 1)
|
host, port = host.split(':', 1)
|
||||||
port = int(port)
|
port = int(port)
|
||||||
else:
|
else:
|
||||||
port = 8999
|
port = 8999
|
||||||
|
|
||||||
manager = client.Manager(host, port, name, lambda m: mpc.run_mpc(m))
|
manager = client.Manager(host, port, name, lambda m: mpc.run_mpc(m))
|
||||||
|
thread.start_new_thread(stdin_thread, (manager,))
|
||||||
manager.start()
|
manager.start()
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#coding:utf8
|
#coding:utf8
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
import re
|
||||||
|
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
from twisted.internet.protocol import ClientFactory
|
from twisted.internet.protocol import ClientFactory
|
||||||
@ -169,7 +170,8 @@ class Manager(object):
|
|||||||
self.last_player_update = None
|
self.last_player_update = None
|
||||||
self.player_speed_fix = False
|
self.player_speed_fix = False
|
||||||
self.player_filename = None
|
self.player_filename = None
|
||||||
|
self.player_position_before_last_seek = 0
|
||||||
|
|
||||||
self.seek_sent_wait = False
|
self.seek_sent_wait = False
|
||||||
self.status_ask_sent = 0
|
self.status_ask_sent = 0
|
||||||
self.status_ask_received = 0
|
self.status_ask_received = 0
|
||||||
@ -278,7 +280,23 @@ class Manager(object):
|
|||||||
if self.protocol and self.player_filename:
|
if self.protocol and self.player_filename:
|
||||||
self.protocol.send_playing(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):
|
def update_player_status(self, paused, position):
|
||||||
self.status_ask_received += 1
|
self.status_ask_received += 1
|
||||||
if self.status_ask_received < self.status_ask_sent:
|
if self.status_ask_received < self.status_ask_sent:
|
||||||
@ -376,6 +394,7 @@ class Manager(object):
|
|||||||
self.global_position = position
|
self.global_position = position
|
||||||
self.last_global_update = curtime
|
self.last_global_update = curtime
|
||||||
if self.player:
|
if self.player:
|
||||||
|
self.player_position_before_last_seek = self.player_position
|
||||||
self.player.set_position(position)
|
self.player.set_position(position)
|
||||||
self.ask_player()
|
self.ask_player()
|
||||||
|
|
||||||
|
|||||||
@ -72,7 +72,8 @@ class MPCHCPlayer(object):
|
|||||||
|
|
||||||
self.pinged = False
|
self.pinged = False
|
||||||
self.tmp_filename = None
|
self.tmp_filename = None
|
||||||
|
self.tmp_position = None
|
||||||
|
|
||||||
self.agent = Agent(reactor)
|
self.agent = Agent(reactor)
|
||||||
|
|
||||||
def drop(self):
|
def drop(self):
|
||||||
@ -98,14 +99,17 @@ class MPCHCPlayer(object):
|
|||||||
|
|
||||||
def status_response(self, status, headers, body):
|
def status_response(self, status, headers, body):
|
||||||
m = RE_MPC_STATUS.match(body)
|
m = RE_MPC_STATUS.match(body)
|
||||||
if not m:
|
if m:
|
||||||
return
|
filename, paused, position = m.group(1), m.group(2), m.group(3)
|
||||||
filename, paused, position = m.group(1), m.group(2), m.group(3)
|
paused = PLAYING_STATUSES.get(paused)
|
||||||
|
else:
|
||||||
paused = PLAYING_STATUSES.get(paused)
|
filename, paused, position = self.tmp_filename, True, self.tmp_position
|
||||||
if paused is None:
|
if paused is None:
|
||||||
return
|
paused = True #assume stopped or changing episodes!
|
||||||
position = float(position)/1000
|
position = self.tmp_position
|
||||||
|
else:
|
||||||
|
position = float(position)/1000
|
||||||
|
self.tmp_position = position
|
||||||
if self.pinged:
|
if self.pinged:
|
||||||
self.manager.update_player_status(paused, position)
|
self.manager.update_player_status(paused, position)
|
||||||
else:
|
else:
|
||||||
@ -138,7 +142,6 @@ class MPCHCPlayer(object):
|
|||||||
print 'Failed to connect to MPC-HC web interface'
|
print 'Failed to connect to MPC-HC web interface'
|
||||||
self.manager.stop()
|
self.manager.stop()
|
||||||
|
|
||||||
|
|
||||||
def run_mpc(manager, host=None):
|
def run_mpc(manager, host=None):
|
||||||
mpc = MPCHCPlayer(manager, host)
|
mpc = MPCHCPlayer(manager, host)
|
||||||
mpc.make_ping()
|
mpc.make_ping()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user