Skip to content

Commit

Permalink
Merge pull request #8 from soumitradev/fix-chunk-write
Browse files Browse the repository at this point in the history
Fix and optimize chunk write
  • Loading branch information
pnicto authored Oct 7, 2023
2 parents 210dee1 + d89bde4 commit 5fa5017
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
9 changes: 3 additions & 6 deletions impartus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 21 additions & 3 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"strings"
)

func ChooseCourse(courses Courses) int {
Expand All @@ -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)
Expand All @@ -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")
}
14 changes: 13 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5fa5017

Please sign in to comment.