Skip to content

Commit

Permalink
feat: show progress better using mpb
Browse files Browse the repository at this point in the history
  • Loading branch information
pnicto committed Apr 10, 2024
1 parent 088332a commit 9a10e34
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
24 changes: 18 additions & 6 deletions impartus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
"regexp"
"strings"

"github.com/schollz/progressbar/v3"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)

type (
Expand Down Expand Up @@ -331,7 +332,7 @@ type DownloadedPlaylist struct {
Playlist ParsedPlaylist
}

func DownloadPlaylist(playlist ParsedPlaylist) DownloadedPlaylist {
func DownloadPlaylist(playlist ParsedPlaylist, p *mpb.Progress) DownloadedPlaylist {
config := GetConfig()
var downloadedPlaylist DownloadedPlaylist

Expand All @@ -346,7 +347,13 @@ func DownloadPlaylist(playlist ParsedPlaylist) DownloadedPlaylist {
decryptionKey := getDecryptionKey(keyUrlContent)

if len(playlist.FirstViewURLs) > 0 && config.Views != "left" {
bar := progressbar.NewOptions64(-1, progressbar.OptionSetDescription(fmt.Sprintf("Lec %03d downloading right view chunks", playlist.SeqNo)))
b := p.AddBar(int64(len(playlist.FirstViewURLs)),
mpb.PrependDecorators(
decor.Name(fmt.Sprintf("Downloading Lec %03d left view ", playlist.SeqNo)),
),
mpb.AppendDecorators(decor.Percentage()),
)

for i, url := range playlist.FirstViewURLs {
f, err := downloadUrl(url, playlist.Id, i, "first")
if err != nil {
Expand All @@ -356,12 +363,17 @@ func DownloadPlaylist(playlist ParsedPlaylist) DownloadedPlaylist {
}
chunkPath := decryptChunk(f, decryptionKey)
downloadedPlaylist.FirstViewChunks = append(downloadedPlaylist.FirstViewChunks, chunkPath)
bar.Add(1)
b.Increment()
}
}

if len(playlist.SecondViewURLs) > 0 && config.Views != "right" {
bar := progressbar.NewOptions64(-1, progressbar.OptionSetDescription(fmt.Sprintf("Lec %03d downloading left view chunks", playlist.SeqNo)))
b := p.AddBar(int64(len(playlist.FirstViewURLs)),
mpb.PrependDecorators(
decor.Name(fmt.Sprintf("Downloading Lec %03d right view ", playlist.SeqNo)),
),
mpb.AppendDecorators(decor.Percentage()),
)
for i, url := range playlist.SecondViewURLs {
f, err := downloadUrl(url, playlist.Id, i, "second")
if err != nil {
Expand All @@ -371,7 +383,7 @@ func DownloadPlaylist(playlist ParsedPlaylist) DownloadedPlaylist {
}
chunkPath := decryptChunk(f, decryptionKey)
downloadedPlaylist.SecondViewChunks = append(downloadedPlaylist.SecondViewChunks, chunkPath)
bar.Add(1)
b.Increment()
}
}

Expand Down
29 changes: 28 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package main
import (
"fmt"
"log"
"math"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"

"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)

func main() {
Expand Down Expand Up @@ -69,16 +73,38 @@ func main() {
numWorkers := config.NumWorkers
playlistJobs := make(chan ParsedPlaylist, numWorkers)

p := mpb.New()

downloadBar := p.AddBar(int64(len(playlists)),
mpb.PrependDecorators(
decor.Name("Downloaded "),
decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
),
mpb.AppendDecorators(decor.Percentage()),
mpb.BarPriority(math.MaxInt64-1),

Check failure on line 84 in main.go

View workflow job for this annotation

GitHub Actions / release

cannot use math.MaxInt64 - 1 (untyped int constant 9223372036854775806) as int value in argument to mpb.BarPriority (overflows)
)

joiningBar := p.AddBar(int64(len(playlists)),
mpb.PrependDecorators(
decor.Name("Joined "),
decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
),
mpb.AppendDecorators(decor.Percentage()),
mpb.BarPriority(math.MaxInt64),

Check failure on line 93 in main.go

View workflow job for this annotation

GitHub Actions / release

cannot use math.MaxInt64 (untyped int constant 9223372036854775807) as int value in argument to mpb.BarPriority (overflows)
)

var joinWg sync.WaitGroup
for i := 0; i < numWorkers; i++ {
go func() {
for playlist := range playlistJobs {
// fmt.Println("Downloading playlist: ", playlist.Title, playlist.SeqNo)
downloadedPlaylist := DownloadPlaylist(playlist)
downloadedPlaylist := DownloadPlaylist(playlist, p)
metadataFile := CreateTempM3U8File(downloadedPlaylist)
downloadBar.Increment()
// fmt.Println("Downloaded playlist: ", playlist.Title, playlist.SeqNo)

go func(file M3U8File) {
defer joiningBar.Increment()
defer joinWg.Done()
// fmt.Println("Joining chunks for: ", file.Playlist.Title, file.Playlist.SeqNo)
var left, right string
Expand All @@ -105,6 +131,7 @@ func main() {
}

joinWg.Wait()
p.Wait()
close(playlistJobs)

fmt.Print("\n\n")
Expand Down

0 comments on commit 9a10e34

Please sign in to comment.