-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_message.py
executable file
·175 lines (148 loc) · 6.64 KB
/
create_message.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
"""Post a list of newly published archival collections to a Microsoft Teams channel.
Requires the following encrypted environment variables to be set:
- ACCESS_KEY_ID - an access key for an AWS IAM user that has permissions to
write to the S3 bucket specified by `BUCKET_NAME`.
- SECRET_ACCESS_KEY - a secret key for an AWS IAM user that has permissions to
write to the S3 bucket specified by `BUCKET_NAME`.
- BUCKET_NAME - an S3 bucket in which to store a list of published collections.
- AS_BASEURL - base URL of the ArchivesSpace instance to check for newly
published resource records.
- AS_USERNAME - username for an ArchivesSpace user with access to the `search` endpoint.
- AS_PASSWORD - password for an ArchivesSpace user with access to the `search` endpoint.
- CARTOGRAPHER_BASEURL - base URL of the Cartographer instance to check for
newly published arrangement maps.
- TEAMS_URL - the webhook URL for a Teams channel in which newly published collections should be posted.
"""
import calendar
import json
from base64 import b64decode
from datetime import datetime
from os import environ
import boto3
import requests
import shortuuid
from asnake.aspace import ASpace
PREVIOUS_RESULTS_KEY = "results.json"
def main(event=None, context=None):
url = decrypt_env_variable('TEAMS_URL')
date_format_string = '%B %e, %Y'
s3_client = boto3.client('s3',
aws_access_key_id=decrypt_env_variable('ACCESS_KEY_ID'),
aws_secret_access_key=decrypt_env_variable('SECRET_ACCESS_KEY'))
today = datetime.now()
prev_month = today.month - 1
from_date = datetime(
year=today.year,
month=prev_month,
day=1,
hour=0,
minute=0,
second=0)
to_date = datetime(
year=today.year,
month=prev_month,
day=calendar.monthrange(today.year, prev_month)[1],
hour=0,
minute=0,
second=0)
previous_as_results = get_aspace_previously_published(s3_client)
new_updates = list(get_updated_archivesspace_resources())
as_results = [format_result(n) for n in new_updates if n not in previous_as_results]
cartographer_results = [format_result(r) for r in get_updated_cartographer_maps(from_date)]
message = {
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"contentUrl": None,
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.4",
"body": [
{
"type": "TextBlock",
"weight": "bolder",
"text": f"New collections and updated arrangement maps from {from_date.strftime(date_format_string)} through {to_date.strftime(date_format_string)}",
"style": "heading",
"size": "large",
"wrap": True,
},
{
"type": "TextBlock",
"weight": "bolder",
"text": "Newly Published Collections",
"style": "heading",
"size": "medium",
"wrap": True,
},
{
"type": "TextBlock",
"text": " \n".join(as_results) if len(as_results) else "No new collections published during this period.",
"wrap": True
},
{
"type": "TextBlock",
"weight": "bolder",
"text": "Updated Arrangement Maps",
"style": "heading",
"size": "medium",
"wrap": True,
},
{
"type": "TextBlock",
"text": " \n".join(cartographer_results) if len(cartographer_results) else "No updated maps during this period.",
"wrap": True
}
]
}
}
]
}
encoded_msg = json.dumps(message)
requests.post(url, headers={'Content-Type': 'application/json'}, data=encoded_msg)
update_aspace_previously_published(new_updates, s3_client)
def decrypt_env_variable(env_key):
encrypted = environ.get(env_key)
return boto3.client('kms').decrypt(
CiphertextBlob=b64decode(encrypted),
EncryptionContext={'LambdaFunctionName': environ['AWS_LAMBDA_FUNCTION_NAME']}
)['Plaintext'].decode('utf-8')
def format_result(result):
"""Creates a formatted DIMES link from a result."""
dimes_id = shortuuid.uuid(name=result['uri'])
return f"[{result['title']}](https://dimes.rockarch.org/collections/{dimes_id})"
def get_updated_archivesspace_resources():
"""Gets updated resource records from ArchivesSpace."""
client = ASpace(
baseurl=decrypt_env_variable('AS_BASEURL'),
username=decrypt_env_variable('AS_USERNAME'),
password=decrypt_env_variable('AS_PASSWORD')).client
return client.get_paged(
"/search?q=publish:true&type[]=resource&fields[]=title,uri")
def get_updated_cartographer_maps(from_date):
"""Gets updated maps from Cartographer."""
resp = requests.get(
f"{decrypt_env_variable('CARTOGRAPHER_BASEURL')}/api/maps/?modified_since={int(from_date.timestamp())}")
resp.raise_for_status()
maps = resp.json()['results']
for idx, map in enumerate(maps):
map_data = requests.get(
f"{decrypt_env_variable('CARTOGRAPHER_BASEURL')}{map['ref']}").json()
maps[idx]['uri'] = map_data['children'][0]['archivesspace_uri']
return maps
def get_aspace_previously_published(client):
"""Gets a list of previously published collections from an AWS bucket."""
object = client.get_object(
Bucket=decrypt_env_variable("BUCKET_NAME"),
Key=PREVIOUS_RESULTS_KEY)
return json.loads(object['Body'].read())
def update_aspace_previously_published(results, client):
"""Updates a list of previously published collections in an AWS bucket."""
client.put_object(
Bucket=decrypt_env_variable('BUCKET_NAME'),
Key=PREVIOUS_RESULTS_KEY,
Body=bytes(json.dumps(results), 'utf-8'))
if __name__ == "__main__":
main()