|
| 1 | +"""Basic Python wrapper for the NZBGet API""" |
| 2 | + |
| 3 | +__author__ = """Holiest of Hand Grenades""" |
| 4 | + |
| 5 | +import json |
| 6 | +import logging |
| 7 | +import ssl |
| 8 | +from urllib.parse import quote |
| 9 | +import xmlrpc.client |
| 10 | + |
| 11 | +_LOGGER = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class NZBGetAPIException(Exception): |
| 15 | + """Top level module exception.""" |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +class NZBGetAPI: |
| 20 | + """Simple wrapper for NZBGet's API.""" |
| 21 | + |
| 22 | + def __init__( |
| 23 | + self, |
| 24 | + host, |
| 25 | + username=None, |
| 26 | + password=None, |
| 27 | + secure=False, |
| 28 | + verify_certificate=True, |
| 29 | + port=6789, |
| 30 | + ): |
| 31 | + """Initialize NZBGet API and set headers needed later.""" |
| 32 | + |
| 33 | + ssl = "s" if secure else "" |
| 34 | + |
| 35 | + if username is not None and password is not None: |
| 36 | + url = ( |
| 37 | + f"http{ssl}://{quote(username)}:{quote(password)}@{host}:{port}/xmlrpc" |
| 38 | + ) |
| 39 | + else: |
| 40 | + url = f"http{ssl}://{host}:{port}/xmlrpc" |
| 41 | + |
| 42 | + if secure and not verify_certificate: |
| 43 | + ssl_ctx = ssl.create_default_context() |
| 44 | + ssl_ctx.check_hostname = False |
| 45 | + ssl_ctx.verify_mode = ssl.CERT_NONE |
| 46 | + |
| 47 | + self.proxy = xmlrpc.client.ServerProxy(url, context=ssl_ctx) |
| 48 | + else: |
| 49 | + self.proxy = xmlrpc.client.ServerProxy(url) |
| 50 | + |
| 51 | + def __proxy_call(self, method_call): |
| 52 | + try: |
| 53 | + # Internal method to transalte xmlrpc exceptions to library ones. |
| 54 | + return method_call() |
| 55 | + except xmlrpc.client.Error as err: |
| 56 | + raise NZBGetAPIException(str(err)) from None |
| 57 | + |
| 58 | + def version(self): |
| 59 | + """Get Version""" |
| 60 | + return self.__proxy_call(lambda: self.proxy.version()) |
| 61 | + |
| 62 | + def shutdown(self): |
| 63 | + """Shutdown NZBGet""" |
| 64 | + return self.__proxy_call(lambda: self.proxy.shutdown()) |
| 65 | + |
| 66 | + def reload(self): |
| 67 | + """Reload NZBGet""" |
| 68 | + return self.__proxy_call(lambda: self.proxy.reload()) |
| 69 | + |
| 70 | + def listgroups(self, number_of_log_entries): |
| 71 | + """Get current groups (NZB Files)""" |
| 72 | + return self.__proxy_call(lambda: self.proxy.listgroups(number_of_log_entries)) |
| 73 | + |
| 74 | + def listfiles(self, nzbid): |
| 75 | + """Get file list from an nzbfile""" |
| 76 | + return self.__proxy_call(lambda: self.proxy.listfiles(0, 0, nzbid)) |
| 77 | + |
| 78 | + def history(self, hidden=False): |
| 79 | + """Get History""" |
| 80 | + return self.__proxy_call(lambda: self.proxy.history(hidden)) |
| 81 | + |
| 82 | + def append( |
| 83 | + self, |
| 84 | + nzbfilename, |
| 85 | + nzbcontent, |
| 86 | + category, |
| 87 | + priority, |
| 88 | + add_to_top, |
| 89 | + add_paused, |
| 90 | + dupe_key, |
| 91 | + dupe_score, |
| 92 | + dupe_mode, |
| 93 | + pp_parameters, |
| 94 | + ): |
| 95 | + """Append an NZB to the queue.""" |
| 96 | + return self.__proxy_call( |
| 97 | + lambda: self.proxy.append( |
| 98 | + nzbfilename, |
| 99 | + nzbcontent, |
| 100 | + category, |
| 101 | + priority, |
| 102 | + add_to_top, |
| 103 | + add_paused, |
| 104 | + dupe_key, |
| 105 | + dupe_score, |
| 106 | + dupe_mode, |
| 107 | + pp_parameters, |
| 108 | + ) |
| 109 | + ) |
| 110 | + |
| 111 | + def editqueue(self, command, param, ids): |
| 112 | + """Edit the queue.""" |
| 113 | + return self.__proxy_call(lambda: self.proxy.editqueue(command, param, ids)) |
| 114 | + |
| 115 | + def scan(self): |
| 116 | + """Scan the nzbdirectory.""" |
| 117 | + return self.__proxy_call(lambda: self.proxy.scan()) |
| 118 | + |
| 119 | + def status(self): |
| 120 | + """Get Status.""" |
| 121 | + return self.__proxy_call(lambda: self.proxy.status()) |
| 122 | + |
| 123 | + def log(self, id_from, number_of_entries): |
| 124 | + """Return the log.""" |
| 125 | + return self.__proxy_call(lambda: self.proxy.log(id_from, number_of_entries)) |
| 126 | + |
| 127 | + def writelog(self, kind, text): |
| 128 | + """Write a new log message.""" |
| 129 | + return self.__proxy_call(lambda: self.proxy.writelog(kind, text)) |
| 130 | + |
| 131 | + def loadlog(self, nzbid, id_from, number_of_entries): |
| 132 | + """Return nzbg-log for a specific nzb-file.""" |
| 133 | + return self.__proxy_call( |
| 134 | + lambda: self.proxy.loadlog(nzbid, id_from, number_of_entries) |
| 135 | + ) |
| 136 | + |
| 137 | + def servervolumes(self): |
| 138 | + """Collect newsserver historical transfer statistics.""" |
| 139 | + return self.__proxy_call(lambda: self.proxy.servervolumes()) |
| 140 | + |
| 141 | + def resetservervolume(self, server_id, sounter): |
| 142 | + """Reset newsserver historical transfer statistics.""" |
| 143 | + return self.__proxy_call( |
| 144 | + lambda: self.proxy.resetservervolume(server_id, sounter) |
| 145 | + ) |
| 146 | + |
| 147 | + def rate(self, limit): |
| 148 | + """Set transfer limit.""" |
| 149 | + return self.__proxy_call(lambda: self.proxy.rate(limit)) |
| 150 | + |
| 151 | + def pausedownload(self): |
| 152 | + """Pause download queue.""" |
| 153 | + return self.__proxy_call(lambda: self.proxy.pausedownload()) |
| 154 | + |
| 155 | + def resumedownload(self): |
| 156 | + """Resume downloads.""" |
| 157 | + return self.__proxy_call(lambda: self.proxy.resumedownload()) |
| 158 | + |
| 159 | + def pausepost(self): |
| 160 | + """Pause post-processing.""" |
| 161 | + return self.__proxy_call(lambda: self.proxy.pausepost()) |
| 162 | + |
| 163 | + def resumescan(self): |
| 164 | + """Resume previously paused scanning for new nzbfiles.""" |
| 165 | + return self.__proxy_call(lambda: self.proxy.resumescan()) |
| 166 | + |
| 167 | + def scheduleresume(self, seconds): |
| 168 | + """Schedule resume after expiring of wait interval.""" |
| 169 | + return self.__proxy_call(lambda: self.proxy.scheduleresume(seconds)) |
| 170 | + |
| 171 | + def config(self): |
| 172 | + """Get current config.""" |
| 173 | + return self.__proxy_call(lambda: self.proxy.config()) |
| 174 | + |
| 175 | + def loadconfig(self): |
| 176 | + """Read configuration file from disk.""" |
| 177 | + return self.__proxy_call(lambda: self.proxy.loadconfig()) |
| 178 | + |
| 179 | + def saveconfig(self, options): |
| 180 | + """Save configuration file to disk.""" |
| 181 | + return self.__proxy_call(lambda: self.proxy.saveconfig(options)) |
| 182 | + |
| 183 | + def configtemplates(self, load_from_disk): |
| 184 | + """Return configuration file template and configuration sections""" |
| 185 | + return self.__proxy_call(lambda: self.proxy.configtemplaes(load_from_disk)) |
0 commit comments