First stage of MPC support refactoring
This commit is contained in:
parent
204317762d
commit
8ed7f061bc
@ -17,8 +17,6 @@ if __name__ == '__main__':
|
|||||||
else:
|
else:
|
||||||
port = 8999
|
port = 8999
|
||||||
|
|
||||||
manager = client.Manager(host, port, name)
|
manager = client.Manager(host, port, name, lambda: mpc.MPCHCPlayer(manager))
|
||||||
manager.start()
|
manager.start()
|
||||||
player = mpc.MPCHCPlayer(manager)
|
|
||||||
reactor.run()
|
|
||||||
|
|
||||||
|
|||||||
@ -5,11 +5,15 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
|
|
||||||
|
from twisted.internet.defer import succeed
|
||||||
from twisted.internet.protocol import (
|
from twisted.internet.protocol import (
|
||||||
ProcessProtocol,
|
ProcessProtocol,
|
||||||
Protocol,
|
Protocol,
|
||||||
)
|
)
|
||||||
from twisted.protocols.basic import LineReceiver
|
from twisted.protocols.basic import LineReceiver
|
||||||
|
from twisted.web.iweb import IBodyProducer
|
||||||
|
|
||||||
|
from zope.interface import implements
|
||||||
|
|
||||||
class CommandProtocol(LineReceiver):
|
class CommandProtocol(LineReceiver):
|
||||||
states = None
|
states = None
|
||||||
@ -87,22 +91,42 @@ class LineProcessProtocol(ProcessProtocol):
|
|||||||
for line in lines:
|
for line in lines:
|
||||||
self.transport.write(line+'\n')
|
self.transport.write(line+'\n')
|
||||||
|
|
||||||
|
|
||||||
|
class BodyProducer(object):
|
||||||
|
implements(IBodyProducer)
|
||||||
|
|
||||||
|
def __init__(self, body):
|
||||||
|
self.body = body
|
||||||
|
self.length = len(body)
|
||||||
|
|
||||||
|
def startProducing(self, consumer):
|
||||||
|
consumer.write(self.body)
|
||||||
|
return succeed(None)
|
||||||
|
|
||||||
|
def pauseProducing(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def stopProducing(self):
|
||||||
|
pass
|
||||||
|
|
||||||
class BodyGetter(Protocol):
|
class BodyGetter(Protocol):
|
||||||
def __init__(self, length, callback):
|
def __init__(self, length, callback):
|
||||||
self.length = length
|
self.length = length
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
self.body = StringIO()
|
self.body = StringIO()
|
||||||
|
|
||||||
def dataReceived(self, data):
|
def dataReceived(self, data):
|
||||||
if self.length > 0:
|
if self.length > 0:
|
||||||
data = data[:self.length]
|
data = data[:self.length]
|
||||||
self.body.write(data)
|
self.body.write(data)
|
||||||
self.length -= len(length)
|
self.length -= len(length)
|
||||||
|
|
||||||
def connectionLost(self, reason):
|
def connectionLost(self, reason):
|
||||||
self.callback(self.body.getvalue())
|
self.callback(self.body.getvalue())
|
||||||
|
|
||||||
|
def null_response_handler(status, headers, body):
|
||||||
|
pass
|
||||||
|
|
||||||
def handle_response(callback):
|
def handle_response(callback):
|
||||||
def defer_callback(response):
|
def defer_callback(response):
|
||||||
status = response.code
|
status = response.code
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
#coding:utf8
|
#coding:utf8
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
from twisted.internet.defer import Deferred, succeed
|
|
||||||
from twisted.web.client import Agent
|
from twisted.web.client import Agent
|
||||||
from twisted.web.http_headers import Headers
|
from twisted.web.http_headers import Headers
|
||||||
|
|
||||||
from zope.interface import implements
|
|
||||||
from twisted.web.iweb import IBodyProducer
|
|
||||||
|
|
||||||
from ..network_utils import handle_response
|
from ..network_utils import (
|
||||||
|
BodyProducer,
|
||||||
|
handle_response,
|
||||||
|
null_response_handler,
|
||||||
|
)
|
||||||
|
|
||||||
#RE_MPC_STATUS = re.compile("^OnStatus\('(.+)', '(Paused|Playing)', (\d+), '\d{2}:\d{2}:\d{2}', \d+, '\d{2}:\d{2}:\d{2}', \d+, \d+, '.+'\)$")
|
#RE_MPC_STATUS = re.compile("^OnStatus\('(.+)', '(Paused|Playing)', (\d+), '\d{2}:\d{2}:\d{2}', \d+, '\d{2}:\d{2}:\d{2}', \d+, \d+, '.+'\)$")
|
||||||
RE_MPC_STATUS = re.compile(r"^OnStatus\('((?:[^']*(?:\\\\)*\\')*[^']*)', '(.+?)', (\d+),")
|
RE_MPC_STATUS = re.compile(r"^OnStatus\('((?:[^']*(?:\\\\)*\\')*[^']*)', '(.+?)', (\d+),")
|
||||||
@ -64,88 +64,60 @@ PLAYING_STATUSES = {
|
|||||||
'Duraklatıldı': True,
|
'Duraklatıldı': True,
|
||||||
'Пауза': True,
|
'Пауза': True,
|
||||||
}
|
}
|
||||||
|
|
||||||
class MPCHCPlayer(object):
|
class MPCHCPlayer(object):
|
||||||
def __init__(self, manager):
|
def __init__(self, manager, host = None):
|
||||||
self.manager = manager
|
self.manager = manager
|
||||||
|
self.host = 'localhost:13579'
|
||||||
|
|
||||||
|
self.filename = None # To be moved to Manager
|
||||||
|
|
||||||
manager.player = self
|
manager.player = self
|
||||||
|
self.agent = Agent(reactor)
|
||||||
def send_set_paused(self, value):
|
|
||||||
self.set_property('Paused', value)
|
|
||||||
|
|
||||||
def send_get_paused(self):
|
def set_paused(self, value):
|
||||||
self.get_property('Paused')
|
self.send_post_request('wm_command=%d' % (888 if value else 887))
|
||||||
|
|
||||||
def send_set_position(self, value):
|
|
||||||
self.set_property('Position', '%d'%(value*1000))
|
|
||||||
|
|
||||||
def send_get_position(self):
|
def set_position(self, value):
|
||||||
self.get_property('Position')
|
value = int(value*1000)
|
||||||
|
seconds, mseconds = divmod(value, 1000)
|
||||||
|
minutes, seconds = divmod(seconds, 60)
|
||||||
|
hours, minutes = divmod(minutes, 60)
|
||||||
|
self.send_post_request('wm_command=-1&position=%d.%d.%d.%d' % (
|
||||||
|
hours, minutes, seconds, mseconds
|
||||||
|
))
|
||||||
|
|
||||||
def send_set_speed(self, value):
|
self.send_post_request(body)
|
||||||
pass
|
|
||||||
|
|
||||||
def send_get_speed(self):
|
def status_response(self, status, headers, body):
|
||||||
pass
|
m = RE_MPC_STATUS.match(body)
|
||||||
|
if not m:
|
||||||
def set_property(self, name, value):
|
return
|
||||||
requestData = {
|
filename, paused, position = m.group(1), m.group(2), m.group(3)
|
||||||
'Paused': lambda value: 'wm_command=888&null=0' if value else 'wm_command=887&null=0',
|
|
||||||
'Position': lambda value: "wm_command=-1&position="+ '%d.%d.%d.%d' % ((int(value)/3600000), (int(value)/60000)%60, (int(value)/1000)%60, int(value)%1000)
|
|
||||||
}[name](value)
|
|
||||||
|
|
||||||
body = StringBodyProducer(requestData)
|
paused = PLAYING_STATUSES.get(paused)
|
||||||
self.sendPostRequest(body)
|
if paused is None:
|
||||||
|
return
|
||||||
def get_property(self, propertyName):
|
if self.filename is None:
|
||||||
agent = Agent(reactor)
|
self.filename = filename
|
||||||
request = agent.request(
|
position = float(position)/1000
|
||||||
|
self.manager.update_player_status(paused, position)
|
||||||
|
|
||||||
|
def ask_for_status(self, propertyName):
|
||||||
|
request = self.agent.request(
|
||||||
'GET',
|
'GET',
|
||||||
'http://localhost:13579/status.html',
|
'http://localhost:13579/status.html',
|
||||||
Headers(),
|
Headers(),
|
||||||
None)
|
None,
|
||||||
|
)
|
||||||
def cbRequest(status, headers, body):
|
request.addCallback(handle_response(self.status_response))
|
||||||
m = RE_MPC_STATUS.match(body)
|
|
||||||
if not m:
|
|
||||||
return
|
|
||||||
fileName, playerStatus, currentTime = m.group(1), m.group(2), m.group(3)
|
|
||||||
playerStatus = PLAYING_STATUSES.get(playerStatus)
|
|
||||||
if playerStatus is None:
|
|
||||||
return
|
|
||||||
if(propertyName == "Paused"):
|
|
||||||
self.manager.update_player_paused(playerStatus)
|
|
||||||
if(propertyName == "Position"):
|
|
||||||
self.manager.update_player_position(float(currentTime)/1000.0)
|
|
||||||
|
|
||||||
request.addCallback(handle_response(cbRequest))
|
|
||||||
|
|
||||||
def sendPostRequest(self, body):
|
def send_post_request(self, body):
|
||||||
agent = Agent(reactor)
|
request = self.agent.request(
|
||||||
request = agent.request(
|
|
||||||
'POST',
|
'POST',
|
||||||
'http://localhost:13579/command.html',
|
'http://%s/command.html' % self.host,
|
||||||
Headers({'Content-Type': ['application/x-www-form-urlencoded']}),
|
Headers({'Content-Type': ['application/x-www-form-urlencoded']}),
|
||||||
body)
|
BodyProducer(body),
|
||||||
|
)
|
||||||
def cbRequest(ignored):
|
request.addCallback(handle_response(null_response_handler))
|
||||||
return
|
|
||||||
request.addCallback(cbRequest)
|
|
||||||
|
|
||||||
class StringBodyProducer(object):
|
|
||||||
implements(IBodyProducer)
|
|
||||||
|
|
||||||
def __init__(self, body):
|
|
||||||
self.body = body
|
|
||||||
self.length = len(body)
|
|
||||||
|
|
||||||
def startProducing(self, consumer):
|
|
||||||
consumer.write(self.body)
|
|
||||||
return succeed(None)
|
|
||||||
|
|
||||||
def pauseProducing(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def stopProducing(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user