Skip to content

Commit

Permalink
feat: built tree struct to store data
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgrbacbravo committed Nov 18, 2023
1 parent 84af4aa commit ddfbe28
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 9 deletions.
1 change: 0 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,4 @@ func GetLastLoggedIn(client *http.Client, baseURL string) string {
})
output := userData["Last login:"]
return output[:15]

}
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module themis-cli

go 1.21.4

require github.com/PuerkitoBio/goquery v1.8.1
require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/joho/godotenv v1.5.1
)

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
golang.org/x/net v0.7.0 // indirect
)
Binary file modified main
Binary file not shown.
26 changes: 21 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"themis-cli/client"
"themis-cli/config"
"themis-cli/parser"
"themis-cli/tree"
)

const (
Expand Down Expand Up @@ -63,27 +64,42 @@ func main() {
log.Fatal(err)
return
}
for _, course := range courses {
courseNodes := make([]*tree.AssignmentNode, 0)
for i, course := range courses {
courseNodes = append(courseNodes, tree.BuildRootAssignmentNode(course["name"], course["url"]))
assignments, err := parser.GetAssignmentsOnPage(&httpClient, course["url"])

if err != nil {
log.Fatal(err)
return
}
for _, assignment := range assignments {
if i >= len(courseNodes) {
log.Println("Index out of range, skipping iteration")
continue
}
log.Default().Println(i)
currentAssignmentNode := tree.BuildAssignmentNode(courseNodes[i], assignment["name"], assignment["url"])
courseNodes[i].AppendChild(currentAssignmentNode)
subAssigments, err := parser.GetAssignmentsOnPage(&httpClient, assignment["url"])

if err != nil {
log.Fatal(err)
return
}

for _, subAssignment := range subAssigments {
log.Print(subAssignment["name"] + " " + subAssignment["url"])
activities, err := parser.GetAssignmentsOnPage(&httpClient, subAssignment["url"])
currentSubAssignmentNode := tree.BuildAssignmentNode(currentAssignmentNode, subAssignment["name"], subAssignment["url"])
currentAssignmentNode.AppendChild(currentSubAssignmentNode)
activity, err := parser.GetAssignmentsOnPage(&httpClient, subAssignment["url"])
if err != nil {
log.Fatal(err)
return
}
for _, activity := range activities {
log.Print(activity["name"] + " " + activity["url"])

for _, activity := range activity {
currentActivityNode := tree.BuildAssignmentNode(currentSubAssignmentNode, activity["name"], activity["url"])
currentSubAssignmentNode.AppendChild(currentActivityNode)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion models/models.go

This file was deleted.

18 changes: 18 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,21 @@ func GetAssignmentsOnPage(client *http.Client, URL string) ([]map[string]string,
})
return assignments, nil
}

func doesContainAssignmentsOnPage(client *http.Client, URL string) (isLeafNode bool, err error) {
exists := true
resp, err := client.Get(URL)
if err != nil {
return false, err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return false, err
}
doc.Find("div.subsec.round.shade.ass-children ul.round li").Each(func(i int, s *goquery.Selection) {
anchor := s.Find("span.ass-link a")
_, exists = anchor.Attr("href")
})
return exists, nil
}
32 changes: 32 additions & 0 deletions tree/tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tree

import (
"fmt"
"log"
)

type AssignmentNode struct {
parent *AssignmentNode
Name string
URL string
children []*AssignmentNode
}

func (n *AssignmentNode) AppendChild(c *AssignmentNode) {
log.Default().Println(fmt.Sprintf("Appending child %s to parent %s", c.Name, n.Name))
c.parent = n
n.children = append(n.children, c)
}

func BuildAssignmentNode(parent *AssignmentNode, name string, url string) *AssignmentNode {
log.Default().Println(fmt.Sprintf("Building node %s", name))
node := &AssignmentNode{
Name: name,
URL: url,
parent: parent,
}
return node
}
func BuildRootAssignmentNode(name string, url string) *AssignmentNode {
return BuildAssignmentNode(nil, name, url)
}
3 changes: 3 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
package ui



0 comments on commit ddfbe28

Please sign in to comment.