diff --git a/impartus.go b/impartus.go index 2099997..6310728 100644 --- a/impartus.go +++ b/impartus.go @@ -234,19 +234,16 @@ func downloadChunk(ttid int, resolution string, view string, chunk int) string { outFilepath := filepath.Join(config.TempDirLocation, fmt.Sprintf("%d_%04d_%s.ts.temp", ttid, chunk, view)) outFile, err := os.Create(outFilepath) + defer outFile.Close() if err != nil { fmt.Printf("Could not download chunk %d %d %v", ttid, chunk, err) } - outFileContent, err := io.ReadAll(resp.Body) - if err != nil { - fmt.Printf("Could not read chunk %d %d %v", ttid, chunk, err) - } - - _, err = outFile.Write(outFileContent) + _, err = io.Copy(outFile, resp.Body) if err != nil { fmt.Printf("Could not write chunk %d %d %v", ttid, chunk, err) } + outFile.Sync() return outFilepath } diff --git a/main.go b/main.go index 0dfd1f0..bf54d07 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,12 @@ func main() { courseIndex := ChooseCourse(courses) lectures := GetLectures(courses[courseIndex]) - startLectureIndex, endLectureIndex := ChooseLectures(lectures) - GetMetadata(lectures[startLectureIndex : endLectureIndex+1]) + startLectureIndex, endLectureIndex, skipEmptyLectures := ChooseLectures(lectures) + var chosenLectures Lectures + if skipEmptyLectures { + chosenLectures = removeEmptyLectures(lectures[startLectureIndex : endLectureIndex+1]) + } else { + chosenLectures = lectures[startLectureIndex : endLectureIndex+1] + } + GetMetadata(chosenLectures) } diff --git a/ui.go b/ui.go index cdec327..14cd0ea 100644 --- a/ui.go +++ b/ui.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "strings" ) func ChooseCourse(courses Courses) int { @@ -27,13 +28,13 @@ func ChooseCourse(courses Courses) int { return choice - 1 } -func ChooseLectures(lectures Lectures) (int, int) { +func ChooseLectures(lectures Lectures) (int, int, bool) { log.Println("User entered choose lecture") var startIndex int var endIndex int - fmt.Println("Choose a course to download from") + fmt.Println("Choose the lecture range you want to download") fmt.Println() for i, lecture := range lectures { fmt.Printf("%3d LEC %d %s\n", i+1, lecture.SeqNo, lecture.Topic) @@ -48,5 +49,22 @@ func ChooseLectures(lectures Lectures) (int, int) { log.Printf("User chose %d %d\n", startIndex, endIndex) log.Printf("Indices are %d %d\n", startIndex-1, endIndex-1) - return startIndex - 1, endIndex - 1 + var skipEmptyLectures string + + fmt.Println("Skip lectures with titles like 'No class' or 'No lecture'? [Y/n]") + fmt.Scanf("%s\n", &skipEmptyLectures) + skipEmptyLectures = strings.ToLower(skipEmptyLectures) + + for skipEmptyLectures != "y" && skipEmptyLectures != "n" && skipEmptyLectures != "" { + fmt.Println("Please enter a valid choice: [Y/n]") + fmt.Scanf("%s\n", &skipEmptyLectures) + } + + if skipEmptyLectures == "n" { + log.Printf("User chose not to skip empty lectures\n") + } else { + log.Printf("User chose to skip empty lectures\n") + } + + return startIndex - 1, endIndex - 1, (skipEmptyLectures != "n") } diff --git a/utils.go b/utils.go index c671b27..8ee1ab2 100644 --- a/utils.go +++ b/utils.go @@ -35,6 +35,18 @@ func CreateDirInsideDownloads(dirName string) string { func RemoveFile(path string) { if err := os.Remove(path); err != nil { - fmt.Printf("Could not remove %s because %v", path, err) + fmt.Printf("Could not remove %s because %v\n", path, err) } } + +// https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating +func removeEmptyLectures(lectures Lectures) Lectures { + filteredLectures := lectures[:0] + for _, lecture := range lectures { + lowercaseTitle := strings.ToLower(lecture.Topic) + if !(lowercaseTitle == "no class" || lowercaseTitle == "no lecture") { + filteredLectures = append(filteredLectures, lecture) + } + } + return filteredLectures +}