Last active
August 29, 2015 14:01
-
-
Save oleeander/2e18a1dc4737d7549e48 to your computer and use it in GitHub Desktop.
TWRC is a library giving remote access to TwistedWave.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Copyright (C) 2014 Oleander Reis <oleander@oleander.cc> | |
# | |
# This software is provided 'as-is', without any express or implied | |
# warranty. In no event will the authors be held liable for any damages | |
# arising from the use of this software. | |
# | |
# Permission is granted to anyone to use this software for any purpose, | |
# including commercial applications, and to alter it and redistribute it | |
# freely, subject to the following restrictions: | |
# | |
# 1. The origin of this software must not be misrepresented; you must not | |
# claim that you wrote the original software. If you use this software | |
# in a product, an acknowledgment in the product documentation would be | |
# appreciated but is not required. | |
# 2. Altered source versions must be plainly marked as such, and must not be | |
# misrepresented as being the original software. | |
# 3. This notice may not be removed or altered from any source distribution. | |
""" | |
TWRC | |
~~~~ | |
TWRC is a library giving remote access to TwistedWave. | |
Basic usage: | |
>>> from twrc import TWRC | |
>>> tw = TWRC("localhost", "test") | |
>>> tw.record_stop() | |
<Status cursor_position=00'00"000 file_length=2'21"32 playing=False recording=True> | |
""" | |
__title__ = "TWRC" | |
__version__ = "0.0.2" | |
__author__ = "Oleander Reis" | |
__license__ = "zlib" | |
__copyright__ = "2014 by Oleander Reis" | |
import requests | |
def generate_status(state): | |
return Status( | |
cursor_position=state["cursor"], | |
file_length=state["fileLength"], | |
playing=state["playing"], | |
recording=state["recording"] | |
) | |
class TWRC(object): | |
def __init__(self, host, client_id, ask_for_permission=True, always_auth=False): | |
self.host = host | |
self.client_id = str(client_id) | |
self.ask_for_permission = ask_for_permission | |
self.always_auth = always_auth | |
self.authenticated = False | |
self.auth() | |
def auth(self): | |
try: | |
r = requests.post("http://{host}:3000/auth".format(host=self.host), | |
data={ | |
"clientID": self.client_id, | |
"clientName": "TWRC {client_id}".format(client_id=self.client_id), | |
"isTempID": "false" if self.ask_for_permission else "true" # seriously? | |
}, | |
timeout=120) | |
except requests.exceptions.ConnectionError as e: | |
return e | |
except requests.exceptions.Timeout as e: | |
return e | |
if not self.always_auth: | |
self.authenticated = True | |
return generate_status(r.json()) | |
def eval(self, function): | |
if not self.authenticated: | |
self.auth() | |
try: | |
r = requests.post("http://{host}:3000/eval".format(host=self.host), | |
data={ | |
"clientID": self.client_id, | |
"fun": function | |
}, | |
timeout=5) | |
except requests.exceptions.ConnectionError as e: | |
return e | |
except requests.exceptions.Timeout as e: | |
return e | |
return generate_status(r.json()) | |
def get_status(self): | |
try: | |
r = requests.post("http://{host}:3000/poll".format(host=self.host), | |
data={ | |
"clientID": self.client_id, | |
"clientName": self.client_id | |
}, | |
timeout=5) | |
except requests.exceptions.ConnectionError as e: | |
return e | |
except requests.exceptions.Timeout as e: | |
return e | |
return generate_status(r.json()) | |
def play_pause(self): | |
return self.eval("remote-play-pause") | |
def record_stop(self): | |
return self.eval("remote-record") | |
def place_marker(self): | |
return self.eval("remote-place-marker") | |
def move_start(self): | |
return self.eval("remote-move-start") | |
def move_end(self): | |
return self.eval("remote-move-end") | |
class Status(object): | |
def __init__(self, cursor_position=None, file_length=None, playing=None, recording=None): | |
self._cursor_position = cursor_position | |
self._file_length = file_length | |
self._playing = playing | |
self._recording = recording | |
def __repr__(self): | |
return "<{cls} cursor_position={cursor_position} file_length={file_length} playing={playing} recording={recording}>".format( | |
cls=type(self).__name__, | |
cursor_position=self._cursor_position, | |
file_length=self._file_length, | |
playing=self._playing, | |
recording=self._recording | |
) | |
@property | |
def cursor_position(self): | |
return self._cursor_position | |
@property | |
def file_length(self): | |
return self._file_length | |
@property | |
def playing(self): | |
return self._playing | |
@property | |
def recording(self): | |
return self._recording | |
if __name__ == "__main__": | |
tw = TWRC("localhost", "test") | |
print(tw.record_stop()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment