diff options
author | kaa <kaa@disroot.org> | 2023-11-25 12:40:23 -0800 |
---|---|---|
committer | kaa <kaa@disroot.org> | 2023-11-25 12:40:23 -0800 |
commit | 06b553ab3f6355e643049dad0ccb570a0bc0d613 (patch) | |
tree | ef758a08c2dea66085f11c333984f5e96d4ab444 | |
parent | 3df21ef899aafd2461570687ef3b9b1c9f9a555e (diff) |
-rw-r--r-- | cantree/config.go | 5 | ||||
-rw-r--r-- | cantree/go.mod | 3 | ||||
-rw-r--r-- | cantree/test/example.json | 1 | ||||
-rw-r--r-- | cantree/test/t.go | 17 | ||||
-rw-r--r-- | cantree/tree.go | 102 | ||||
-rw-r--r-- | makefile | 12 | ||||
-rw-r--r-- | old/canls.c | 70 | ||||
-rwxr-xr-x | old/dump.sh | 58 | ||||
-rwxr-xr-x | t | 3 | ||||
-rw-r--r-- | testing.c | 2 |
10 files changed, 268 insertions, 5 deletions
diff --git a/cantree/config.go b/cantree/config.go new file mode 100644 index 0000000..147a7ee --- /dev/null +++ b/cantree/config.go @@ -0,0 +1,5 @@ +package main + +const ( + base = `https://sdccd.instructure.com/api/v1/` +) diff --git a/cantree/go.mod b/cantree/go.mod new file mode 100644 index 0000000..14df6fb --- /dev/null +++ b/cantree/go.mod @@ -0,0 +1,3 @@ +module mesacsclub.com/go/cantree + +go 1.20 diff --git a/cantree/test/example.json b/cantree/test/example.json new file mode 100644 index 0000000..9fb056c --- /dev/null +++ b/cantree/test/example.json @@ -0,0 +1 @@ +{"id":17428885,"name":"my files","full_name":"my files","context_id":9064950,"context_type":"User","parent_folder_id":null,"created_at":"2023-08-18T04:06:27Z","updated_at":"2023-08-18T04:06:27Z","lock_at":null,"unlock_at":null,"position":null,"locked":false,"folders_url":"https://sdccd.instructure.com/api/v1/folders/17428885/folders","files_url":"https://sdccd.instructure.com/api/v1/folders/17428885/files","files_count":15,"folders_count":10,"hidden":null,"locked_for_user":false,"hidden_for_user":false,"for_submissions":false,"can_upload":true} diff --git a/cantree/test/t.go b/cantree/test/t.go new file mode 100644 index 0000000..203d6b9 --- /dev/null +++ b/cantree/test/t.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "encoding/json" + "io" + "os" +) + +func main() { + in, _ := os.Open("example.json") + contents, _ := io.ReadAll(in) + + var m map[string]interface{} + json.Unmarshal(contents, &m) + fmt.Printf("%v\n", m) +}
\ No newline at end of file diff --git a/cantree/tree.go b/cantree/tree.go new file mode 100644 index 0000000..8a7464f --- /dev/null +++ b/cantree/tree.go @@ -0,0 +1,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) + } +} @@ -3,13 +3,17 @@ CFLAGS = -std=c89 -g -Wall #CC = tcc #CFLAGS = -Wall -g LDFLAGS = -static -OUT = canup testing canls +OUT = canup testing cantree/cantree all: $(OUT) +cantree/cantree: + cd cantree && go build + +dump: + dump.sh + .c: $(CC) $(LDFLAGS) $(CFLAGS) -o $@ $< strip $@ clean: - rm $(OUT) -dist: clean - rm -rf dump/* + rm -f $(OUT) diff --git a/old/canls.c b/old/canls.c new file mode 100644 index 0000000..02a41e2 --- /dev/null +++ b/old/canls.c @@ -0,0 +1,70 @@ +#include <stdio.h> +#include <stdlib.h> /* calloc */ +#include <string.h> /* memmove */ +#include <unistd.h> /* chdir */ +#include "shared.h" + +static char *read_dir(FILE *dir_dump, char *buf) +{ + static char *contents = NULL; + int read, size = 0; + while ((read = fread(buf, 1, bufsize, dir_dump)) > 0) { + size += read; + contents = realloc(contents, size); + memmove(&contents[size-read], buf, read); + } + return contents; +} + +const char *key_delim = "\":"; +/* Return character pointer to beginning of value. */ +char * +getval(char *str, char *key) +{ + key = realloc(key, strlen(key) + strlen(key_delim) + 1); + strcat(key, key_delim); + char *rt = strstr(str, key); + if (rt == NULL) { + fprintf(stderr, "%s\n", "Invalid key.\n"); + } + return rt; +} + +/* Count directories. */ +int +count_dir(char *contents) +{ + int count = 0; + char *p = contents; + while ((p = strstr(p, "\n")) != NULL) { + count++; + p = &p[1]; /* Increment p past the current '\n'. */ + } + return count; +} + +int +main(int argc, char **argv) +{ + + char *path = NULL; + if (argc > 1) { + path = argv[1]; + } + + chdir("dump"); + FILE *root_stream = fopen("folders", "r"); + if (root_stream == NULL) { + fprintf(stderr, "Generate a dump of your Canvas" + " account directory structure" + " using dump.sh.\n"); + return 1; + } + + char buf[bufsize]; + char *root_contents = read_dir(root_stream, buf); + int root_count = count_dir(root_contents); + printf("%d\n", root_count); + + return 0; +} diff --git a/old/dump.sh b/old/dump.sh new file mode 100755 index 0000000..bc50cfb --- /dev/null +++ b/old/dump.sh @@ -0,0 +1,58 @@ +#!/bin/sh + +if [ -z $CANKEY ] +then + echo Provide an API key in the environment variable CANKEY. + return 1 +fi +if ! [ -d dump ] +then + mkdir dump +fi +cd dump +base=https://sdccd.instructure.com/api/v1/ +process_json() +{ + jq -M +} +get() +{ + url=$base$1 + curl -s -X GET "$url" -H "Authorization: Bearer $CANKEY" | jq -M +} + +get users/self/folders > folders +get users/self/files > files + +get_id() +{ + < folders sed -e 's/,.*//g' -e 's/.*://g' +} +get_name() +{ + < folders grep id.:$1 | sed -e 's/.*name":"//' -e 's/",.*//' +} + +# Recursively resolve directories and files. +# Empty response: [] +get_dir() +{ + id=$1 + name=$2 + echo $id + mkdir $id + cd $id + echo $name > name + get folders/$id/files > files + get folders/$id/folders > folders + for i in $(get_id) + do + get_dir $i "$(get_name $i)" + done + cd .. +} + +for i in $(get_id) +do + get_dir $i +done @@ -0,0 +1,3 @@ +#!/bin/sh + +testing $1 | jq -M @@ -22,7 +22,7 @@ main(int argc, char **argv) char *cmd = calloc(cmdsize, sizeof(char)); snprintf(cmd, cmdsize, "curl -X GET '%s%s' -H 'Authorization: Bearer %s'", base, argv[1], upload_key); - puts(cmd); + /*puts(cmd);*/ system(cmd); return 0; } |