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
|
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"encoding/json"
"errors"
"io/fs"
"strconv"
)
var downloadKey string
func request(dirID string) []byte {
req, err := http.NewRequest("GET", base+dirID, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Authorization", "Bearer "+downloadKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
rt, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
return rt
}
func traverse(j map[string]interface{}) {
name := j["name"].(string)
/* Internally stored as float64.
Must be converted to int before string. */
id := strconv.Itoa(int(j["id"].(float64)))
fmt.Println(name, id)
var files []map[string]interface{}
var err error
str := "folders/" + string(id) + "/files"
fmt.Println(str)
err = json.Unmarshal(request("folders/"+string(id)+"/files"),
&files)
if err != nil {
log.Fatal(err)
}
for _, i := range files {
for i, j := range i {
fmt.Printf("%s %v\n", i, j)
}
}
return
err = os.Mkdir(name, 0777)
if err != nil {
log.Fatal(err)
}
_ = os.Chdir(name)
defer os.Chdir("..")
}
// Traverse and download the contents of the Canvas API.
func main() {
var set bool
downloadKey, set = os.LookupEnv("CANKEY")
if !set {
log.Fatal("Set the environment variable CANKEY to persistently contain your Canvas account API key.")
}
_, err := os.Stat("root")
if errors.Is(err, fs.ErrNotExist) {
os.Mkdir("root", 0777)
}
err = os.Chdir("root")
if err != nil {
log.Fatal(err)
}
body := request("users/self/folders")
var m []map[string]interface{}
err = json.Unmarshal(body, &m)
if err != nil {
log.Fatal(err)
}
for _, i := range m {
traverse(i)
}
}
|