Skip to content

fetch ctftime info with a spinner and display it only when its all ready #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 51 additions & 15 deletions commands/ctftime.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"time"
)

type CTFTimeInfo struct {
Title string
Start time.Time
Finish time.Time
}

func init() {
Commands = append(Commands, Command{
Name: "ctftime",
Expand All @@ -37,7 +43,20 @@ func CtfTimeHelp() string {
"the ctftime urls will be used to aid in the generation of writeups with the " + theme.ColorGreen + "`writeup`" + theme.ColorReset + " command\n\n"
}

func showStats(ctfTimeUrl string) {
func ctftimeSpinner() {
emojis := []string{
"🕛", "⏳", "🕧", "🕐", "⏰", "🕞", "⏲️", "🚩",
}

for {
for _, e := range emojis {
fmt.Printf("\r%s", e)
time.Sleep(200 * time.Millisecond)
}
}
}

func fetchInfo(ctfTimeUrl string, info *CTFTimeInfo) {
splits := strings.Split(ctfTimeUrl, "/")
eventId := splits[len(splits)-1]

Expand Down Expand Up @@ -68,36 +87,41 @@ func showStats(ctfTimeUrl string) {
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
}

title := result["title"]
fmt.Printf(theme.ColorGray+"title: "+theme.ColorBlue+"%s"+theme.ColorReset+"\n", title)

now := time.Now()
info.Title = fmt.Sprintf("%s", result["title"])

start := fmt.Sprintf("%s", result["start"])
startTime, err := time.Parse(time.RFC3339, start)
info.Start, err = time.Parse(time.RFC3339, start)

if err != nil {
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
}

finish := fmt.Sprintf("%s", result["finish"])
finishTime, err := time.Parse(time.RFC3339, finish)
info.Finish, err = time.Parse(time.RFC3339, finish)

if err != nil {
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
}
}

func showInfo(info *CTFTimeInfo) {
fmt.Printf(theme.ColorGray+"title: "+theme.ColorBlue+"%s"+theme.ColorReset+"\n", info.Title)

now := time.Now()

fmt.Printf(theme.ColorGray+"start: "+theme.ColorGreen+"%v "+theme.ColorGray+"finish: "+theme.ColorRed+"%v"+theme.ColorReset+"\n", startTime, finishTime)
fmt.Printf(theme.ColorGray+"start: "+theme.ColorGreen+"%v "+theme.ColorGray+"finish: "+theme.ColorRed+"%v"+theme.ColorReset+"\n", info.Start, info.Finish)

started := startTime.Before(now)
finished := finishTime.Before(now)
started := info.Start.Before(now)
finished := info.Finish.Before(now)
ongoing := started && !finished

if ongoing {
fmt.Printf(theme.ColorGray+"time remaining: "+theme.ColorReset+"%v\n", finishTime.Sub(now))
fmt.Printf(theme.ColorGray+"time remaining: "+theme.ColorReset+"%v\n", info.Finish.Sub(now))
} else if finished {
fmt.Printf(theme.ColorGray+"time since finish: "+theme.ColorReset+"%v\n", finishTime.Sub(now))
fmt.Printf(theme.ColorGray+"time since finish: "+theme.ColorReset+"%v\n", info.Finish.Sub(now))
} else {
fmt.Printf(theme.ColorGray+"time to start: "+theme.ColorReset+"%v\n", startTime.Sub(now))
fmt.Printf(theme.ColorGray+"time to finish: "+theme.ColorReset+"%v\n", finishTime.Sub(now))
fmt.Printf(theme.ColorGray+"time to start: "+theme.ColorReset+"%v\n", info.Start.Sub(now))
fmt.Printf(theme.ColorGray+"time to finish: "+theme.ColorReset+"%v\n", info.Finish.Sub(now))
}
}

Expand All @@ -120,11 +144,23 @@ func CtfTime(args []string) {
return
}

go ctftimeSpinner()

infos := make([]CTFTimeInfo, len(config.CtfTimeUrls))

for idx, ctfTimeUrl := range config.CtfTimeUrls {
if strings.Contains(ctfTimeUrl, "ctftime.org") {
fetchInfo(ctfTimeUrl, &infos[idx])
}
}

fmt.Printf("\r")

for idx, ctfTimeUrl := range config.CtfTimeUrls {
fmt.Printf(theme.ColorGray+"url: "+theme.ColorReset+"%v"+theme.ColorReset+"\n", ctfTimeUrl)

if strings.Contains(ctfTimeUrl, "ctftime.org") {
showStats(ctfTimeUrl)
showInfo(&infos[idx])
if idx+1 < len(config.CtfTimeUrls) {
fmt.Printf("\n")
}
Expand Down