-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_handler.py
More file actions
117 lines (95 loc) · 4.84 KB
/
lambda_handler.py
File metadata and controls
117 lines (95 loc) · 4.84 KB
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
import boto3
import json
import os
import zipfile
from io import BytesIO
from urllib.parse import quote
import urllib3
from botocore.vendored import requests
s3 = boto3.client("s3")
cloudfront = boto3.client("cloudfront")
WEBHOOK = "https://discord.com/api/webhooks/<REDACTED>"
def lambda_handler(event, context):
BASE_URL = "<REDACTED>"
distribution_id = "E2K<REDACTED>"
# Get the S3 bucket and object key from the event
bucket = event["Records"][0]["s3"]["bucket"]["name"]
key = event["Records"][0]["s3"]["object"]["key"]
# Download the file from S3
zip_file = BytesIO()
s3.download_fileobj(bucket, key, zip_file)
# Check if the file is a ZIP file
if zipfile.is_zipfile(zip_file):
# Extract the ZIP file
with zipfile.ZipFile(zip_file) as zf:
# Read the README and VERSION files
try:
readme_contents = zf.read("README.txt").decode("utf-8")
version_contents = zf.read("VERSION.txt").decode("utf-8")
except KeyError:
print("README or VERSION file not found in the ZIP file.")
return {
"statusCode": 400,
"body": json.dumps("README or VERSION file not found in the ZIP file.")
}
# Extract the mod name from the key
mod_name = os.path.splitext(os.path.basename(key))[0]
# Download the repo.xml file
repo_xml = BytesIO()
s3.download_fileobj(bucket, "repo.xml", repo_xml)
repo_xml_content = repo_xml.getvalue().decode("utf-8")
# Replace new lines with the desired format
sanitized_readme_contents = readme_contents.replace('\r\n', ' ').replace('\r', ' ').replace('\n', ' ')
# Create the mod string
mod_str = f'<mod name="{mod_name}" version="{version_contents}" url="{BASE_URL + quote(key)}">{sanitized_readme_contents}</mod>'
# Update or create the mod element
mod_element_start = repo_xml_content.find(f'<mod name="{mod_name}"')
if mod_element_start != -1:
# Update the existing mod element
mod_element_end = repo_xml_content.find('</mod>', mod_element_start) + len('</mod>')
repo_xml_content = repo_xml_content[:mod_element_start] + mod_str + repo_xml_content[mod_element_end:]
else:
# Create a new mod element
mod_list_end = repo_xml_content.find('</mod_list>')
repo_xml_content = repo_xml_content[:mod_list_end] + ' ' + mod_str + '\n' + repo_xml_content[mod_list_end:]
# Upload the updated repo.xml file to S3
repo_xml = BytesIO(repo_xml_content.encode('utf-8'))
s3.upload_fileobj(repo_xml, bucket, "repo.xml")
# Clone the repo.xml file to mods/repo.xml
s3.copy_object(Bucket=bucket, CopySource=bucket + "/repo.xml", Key="mods/repo.xml")
# Invalidate the CloudFront cache for the repo.xml file and the mods/repo.xml file
cloudfront.create_invalidation( DistributionId=distribution_id, InvalidationBatch={ 'Paths': { 'Quantity': 2, 'Items': [ '/repo.xml', '/mods/repo.xml' ] }, 'CallerReference': 'ovgme-repo-xml-invalidation' } )
# Invalidate the CloudFront cache for the mod file
cloudfront.create_invalidation( DistributionId=distribution_id, InvalidationBatch={ 'Paths': { 'Quantity': 1, 'Items': [ f'/{quote(key)}' ] }, 'CallerReference': 'ovgme-mod-invalidation' } )
# Send a Discord notification about the mod being uploaded, including the mod name and version as fields in an embed message.
data = {
"username" : "OvGME"
}
data['embeds'] = [
{
"title": "Mod Added or Changed",
"description": f"**{mod_name}**",
"author": {
"name": "OvGME Lambda Handler",
"url": "https://us-east-2.console.aws.amazon.com/lambda/home?region=us-east-2#/functions/<REDACTED>"
},
"fields": [
{
"name": "Version",
"value": f"{version_contents}"
},
{
"name": "URL",
"value": f"{BASE_URL + quote(key)}"
},
]
}
]
http = urllib3.PoolManager()
r = http.request('POST', WEBHOOK, headers={'Content-Type': 'application/json'}, body=json.dumps(data))
else:
print("The file is not a ZIP file.")
return {
"statusCode": 200,
"body": json.dumps("Function executed successfully.")
}