-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.luau
255 lines (204 loc) · 8.36 KB
/
main.luau
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
--!strict
--!nolint LocalShadow
-- by dev chrysalis
-- downloads recent files of canvas so we don't have to :p
local fs = require("@lune/fs")
local net = require("@lune/net")
local process = require("@lune/process")
local datetime = require("@lune/datetime")
local fromIsoDate = datetime.fromIsoDate
local jsonDecode = net.jsonDecode
local exit = process.exit
local List = require("libraries/Lists")
type List = List.List
type ClassInfo = {classid: string, output_dir: string, query: "Files" | "Modules"}
type FileProperties = {
display_name: string,
filename: string,
id: number,
updated_at: string,
url: string,
[string]: any
}
local function readConfig()
assert(fs.isFile(".config.jsonc"), "no .config.jsonc found! please copy the template jsonc and add the relevant info and save it as '.config.jsonc' within this directory.")
local raw_config = fs.readFile(".config.jsonc"):gsub("%s//[%w%p ]*\n", "") -- need to remove comments from jsonc at runtime :/
local config = net.jsonDecode(raw_config)
-- validate api key exists
assert(config.CANVAS_API_KEY ~= "", "Missing Canvas API Key/Token; please generate one in settings.")
local invalid_dirs = List.new()
local classes = List.mapped(config.Classes, function(class: string, info: ClassInfo)
local dir_exists = fs.isDir(info.output_dir)
if dir_exists then
if not info.query then
info.query = "Files"
end
return class, info
else
invalid_dirs:append( {class, info.output_dir} )
return nil
end
end)
if not invalid_dirs.is_empty then
print("Error: invalid output_dirs:")
print(invalid_dirs:table())
exit(1)
end
config.Classes = classes
return config
end
local function getFilesFromCanvasNew(API_KEY: string, BASE_URL: string, Classes: List, search_term: string?)
local function callApi(query: string)
local response = process.spawn("curl", {BASE_URL .. query .. "access_token=" .. API_KEY})
if response.ok then
local result = jsonDecode(response.stdout)
if result.status == "unauthenticated" then
error("User is unauthenticated; please check your URL and api key.")
else
return result
end
else
print("API REQUEST ERROR: response not okay.")
print(response)
exit(1)
return nil
end
end
local search_term = if search_term and search_term ~= "" then
`search_term={search_term}&`
else
""
local queries = {
Files = "/files?sort=created_at&order=desc&" .. search_term,
Modules = "/modules",
}
local select_fields = List.new("display_name", "filename", "id", "updated_at", "url")
local files_by_class = Classes:mappairs(function(class: string, info: ClassInfo)
local response = callApi(queries[info.query])
local files_list = List.from(response)
local files = files_list:map(function(file: FileProperties)
return select_fields:map(function(field: string)
if field == "updated_at" then
return fromIsoDate(file[field]).unixTimestamp
else
return file[field]
end
end)
end)
return files
end)
return files_by_class
end
local function getFilesFromCanvas(API_KEY: string, BASE_URL: string, Classes: List, search_term: string?)
local search_term = if search_term ~= "" then `search_term={search_term}&`
else ``
local queries = {
files_query = "/files?sort=created_at&order=desc&" .. search_term,
modules_query = "/modules"
}
local select_fields = List.new("display_name", "filename", "id", "updated_at", "url")
local callApi = function(url)
local url = `{url}access_token={API_KEY}`
print(url)
exit()
local response = process.spawn("curl", {url})
if response.ok then
local decoded = jsonDecode(response.stdout)
if decoded.status == "unauthenticated" then
print(`URL: {url}`)
error("User is unauthenticated! Please check your API token.")
else
return decoded
end
else
print("Some other HTTP error occurred")
print(response)
error("http error")
end
end
return Classes:map_values(function(class, info: ClassInfo)
local url = BASE_URL .. info.classid .. queries.files_query
local response = callApi(url)
local files = List.from(response)
return files:map(function(file: FileProperties)
return List.mapped(file, function(property, value)
if not select_fields:has(property) then
return nil
end
if property == "updated_at" then
value = fromIsoDate(value).unixTimestamp
end
return property, value
end):table()
end)
end)
end
local function filterRecentFiles(files_by_class: List, days: number)
local days = days or 7
local today_unix = datetime.now().unixTimestamp
local seconds_in_day = 24 * 60^2
return files_by_class:remap_array_values(function(file: FileProperties, _, class: string)
if today_unix - file.updated_at < days * seconds_in_day then
return {
filename = file.filename,
download_link = file.url
}
end
return
end)
end
local function downloadFiles(recent_files: List, class_directories)
type File = {
filename: string,
download_link: string,
}
local function downloadFileToPath(file: File, path: string)
local result = process.spawn("curl", {"-L", `-o{file.filename}`, file.download_link}, {
cwd = path
})
if result.ok then
print(`- Successfully downloaded {file.filename} to:\n {path}/{file.filename}`)
else
print(`!! Download error: {file.filename};\n {result.stderr}`)
end
end
recent_files:mappairs(function(class, files: List)
print(`\n{class}:`)
local target_directory = class_directories[class]
local directory = List.from(fs.readDir(target_directory))
files:each(function(file: File)
if not directory:has(file.filename) then
downloadFileToPath(file, target_directory)
else
print(`- {file.filename} already exists; not downloading.`)
end
end)
return nil
end)
end
--- Procedural
local config = readConfig()
local API_KEY = config.CANVAS_API_KEY
local BASE_URL = config.BASE_URL .. "/api/v1/courses/"
local Classes = config.Classes
local args = table.concat(process.args, " "):split(",")
local day = args[1]:match("(%d+)")
local filter_days = tonumber(day) or 7
local search_term = args[2] or ""
search_term = search_term:gsub(" ", "")
local clear_caches = args[1]:match("clear%-cache")
local class_directories = Classes:map_values(function(name, info: ClassInfo)
return info.output_dir
end):table() :: any
-- print(search_term, clear_caches, filter_days)
if clear_caches then
for class, directory in class_directories do
fs.removeDir(directory)
fs.writeDir(directory)
print(`Cleared directory: {directory}`)
end
end
print(`\nFiltering: last {filter_days} days...`)
local files_by_class = getFilesFromCanvas(API_KEY, BASE_URL, Classes, search_term)
local recent_files_by_class = filterRecentFiles(files_by_class, filter_days)
downloadFiles(recent_files_by_class, class_directories)