def get_from_url(url: str) -> Optional[str]: """ Retrieve URL and return content """ try: req = urllib.request.Request(url) req.add_header("User-Agent", "SABnzbd/%s" % sabnzbd.__version__) with urllib.request.urlopen(req) as response: return ubtou(response.read()) except: return None
def get_script_log(self, nzo_id): """ Return decompressed log file """ data = "" t = (nzo_id, ) if self.execute("SELECT script_log FROM history WHERE nzo_id = ?", t): try: data = ubtou( zlib.decompress(self.c.fetchone().get("script_log"))) except: pass return data
def nntp_to_msg(text: Union[List[AnyStr], str]) -> str: """ Format raw NNTP bytes data for display """ if isinstance(text, list): text = text[0] # Only need to split if it was raw data # Sometimes (failed login) we put our own texts if not isinstance(text, bytes): return text else: lines = text.split(b"\r\n") return ubtou(lines[0])
def convert_version(text): """ Convert version string to numerical value and a testversion indicator """ version = 0 test = True m = RE_VERSION.search(ubtou(text)) if m: version = int(m.group(1)) * 1000000 + int(m.group(2)) * 10000 + int(m.group(3)) * 100 try: if m.group(4).lower() == "rc": version = version + 80 elif m.group(4).lower() == "beta": version = version + 40 version = version + int(m.group(5)) except: version = version + 99 test = False return version, test
def publicipv4(): """Because of dual IPv4/IPv6 clients, finding the public ipv4 needs special attention, meaning forcing IPv4 connections, and not allowing IPv6 connections """ public_ipv4 = None try: ipv4_found = False # we only want IPv4 resolving, so socket.AF_INET: result = addresslookup4(sabnzbd.cfg.selftest_host()) except (socket.error, multiprocessing.context.TimeoutError): # something very bad: no urllib2, no resolving of selftest_host, no network at all return public_ipv4 # we got one or more IPv4 address(es), so let's connect to them for item in result: # get next IPv4 address of sabnzbd.cfg.selftest_host() selftest_ipv4 = item[4][0] try: # put the selftest_host's IPv4 address into the URL req = urllib.request.Request("http://" + selftest_ipv4 + "/") # specify the User-Agent, because certain sites refuse connections with "python urllib2" as User-Agent: req.add_header("User-Agent", "SABnzbd/%s" % sabnzbd.__version__) # specify the Host, because we only provide the IPv4 address in the URL: req.add_header("Host", sabnzbd.cfg.selftest_host()) # get the response, timeout 2 seconds, in case the website is not accessible public_ipv4 = ubtou(urllib.request.urlopen(req, timeout=2).read()) # ... check the response is indeed an IPv4 address: # if we got anything else than a plain IPv4 address, this will raise an exception socket.inet_aton(public_ipv4) # if we get here without exception, we're done: ipv4_found = True break except (socket.error, urllib.error.URLError): # the connect OR the inet_aton raised an exception, so: # continue the for loop to try next server IPv4 address pass if not ipv4_found: public_ipv4 = None return public_ipv4
def _build_request(url): # Detect basic auth # Adapted from python-feedparser user_passwd = None u = urllib.parse.urlparse(url) if u.username is not None or u.password is not None: if u.username and u.password: user_passwd = "%s:%s" % (u.username, u.password) host_port = u.hostname if u.port: host_port += ":" + str(u.port) url = urllib.parse.urlunparse(u._replace(netloc=host_port)) # Start request req = urllib.request.Request(url) # Add headers req.add_header("User-Agent", "SABnzbd/%s" % sabnzbd.__version__) req.add_header("Accept-encoding", "gzip") if user_passwd: req.add_header("Authorization", "Basic " + ubtou(base64.b64encode(utob(user_passwd))).strip()) return urllib.request.urlopen(req)