def rmAPI_search(search_query): URL = 'https://rickandmortyapi.com/api/character/?name=' + search_query IRRELEVANT = ['episode', 'url', 'created', 'image'] try: opened = url_open(URL).read().decode('utf-8') info_json = json.loads(opened) for result in info_json['results']: #'results' : [{'key': value}, {'key' : {'key': value}}] -> format of the json return from the API call for key in result: if key not in IRRELEVANT: value = result[key] if key == "origin" or key == "location": value = result[key]['name'] try: print('{}: {}'.format(key, value)) #Image.open(url_open(result['image'])) except: value = value.encode('utf-8') print('{}: {}'.format(key, value)) #Image.open(url_open(result['image'])) print("\n") except: print('{}: No such character found'.format(search_query))
def main(): module = AnsibleModule(argument_spec=dict( arch=dict(choices=['alpha', 'amd64', 'arm', 'hppa', 'ia64', 'mips', 'ppc', 's390', 'sh', 'sparc', 'x86'], required=True), hardened=dict(type='bool', required=True), multilib=dict(type='bool', required=True))) url = ('http://distfiles.gentoo.org/releases/' + '{arch}/'.format(arch=module.params['arch']) + 'autobuilds/latest-stage3.txt') regexp = build_regexp(module.params) paths = [line.split(' ')[0] for line in url_open(url).read().splitlines() if line and not line.startswith('#')] # Find a Stage path matching with provided parameters. stage_paths = [path for path in paths if re.match(regexp, path)] if len(stage_paths) != 1: module.fail_json(msg='Cannot find a matching Stage') stage_path = stage_paths[0] module.exit_json(changed=True, msg='A Stage archive has been selected.', result=stage_path)
def main(): module = AnsibleModule( argument_spec=dict(arch=dict(choices=[ 'alpha', 'amd64', 'arm', 'hppa', 'ia64', 'mips', 'ppc', 's390', 'sh', 'sparc', 'x86' ], required=True), hardened=dict(type='bool', required=True), multilib=dict(type='bool', required=True))) url = ('http://distfiles.gentoo.org/releases/' + '{arch}/'.format(arch=module.params['arch']) + 'autobuilds/latest-stage3.txt') regexp = build_regexp(module.params) paths = [ line.split(' ')[0] for line in url_open(url).read().splitlines() if line and not line.startswith('#') ] # Find a Stage path matching with provided parameters. stage_paths = [path for path in paths if re.match(regexp, path)] if len(stage_paths) != 1: module.fail_json(msg='Cannot find a matching Stage') stage_path = stage_paths[0] module.exit_json(changed=True, msg='A Stage archive has been selected.', result=stage_path)
def test_request_reading(serve): """ Test actual request/response cycle in the presence of Request.copy() and other methods that can potentially hang. """ with serve(_test_app_req_reading) as server: for key in _test_ops_req_read: resp = url_open(server.url + key, timeout=3) assert resp.read() == b"ok"
def returnSoup(url): htmlReader = url_open(url) page_text = htmlReader.read() htmlReader.close() page_soup = soup(page_text, "html.parser") return page_soup
def from_code(cls, code, fail_handler): """Get the country informations using provided country code.""" url = "https://restcountries.eu/rest/v1/alpha/{code}".format(code=code) try: info = json.loads(url_open(url).read()) except: fail_handler(msg="Failed to parse read informations") return cls(info["name"], info["region"], info["borders"])
def from_code(cls, code, fail_handler): '''Get the country informations using provided country code.''' url = 'https://restcountries.eu/rest/v1/alpha/{code}'.format(code=code) try: info = json.loads(url_open(url).read()) except: fail_handler(msg='Failed to parse read informations') return cls(info['name'], info['region'], info['borders'])
def from_ip(cls, fail_handler, ip=None): """Get the country informations using provided IP address.""" url = "http://ip-api.com/json" if ip is not None: url += "/{ip}".format(ip=ip) try: info = json.loads(url_open(url).read()) except: fail_handler(msg="Failed to parse country from IP") return cls.from_code(info["countryCode"], fail_handler)
def from_ip(cls, fail_handler, ip=None): '''Get the country informations using provided IP address.''' url = 'http://ip-api.com/json' if ip is not None: url += '/{ip}'.format(ip=ip) try: info = json.loads(url_open(url).read()) except: fail_handler(msg='Failed to parse country from IP') return cls.from_code(info['countryCode'], fail_handler)
def parse_from_url(cls, url): return cls.parse(url_open(url).read())