Added json response to ls command and removed print functions.

This commit is contained in:
2018-08-12 23:16:03 +02:00
parent bb5da9c455
commit e22de16d8c
2 changed files with 14 additions and 10 deletions

View File

@@ -31,6 +31,7 @@ import os
import sys import sys
import re import re
import signal import signal
import json
import socket import socket
import logging import logging
import logging.config import logging.config
@@ -240,7 +241,7 @@ def signal_handler(signal, frame):
logger.info('\nGood bye!') logger.info('\nGood bye!')
sys.exit(0) sys.exit(0)
def main(arg): def main(arg=None):
""" """
Main function, parse the input Main function, parse the input
""" """
@@ -284,7 +285,8 @@ def main(arg):
elif arguments['progress']: elif arguments['progress']:
logger.info('Progress cmd selected.') logger.info('Progress cmd selected.')
response = deluge.progress() response = deluge.progress()
[ pprint(t.toJSON()) for t in response ] print(response)
# [ pprint(t.toJSON()) for t in response ]
return response return response
elif arguments['get']: elif arguments['get']:
@@ -296,8 +298,9 @@ def main(arg):
elif arguments['ls']: elif arguments['ls']:
logger.info('List cmd selected') logger.info('List cmd selected')
response = deluge.get_all(_filter=_filter) response = deluge.get_all(_filter=_filter)
[ pprint(t.toJSON()) for t in response ] response = [t.toJSON() for t in response]
return response # pprint(response)
return json.dumps(response)
elif arguments['toggle']: elif arguments['toggle']:
logger.info('Toggling id: {}'.format(_id)) logger.info('Toggling id: {}'.format(_id))

View File

@@ -2,6 +2,7 @@ import asyncio
import datetime import datetime
import random import random
import websockets import websockets
import json
import deluge_cli import deluge_cli
@@ -22,18 +23,18 @@ async def time(websocket, path):
async def deluge(websocket, path): async def deluge(websocket, path):
last_msg = None last_msg = []
downloading = deluge_cli.main(['ls', '--downloading'])
while True: while True:
if downloading != last_msg: downloading = deluge_cli.main(['progress'])
await websocket.send(str(downloading))
if downloading is not last_msg:
await websocket.send(json.dumps(downloading))
print('sending response') print('sending response')
last_msg = downloading last_msg = downloading
await asyncio.sleep(1) await asyncio.sleep(1)
serve_hello = websockets.serve(hello, '0.0.0.0', 8765) serve_hello = websockets.serve(hello, '0.0.0.0', 8765)
# serve_time = websockets.serve(time, '0.0.0.0', 5678)
serve_deluge = websockets.serve(deluge, '0.0.0.0', 5678) serve_deluge = websockets.serve(deluge, '0.0.0.0', 5678)
asyncio.get_event_loop().run_until_complete(serve_hello) asyncio.get_event_loop().run_until_complete(serve_hello)