Skip to content

Commit

Permalink
added additional functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgrbacbravo committed Dec 5, 2023
1 parent 0de4020 commit 3e9f13e
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 283 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.9.1
github.com/charmbracelet/log v0.3.1
github.com/joho/godotenv v1.5.1
)

require (
Expand All @@ -16,7 +17,6 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -26,6 +26,7 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
Expand All @@ -44,6 +46,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
Binary file modified main
Binary file not shown.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ func main() {
firstLoggedIn := client.GetFirstLoggedIn(&httpClient, baseURL)

logger.Info("UserData 🥸 :", "name", name, "email", email, "lastLoggedIn", lastLoggedIn, "firstLoggedIn", firstLoggedIn)

URL := "https://themis.housing.rug.nl/course/2023-2024/progfun/"
rootNode := tree.BuildRootAssignmentNode("root", URL, logger)
rootNode, err = tree.PullAssignmentsFromThemisAndBuildTree(&httpClient, URL, rootNode, 0, logger)
rootNode, err = tree.PullAssignmentsFromThemisAndBuildTree(&httpClient, URL, rootNode, 1, logger)
if err != nil {
log.Fatal(err)
return
}

}
11 changes: 11 additions & 0 deletions models/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

import (
"time"
)

type AssignmentDate struct {
StartDate time.Time
DueDate time.Time
EndDate time.Time
}
79 changes: 78 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"net/http"
"strings"
"themis-cli/models"
"time"

// goquery
"github.com/PuerkitoBio/goquery"
Expand All @@ -28,10 +30,82 @@ func extractCourseData(doc *goquery.Document) []map[string]string {
courses = append(courses, course)
}
})

return courses
}

func GetDatesFromAssignmentPage(client *http.Client, AssignmentPageURL string) (models.AssignmentDate, error) {
// Initialize an empty model
dates := models.AssignmentDate{}

// Send an HTTP GET request to get the assignment page
resp, err := client.Get(AssignmentPageURL)
if err != nil {
return dates, fmt.Errorf("error fetching assignment page: %v", err)
}
defer resp.Body.Close()

// Check HTTP response status
if resp.StatusCode != http.StatusOK {
return dates, fmt.Errorf("receiving non-OK response status %s", resp.Status)
}

// Create a new goquery document from the HTTP response body
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return dates, fmt.Errorf("error reading document: %v", err)
}

// Parse the start date .cfg-line .cfg-key .tip[data-title]
startDate, err := findDateInTooltip(doc, ".cfg-line:contains('Start:') .tip[data-title]")
if err != nil {
return dates, err
}

// Parse the deadline date .cfg-line .cfg-key .tip[data-title]
dueDate, err := findDateInTooltip(doc, ".cfg-line:contains('Deadline:') .tip[data-title]")
if err != nil {
return dates, err
}

// Parse the end date .cfg-line .cfg-key .tip[data-title]
endDate, err := findDateInTooltip(doc, ".cfg-line:contains('End:') .tip[data-title]")
if err != nil {
return dates, err
}

dates = models.AssignmentDate{
StartDate: startDate,
DueDate: dueDate,
EndDate: endDate,
}

return dates, nil
}

// findDateInTooltip finds the date within a tooltip element's data-title attribute
func findDateInTooltip(doc *goquery.Document, selector string) (time.Time, error) {
var parsedDate time.Time
timeLayout := "Mon Jan 02 2006 15:04:05 GMT-0700"

tooltip := doc.Find(selector).First()
dateString, exists := tooltip.Attr("data-title")
if !exists {
return parsedDate, fmt.Errorf("no tooltip with date found")
}

// Extract just the date part from the complex string
dateParts := strings.Split(dateString, " ")
dateString = strings.Join(dateParts[:6], " ")

// Parse the dateString into Go's time.Time
parsedDate, err := time.Parse(timeLayout, dateString)
if err != nil {
return parsedDate, fmt.Errorf("error parsing date: %v", err)
}

return parsedDate, nil
}

func GetAssignmentsOnPage(client *http.Client, URL string) ([]map[string]string, error) {
var assignments []map[string]string

Expand All @@ -46,13 +120,16 @@ func GetAssignmentsOnPage(client *http.Client, URL string) ([]map[string]string,
if err != nil {
return nil, fmt.Errorf("error parsing assignments page: %v", err)
}
// TODO: read the dates from the inside the scanned assignement

// Find the assignments in the HTML
doc.Find("div.subsec.round.shade.ass-children ul.round li").Each(func(i int, s *goquery.Selection) {
assignment := make(map[string]string)
anchor := s.Find("span.ass-link a")
assignmentName := anchor.Text()

href, exists := anchor.Attr("href")

if exists {
assignment["name"] = strings.TrimSpace(assignmentName)
assignment["url"] = baseURL + href
Expand Down
8 changes: 8 additions & 0 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
log "github.com/charmbracelet/log"
)


const (
baseURL = "https://themis.housing.rug.nl"
)
Expand All @@ -20,6 +21,11 @@ type AssignmentNode struct {
children []*AssignmentNode
}

// Title returns the title of the node.
func (n *AssignmentNode) Title() string { return n.Name }
func (n *AssignmentNode) Description() string { return n.URL }
func (n *AssignmentNode) FilterValue() string { return n.Name }

// AppendChild appends a child node to the parent node.
// It sets the parent of the child node and adds the child node to the parent's list of children.
func (n *AssignmentNode) AppendChild(c *AssignmentNode, logger *log.Logger) {
Expand Down Expand Up @@ -71,3 +77,5 @@ func PullAssignmentsFromThemisAndBuildTree(client *http.Client, URL string, root

return rootNode, nil
}

// build
Loading

0 comments on commit 3e9f13e

Please sign in to comment.