-
Notifications
You must be signed in to change notification settings - Fork 0
/
itemretr.py
157 lines (133 loc) · 5.33 KB
/
itemretr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from utils import *
def get_entity_tuples_info(sta_link: str, sta_version: str, entity: str) -> list:
"""
Retrieves information about entities from a STAC API.
Args:
sta_link (str): The base URL of the STAC API.
sta_version (str): The version of the STAC API.
entity (str): The name of the entity to retrieve information about.
Returns:
list: A list of tuples containing the entity ID, title, and description.
"""
fetched_vars: dict = dict()
list_of_entity_tuples: list = []
entity_url = f"{sta_link}/{sta_version}/{entity}?$count=true"
validator_value = validate_sta_link(
link=sta_link,
version=sta_version
)
if validator_value is False:
return
else:
entity_json = open_sta_entity_links(
link=entity_url
)
if entity_json is not None:
if entity_json["@iot.count"] == 0:
print(f"Entity {entity} is empty.")
return None
elif entity_json["@iot.count"] > 0:
sta_link_version = f"{sta_link}/{sta_version}"
entity_count_number = get_number_of_entities(
link=sta_link_version,
entity=entity
)
list_of_entities_id = get_list_of_entities_id(
link=sta_link_version,
entity=entity
)
if entity_count_number == len(list_of_entities_id):
for entity_index, entity_number in enumerate(
list_of_entities_id
):
entity_url_by_number = (
f"{sta_link_version}/{entity}({entity_number})"
)
entity_json_by_number = open_sta_entity_links(
entity_url_by_number
)
fetched_vars["entity_id"] = name_sanitize(str(entity_json_by_number["name"]))
fetched_vars[
"entity_title"
] = entity_json_by_number["name"]
if entity_json_by_number.get("description") is None:
fetched_vars[
"entity_description"
] = "This is a STAC item."
else:
fetched_vars[
"entity_description"
] = entity_json_by_number["description"]
list_of_entity_tuples.append(
(
fetched_vars["entity_id"],
fetched_vars["entity_title"],
fetched_vars["entity_description"],
)
)
else:
print("Error in getting info")
return None
return list_of_entity_tuples
def get_thing_info( thing_json: dict) -> tuple:
"""
A function to get the automatically generated ID,
title, description and properties of a specific Thing.
Args:
things_json (dict) : JSON response of request for a thing/item
Return:
Item-ID (str): id of item, title(str): title of item, description(str): description of item, and properties(dict):associated properties of each thing/item
"""
fetched_vars = dict()
fetched_vars["item_id"] = name_sanitize(str(thing_json["name"]))
fetched_vars["item_title"] = thing_json["name"]
if (
thing_json.get("description") is None
and thing_json.get("properties", {}).get("description") is None
):
fetched_vars[
"item_description"
] = "This is a STAC item."
elif (
thing_json.get("description") is not None
and thing_json.get("properties", {}).get("description") is None
):
fetched_vars["item_description"] = thing_json["description"]
elif (
thing_json.get("description") is None
and thing_json.get("properties", {}).get("description") is not None
):
fetched_vars["item_description"] = thing_json["properties"][
"description"
]
if thing_json.get("properties") is not None:
fetched_vars["properties"] = thing_json.get("properties")
return (
fetched_vars["item_id"],
fetched_vars["item_title"],
fetched_vars["item_description"],
fetched_vars["properties"]
)
def replace_item_info( thing_json: dict):
"""
to change auto generated ID, title with other
Args:
thing_json (dict): JSON response of a Thing or item.
Returns:
tuple: A tuple containing the following elements:
- item_id (str): Unique identifier for the item.
- item_title (str): Title of the item.
- item_description (str): Description of the item.
"""
fetched_vars = dict()
(
fetched_vars["item_id"],
fetched_vars["item_title"],
fetched_vars["item_description"],
fetched_vars["properties"]
) = get_thing_info(thing_json=thing_json)
return (
fetched_vars["item_id"],
fetched_vars["item_title"],
fetched_vars["item_description"],
)