-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe.py
62 lines (53 loc) · 1.94 KB
/
subscribe.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
import requests
import json
import earthaccess
from config import QUEUE_ARN_FILE, COLLECTION_CONCEPT_ID, COLLECTION_NAME, SUBSCRIBER_ID
def get_queue_arn():
try:
with open(QUEUE_ARN_FILE, 'r') as f:
return f.read().strip()
except FileNotFoundError:
raise Exception(f"Queue ARN file {QUEUE_ARN_FILE} not found. Please run create_queue.py first.")
def create_cmr_subscription():
# Get bearer token from earthaccess
auth = earthaccess.login()
bearer_token = auth.token['access_token']
# Get queue ARN
queue_arn = get_queue_arn()
# Create subscription request
subscription_request = {
"Name": f"{SUBSCRIBER_ID}-{COLLECTION_CONCEPT_ID}-subscription",
"CollectionConceptId": COLLECTION_CONCEPT_ID,
"Type": "granule",
"Query": "*",
"SubscriberId": SUBSCRIBER_ID,
"EndPoint": queue_arn,
"Mode": ["New","Update"],
"Method":"ingest",
"MetadataSpecification": {
"URL": "https://cdn.earthdata.nasa.gov/umm/subscription/v1.1.1",
"Name": "UMM-Sub",
"Version": "1.1.1"
}
}
# Make the request to CMR
headers = {
"Authorization": f"Bearer {bearer_token}",
"Content-Type": "application/vnd.nasa.cmr.umm+json"
}
try:
response = requests.post(
"https://cmr.earthdata.nasa.gov/ingest/subscriptions/",
headers=headers,
json=subscription_request
)
if response.status_code == 200:
print("Successfully created CMR subscription")
print(f"Response: {response.text}")
else:
print(f"Error creating subscription. Status code: {response.status_code}")
print(f"Response: {response.text}")
except Exception as e:
print(f"Error making subscription request: {str(e)}")
if __name__ == "__main__":
create_cmr_subscription()