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 |   # Set logging level for streamHandler | ||||||
|   if arguments['--debug']: |   if arguments['--debug']: | ||||||
|     ch.setLevel(logging.DEBUG) |     ch.setLevel(logging.DEBUG) | ||||||
|  |   elif arguments['--info']: | ||||||
|  |     ch.setLevel(logging.INFO) | ||||||
|   elif arguments['--warning']: |   elif arguments['--warning']: | ||||||
|     ch.setLevel(logging.WARNING) |     ch.setLevel(logging.WARNING) | ||||||
|   elif arguments['--error']: |   elif arguments['--error']: | ||||||
|     ch.setLevel(logging.ERROR) |     ch.setLevel(logging.ERROR) | ||||||
|  |  | ||||||
|   logger.info('Deluge client') |  | ||||||
|   logger.debug(arguments) |   logger.debug(arguments) | ||||||
|  |  | ||||||
|   # Get config settings |   # Get config settings | ||||||
| @@ -85,16 +86,16 @@ def main(): | |||||||
|   response = None |   response = None | ||||||
|  |  | ||||||
|   if arguments['add']: |   if arguments['add']: | ||||||
|     logger.info('Add cmd selected with link {}'.format(magnet)) |  | ||||||
|     response = deluge.add(magnet) |     response = deluge.add(magnet) | ||||||
|  |  | ||||||
|     if response is not None: |     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: |     else: | ||||||
|       logger.warning('Add response returned empty: {}'.format(response)) |       logger.warning('Add response returned empty: {}'.format(response)) | ||||||
|  |  | ||||||
|   elif arguments['search']: |   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) |     response = deluge.search(query) | ||||||
|     if response is not None or response != '[]': |     if response is not None or response != '[]': | ||||||
|       logger.info('Search found {} torrents'.format(len(response))) |       logger.info('Search found {} torrents'.format(len(response))) | ||||||
| @@ -102,19 +103,19 @@ def main(): | |||||||
|       logger.info('Empty response for search query.') |       logger.info('Empty response for search query.') | ||||||
|  |  | ||||||
|   elif arguments['progress']: |   elif arguments['progress']: | ||||||
|     logger.info('Progress cmd selected.') |     logger.debug('Progress cmd selected.') | ||||||
|     response = deluge.progress() |     response = deluge.progress() | ||||||
|  |  | ||||||
|   elif arguments['get']: |   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) |     response = deluge.get(_id) | ||||||
|  |  | ||||||
|   elif arguments['ls']: |   elif arguments['ls']: | ||||||
|     logger.info('List cmd selected') |     logger.debug('List cmd selected') | ||||||
|     response = deluge.get_all(_filter=_filter) |     response = deluge.get_all(_filter=_filter) | ||||||
|  |  | ||||||
|   elif arguments['toggle']: |   elif arguments['toggle']: | ||||||
|     logger.info('Toggling id: {}'.format(_id)) |     logger.debug('Toggling id: {}'.format(_id)) | ||||||
|     deluge.togglePaused(_id) |     deluge.togglePaused(_id) | ||||||
|  |  | ||||||
|   elif arguments['rm']: |   elif arguments['rm']: | ||||||
| @@ -132,15 +133,15 @@ def main(): | |||||||
|  |  | ||||||
|   try: |   try: | ||||||
|     if arguments['--json']: |     if arguments['--json']: | ||||||
|       if len(response) > 1: |       print('[{}]'.format(','.join([t.toJSON() for t in response]))) | ||||||
|         print('[{}]'.format(','.join([t.toJSON() for t in response]))) |     elif response: | ||||||
|       else: |       print(response) | ||||||
|         print(response[0].toJSON()) |  | ||||||
|   except KeyError as error: |   except KeyError as error: | ||||||
|     logger.error('Unexpected error while trying to print') |     logger.error('Unexpected error while trying to print') | ||||||
|     raise error |     raise error | ||||||
|  |  | ||||||
|   return response |   sys.exit(0) | ||||||
|  |  | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|   main() |   main() | ||||||
| @@ -19,6 +19,14 @@ def split_words(string): | |||||||
|    logger.debug('Splitting input: {} (type: {}) with split_words'.format(string, type(string))) |    logger.debug('Splitting input: {} (type: {}) with split_words'.format(string, type(string))) | ||||||
|    return re.findall(r"[\w\d']+", string.lower()) |    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): | class Deluge(object): | ||||||
|    """docstring for ClassName""" |    """docstring for ClassName""" | ||||||
|    def __init__(self): |    def __init__(self): | ||||||
| @@ -46,7 +54,7 @@ class Deluge(object): | |||||||
|       return torrents |       return torrents | ||||||
|  |  | ||||||
|    def _connect(self): |    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: |       if self.host != 'localhost' and self.host is not None: | ||||||
|          try: |          try: | ||||||
|             if self.password: |             if self.password: | ||||||
| @@ -66,11 +74,14 @@ class Deluge(object): | |||||||
|  |  | ||||||
|    def add(self, url): |    def add(self, url): | ||||||
|       logger.info('Adding magnet with url: {}.'.format(url)) |       logger.info('Adding magnet with url: {}.'.format(url)) | ||||||
|  |       response = None | ||||||
|       if (url.startswith('magnet')): |       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'): |       elif url.startswith('http'): | ||||||
|          magnet = self.getMagnetFromFile(url) |          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): |    def get_all(self, _filter=None): | ||||||
|       if (type(_filter) is list and len(_filter)): |       if (type(_filter) is list and len(_filter)): | ||||||
| @@ -110,25 +121,34 @@ class Deluge(object): | |||||||
|          response = self.client.call('core.resume_torrent', [id]) |          response = self.client.call('core.resume_torrent', [id]) | ||||||
|       else: |       else: | ||||||
|          response = self.client.call('core.pause_torrent', [id]) |          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())) |       matches = list(filter(lambda t: t.name == name, self.get_all())) | ||||||
|       logger.info('Matches for {}: {}'.format(name, matches)) |       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.') |          raise ValueError('Multiple files found matching key. Unable to remove.') | ||||||
|       elif (len(matches) == 1): |       elif len(matches) == 1: | ||||||
|          torrent = matches[0] |          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))) |          logger.info('Response: {}'.format(str(response))) | ||||||
|  |  | ||||||
|          if (response == False): |          if response == False: | ||||||
|             raise AttributeError('Unable to remove torrent.') |             raise AttributeError('Unable to remove torrent.') | ||||||
|          return response |          return responseToString(response) | ||||||
|       else: |       else: | ||||||
|          logger.error('ERROR. No torrent found with that name.') |          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): |    def filterOnValue(self, torrents, value): | ||||||
|       filteredTorrents = [] |       filteredTorrents = [] | ||||||
|       for t in torrents: |       for t in torrents: | ||||||
| @@ -155,7 +175,7 @@ class Deluge(object): | |||||||
|  |  | ||||||
|    def __del__(self): |    def __del__(self): | ||||||
|       if hasattr(self, 'tunnel'): |       if hasattr(self, 'tunnel'): | ||||||
|          logger.info('Closing ssh tunnel') |          logger.debug('Closing ssh tunnel') | ||||||
|          self.tunnel.stop() |          self.tunnel.stop() | ||||||
|  |  | ||||||
|    def getMagnetFromFile(self, url): |    def getMagnetFromFile(self, url): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user