mirror of
				https://github.com/KevinMidboe/delugeClient.git
				synced 2025-10-29 12:00:13 +00:00 
			
		
		
		
	Terminate with exit code. Also structured func resp & print to caller of main.
This commit is contained in:
		| @@ -66,12 +66,13 @@ def main(): | ||||
|   # Set logging level for streamHandler | ||||
|   if arguments['--debug']: | ||||
|     ch.setLevel(logging.DEBUG) | ||||
|   elif arguments['--info']: | ||||
|     ch.setLevel(logging.INFO) | ||||
|   elif arguments['--warning']: | ||||
|     ch.setLevel(logging.WARNING) | ||||
|   elif arguments['--error']: | ||||
|     ch.setLevel(logging.ERROR) | ||||
|  | ||||
|   logger.info('Deluge client') | ||||
|   logger.debug(arguments) | ||||
|  | ||||
|   # Get config settings | ||||
| @@ -85,16 +86,16 @@ def main(): | ||||
|   response = None | ||||
|  | ||||
|   if arguments['add']: | ||||
|     logger.info('Add cmd selected with link {}'.format(magnet)) | ||||
|     response = deluge.add(magnet) | ||||
|  | ||||
|     if response is not None: | ||||
|       logger.info('Successfully added torrent.\nResponse from deluge: {}'.format(response)) | ||||
|       msg = 'Successfully added torrent with id: {}'.format(response) | ||||
|       logger.info(msg) | ||||
|     else: | ||||
|       logger.warning('Add response returned empty: {}'.format(response)) | ||||
|  | ||||
|   elif arguments['search']: | ||||
|     logger.info('Search cmd selected for query: {}'.format(query)) | ||||
|     logger.debug('Search cmd selected for query: {}'.format(query)) | ||||
|     response = deluge.search(query) | ||||
|     if response is not None or response != '[]': | ||||
|       logger.info('Search found {} torrents'.format(len(response))) | ||||
| @@ -102,19 +103,19 @@ def main(): | ||||
|       logger.info('Empty response for search query.') | ||||
|  | ||||
|   elif arguments['progress']: | ||||
|     logger.info('Progress cmd selected.') | ||||
|     logger.debug('Progress cmd selected.') | ||||
|     response = deluge.progress() | ||||
|  | ||||
|   elif arguments['get']: | ||||
|     logger.info('Get cmd selected for id: {}'.format(_id)) | ||||
|     logger.debug('Get cmd selected for id: {}'.format(_id)) | ||||
|     response = deluge.get(_id) | ||||
|  | ||||
|   elif arguments['ls']: | ||||
|     logger.info('List cmd selected') | ||||
|     logger.debug('List cmd selected') | ||||
|     response = deluge.get_all(_filter=_filter) | ||||
|  | ||||
|   elif arguments['toggle']: | ||||
|     logger.info('Toggling id: {}'.format(_id)) | ||||
|     logger.debug('Toggling id: {}'.format(_id)) | ||||
|     deluge.togglePaused(_id) | ||||
|  | ||||
|   elif arguments['rm']: | ||||
| @@ -132,15 +133,15 @@ def main(): | ||||
|  | ||||
|   try: | ||||
|     if arguments['--json']: | ||||
|       if len(response) > 1: | ||||
|       print('[{}]'.format(','.join([t.toJSON() for t in response]))) | ||||
|       else: | ||||
|         print(response[0].toJSON()) | ||||
|     elif response: | ||||
|       print(response) | ||||
|  | ||||
|   except KeyError as error: | ||||
|     logger.error('Unexpected error while trying to print') | ||||
|     raise error | ||||
|  | ||||
|   return response | ||||
|   sys.exit(0) | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|   main() | ||||
| @@ -19,6 +19,14 @@ def split_words(string): | ||||
|    logger.debug('Splitting input: {} (type: {}) with split_words'.format(string, type(string))) | ||||
|    return re.findall(r"[\w\d']+", string.lower()) | ||||
|  | ||||
| def responseToString(response=None): | ||||
|    try: | ||||
|       response = response.decode('utf-8') | ||||
|    except (UnicodeDecodeError, AttributeError): | ||||
|       pass | ||||
|  | ||||
|    return response | ||||
|  | ||||
| class Deluge(object): | ||||
|    """docstring for ClassName""" | ||||
|    def __init__(self): | ||||
| @@ -46,7 +54,7 @@ class Deluge(object): | ||||
|       return torrents | ||||
|  | ||||
|    def _connect(self): | ||||
|       logger.info('Checking if script on same server as deluge RPC') | ||||
|       logger.debug('Checking if script on same server as deluge RPC') | ||||
|       if self.host != 'localhost' and self.host is not None: | ||||
|          try: | ||||
|             if self.password: | ||||
| @@ -66,11 +74,14 @@ class Deluge(object): | ||||
|  | ||||
|    def add(self, url): | ||||
|       logger.info('Adding magnet with url: {}.'.format(url)) | ||||
|       response = None | ||||
|       if (url.startswith('magnet')): | ||||
|          return self.client.call('core.add_torrent_magnet', url, {}) | ||||
|          response = self.client.call('core.add_torrent_magnet', url, {}) | ||||
|       elif url.startswith('http'): | ||||
|          magnet = self.getMagnetFromFile(url) | ||||
|          return self.client.call('core.add_torrent_magnet', magnet, {}) | ||||
|          response = self.client.call('core.add_torrent_magnet', magnet, {}) | ||||
|  | ||||
|       return responseToString(response.decode('utf-8')) | ||||
|  | ||||
|    def get_all(self, _filter=None): | ||||
|       if (type(_filter) is list and len(_filter)): | ||||
| @@ -110,25 +121,34 @@ class Deluge(object): | ||||
|          response = self.client.call('core.resume_torrent', [id]) | ||||
|       else: | ||||
|          response = self.client.call('core.pause_torrent', [id]) | ||||
|       return response | ||||
|       return responseToString(response) | ||||
|  | ||||
|    def remove(self, name, destroy=False): | ||||
|    def removeByName(self, name, destroy=False): | ||||
|       matches = list(filter(lambda t: t.name == name, self.get_all())) | ||||
|       logger.info('Matches for {}: {}'.format(name, matches)) | ||||
|        | ||||
|       if (len(matches) > 1): | ||||
|       if len(matches) > 1: | ||||
|          raise ValueError('Multiple files found matching key. Unable to remove.') | ||||
|       elif (len(matches) == 1): | ||||
|       elif len(matches) == 1: | ||||
|          torrent = matches[0] | ||||
|          response = self.client.call('core.remove_torrent', torrent.key, destroy) | ||||
|          response = self.remove(torrent.key, destroy) | ||||
|          logger.info('Response: {}'.format(str(response))) | ||||
|  | ||||
|          if (response == False): | ||||
|          if response == False: | ||||
|             raise AttributeError('Unable to remove torrent.') | ||||
|          return response | ||||
|          return responseToString(response) | ||||
|       else: | ||||
|          logger.error('ERROR. No torrent found with that name.') | ||||
|  | ||||
|    def remove(self, id, destroy=False): | ||||
|       response = self.client.call('core.remove_torrent', id, destroy) | ||||
|       logger.info('Response: {}'.format(str(response))) | ||||
|  | ||||
|       if response == False: | ||||
|          raise AttributeError('Unable to remove torrent.') | ||||
|  | ||||
|       return responseToString(response) | ||||
|  | ||||
|    def filterOnValue(self, torrents, value): | ||||
|       filteredTorrents = [] | ||||
|       for t in torrents: | ||||
| @@ -155,7 +175,7 @@ class Deluge(object): | ||||
|  | ||||
|    def __del__(self): | ||||
|       if hasattr(self, 'tunnel'): | ||||
|          logger.info('Closing ssh tunnel') | ||||
|          logger.debug('Closing ssh tunnel') | ||||
|          self.tunnel.stop() | ||||
|  | ||||
|    def getMagnetFromFile(self, url): | ||||
|   | ||||
		Reference in New Issue
	
	Block a user