|
| 1 | +import requests |
| 2 | +import random |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import argparse |
| 6 | +import HTTPAuthOptions |
| 7 | + |
| 8 | +# OData service base URL |
| 9 | +#BASE_URL = "https://dhr1.cesnet.cz/odata/v2" |
| 10 | +#BASE_URL = "https://gss.dhr.metacentrum.cz/odata/v1" |
| 11 | +#BASE_URL = "https://dhs2.copernicus.eu/odatav4/odata/v2" |
| 12 | +BASE_URL = "https://collgs.cesnet.cz/odata/v1" |
| 13 | + |
| 14 | +# Keycloak authentication data |
| 15 | +TOKEN_URL="https://dhs2.copernicus.eu/auth" |
| 16 | +REALM = "gss" |
| 17 | +CLIENT_ID="dhs2" |
| 18 | + |
| 19 | +# Destination directory for downloads |
| 20 | +DOWNLOAD_DIR = "./tmp/" |
| 21 | +os.makedirs(DOWNLOAD_DIR, exist_ok=True) |
| 22 | + |
| 23 | +MAX_PRODUCTS = 2 |
| 24 | + |
| 25 | +nodes_to_url = lambda node_ids: "/".join([f"Nodes('{node_id}')" for node_id in node_ids]) |
| 26 | + |
| 27 | +def get_products(auth, queries): |
| 28 | + """Fetch products by given queries.""" |
| 29 | + products_by_query = {} |
| 30 | + |
| 31 | + for query in queries: |
| 32 | + response = requests.get( |
| 33 | + f"{BASE_URL}/Products?{query}", |
| 34 | + auth=auth |
| 35 | + ) |
| 36 | + |
| 37 | + if response.status_code == 200: |
| 38 | + data = response.json() |
| 39 | + products = data.get("value", []) |
| 40 | + |
| 41 | + if products: |
| 42 | + products_by_query[query] = random.sample(products, min(MAX_PRODUCTS, len(products))) |
| 43 | + logging.info(f"Found {len(products_by_query[query])} products for type {query}.") |
| 44 | + else: |
| 45 | + logging.warning(f"No products found for type {query}.") |
| 46 | + else: |
| 47 | + logging.error(f"Failed to fetch products for {query}: {response.status_code} {response.text}") |
| 48 | + |
| 49 | + return products_by_query |
| 50 | + |
| 51 | +def download_value(entity, entity_id, auth, entity_type, node_ids=None): |
| 52 | + """Download entity's $value (binary content) to tmp.""" |
| 53 | + if entity_type == 'Nodes': |
| 54 | + url = f"{BASE_URL}/Products({entity_id})/{nodes_to_url(node_ids)}/$value" |
| 55 | + else: |
| 56 | + url = f"{BASE_URL}/{entity_type}({entity_id})/$value" |
| 57 | + response = requests.get(url, auth=auth, stream=True) |
| 58 | + |
| 59 | + if response.status_code == 200: |
| 60 | + file_path = os.path.join(DOWNLOAD_DIR, f"{entity['Name']}") |
| 61 | + with open(file_path, "wb") as f: |
| 62 | + for chunk in response.iter_content(chunk_size=8192): |
| 63 | + f.write(chunk) |
| 64 | + logging.info(f"Downloaded {entity_type} {entity['Id']} to {file_path}") |
| 65 | + else: |
| 66 | + logging.error(f"Failed to download {entity_type} {entity['Id']} value: {response.status_code} {requests.status_codes._codes[response.status_code][0]}") |
| 67 | + |
| 68 | +def inspect_nodes(auth, product_id, node_id, depth=0, max_depth=1): |
| 69 | + """Recursively explore Nodes and download some of their $value.""" |
| 70 | + if depth > max_depth: |
| 71 | + return |
| 72 | + |
| 73 | + logging.info(f"Inspecting Node {node_id}, Depth {depth}") |
| 74 | + |
| 75 | + node_entity_response = requests.get( |
| 76 | + f"{BASE_URL}/Products({product_id})/{nodes_to_url(node_id)}?$format=json", |
| 77 | + auth=auth |
| 78 | + ) |
| 79 | + if node_entity_response.status_code == 200: |
| 80 | + node_entity = node_entity_response.json() |
| 81 | + if node_entity: |
| 82 | + logging.info(f" Node entity found for Node {node_id}.") |
| 83 | + inspect_child_nodes(auth, product_id, node_id, depth, max_depth) |
| 84 | + else: |
| 85 | + logging.warning(f" No node entity found for Node {node_id}.") |
| 86 | + else: |
| 87 | + logging.error(f" Failed to fetch node entity for Node {node_id}: {node_entity_response.status_code}") |
| 88 | + |
| 89 | +# Fetch child nodes |
| 90 | +def inspect_child_nodes(auth, product_id, node_ids, depth=0, max_depth=1): |
| 91 | + node_response = requests.get( |
| 92 | + f"{BASE_URL}/Products({product_id})/{nodes_to_url(node_ids)}/Nodes?$format=json", |
| 93 | + auth=auth |
| 94 | + ) |
| 95 | + |
| 96 | + if node_response.status_code == 200: |
| 97 | + nodes = node_response.json().get("value", []) |
| 98 | + if nodes: |
| 99 | + logging.info(f" Found {len(nodes)} child nodes for Node {node_ids}") |
| 100 | + |
| 101 | + # Randomly select a few nodes to download |
| 102 | + selected_nodes = random.sample(nodes, min(2, len(nodes))) |
| 103 | + for node in selected_nodes: |
| 104 | + node_id = node["Id"] |
| 105 | + #download_value(node, product_id, auth, "Nodes", node_ids) |
| 106 | + |
| 107 | + # Recursively go deeper |
| 108 | + inspect_nodes(auth, product_id, node_ids + [node_id], depth + 1, max_depth) |
| 109 | + else: |
| 110 | + logging.warning(f" No child nodes found for Node {node_ids}") |
| 111 | + else: |
| 112 | + logging.error(f" Failed to fetch nodes for Node {node_ids}: {node_response.status_code}") |
| 113 | + |
| 114 | +def inspect_products(auth, products_by_query): |
| 115 | + """Fetch and log attributes, nodes, and download $value for selected products.""" |
| 116 | + for query, products in products_by_query.items(): |
| 117 | + logging.info(f"Inspecting Product Result: {query}") |
| 118 | + |
| 119 | + for product in products: |
| 120 | + product_id = product["Id"] |
| 121 | + product_name = product["Name"] |
| 122 | + logging.info(f"Product ID: {product_id}, Name: {product_name}") |
| 123 | + |
| 124 | + # Download product $value |
| 125 | + download_value(product, product_id, auth, "Products") |
| 126 | + |
| 127 | + # Get attributes |
| 128 | + attr_response = requests.get( |
| 129 | + f"{BASE_URL}/Products({product_id})/Attributes?$format=json", |
| 130 | + auth=auth |
| 131 | + ) |
| 132 | + if attr_response.status_code == 200: |
| 133 | + attributes = attr_response.json().get("value", []) |
| 134 | + if attributes: |
| 135 | + logging.info(f" Attributes found for Product {product_id}.") |
| 136 | + else: |
| 137 | + logging.warning(f" No attributes found for Product {product_id}.") |
| 138 | + else: |
| 139 | + logging.error(f" Failed to fetch attributes for Product {product_id}: {attr_response.status_code}") |
| 140 | + |
| 141 | + # Get nodes |
| 142 | + node_response = requests.get( |
| 143 | + f"{BASE_URL}/Products({product_id})/Nodes?$format=json", |
| 144 | + auth=auth |
| 145 | + ) |
| 146 | + if node_response.status_code == 200: |
| 147 | + nodes = node_response.json().get("value", []) |
| 148 | + if nodes: |
| 149 | + logging.info(f"Found {len(nodes)} nodes for Product {product_id}") |
| 150 | + |
| 151 | + # Select a random node and walk deeper |
| 152 | + random_node = random.choice(nodes) |
| 153 | + inspect_nodes(auth, product_id, [random_node["Id"]]) |
| 154 | + else: |
| 155 | + logging.warning(f"No nodes found for Product {product_id}") |
| 156 | + else: |
| 157 | + logging.error(f" Failed to fetch nodes for Product {product_id}: {node_response.status_code}") |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + parser = argparse.ArgumentParser() |
| 163 | + |
| 164 | + # Create a mutually exclusive group |
| 165 | + group = parser.add_mutually_exclusive_group(required=True) |
| 166 | + group.add_argument("-b", action="store_true", help="Use basic authentication (.basic-auth file)") |
| 167 | + group.add_argument("-t", action="store_true", help="Use token authentication (.token file)") |
| 168 | + group.add_argument("-k", action="store_true", help="Use keycloak authentication (.basic-auth file)") |
| 169 | + |
| 170 | + parser.add_argument("-d", action="count", default=0, help="Increase logging verbosity (-d: INFO, -dd: DEBUG)") |
| 171 | + |
| 172 | + # Parse arguments |
| 173 | + args = parser.parse_args() |
| 174 | + |
| 175 | + # Set logging level based on occurrences of -d |
| 176 | + if args.d >= 2: |
| 177 | + log_level = logging.DEBUG |
| 178 | + elif args.d == 1: |
| 179 | + log_level = logging.INFO |
| 180 | + else: |
| 181 | + log_level = logging.WARNING |
| 182 | + |
| 183 | + # Configure logging |
| 184 | + logging.basicConfig(level=log_level, format="%(asctime)s - %(levelname)s - %(message)s") |
| 185 | + |
| 186 | + if args.k: |
| 187 | + auth = HTTPAuthOptions.KeycloakTokenAuth(server_url=TOKEN_URL, realm=REALM, client_id=CLIENT_ID) |
| 188 | + elif args.t: |
| 189 | + auth = HTTPAuthOptions.HTTPBearerAuth() |
| 190 | + else: |
| 191 | + #auth = HTTPAuthOptions.FileBasedBasicAuth() |
| 192 | + auth = None |
| 193 | + |
| 194 | + with open("filters.txt", "r") as file: |
| 195 | + lines = file.readlines() |
| 196 | + queries = [line.strip() for line in lines if line.strip() and not line.lstrip().startswith("#")] |
| 197 | + |
| 198 | + logging.info("Starting OData queries...") |
| 199 | + products_by_query = get_products(auth, queries) |
| 200 | + inspect_products(auth, products_by_query) |
| 201 | + |
| 202 | + logging.info("OData queries completed successfully.") |
0 commit comments