Skip to content

Commit

Permalink
Config page completed
Browse files Browse the repository at this point in the history
  • Loading branch information
vpoluyaktov committed Nov 7, 2023
1 parent 1e56a3d commit f7bc816
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 95 deletions.
79 changes: 40 additions & 39 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ type Config struct {
LogFileName string `yaml:"LogFileName"`
OutputDir string `yaml:"OutputDir"`
LogLevel string `yaml:"LogLevel"`
MaxSearchRows int `yaml:"MaxSearchRows"`
SearchRowsMax int `yaml:"SearchRowsMax"`
UseMock bool `yaml:"UseMock"`
SaveMock bool `yaml:"SaveMock"`
SearchCondition string `yaml:"SearchCondition"`
ParrallelDownloads int `yaml:"ParrallelDownloads"`
ParrallelEncoders int `yaml:"ParrallelEncoders"`
ConcurrentDownloaders int `yaml:"ConcurrentDownloaders"`
ConcurrentEncoders int `yaml:"ConcurrentEncoders"`
ReEncodeFiles bool `yaml:"ReEncodeFiles"`
BitRate string `yaml:"BitRate"`
SampleRate string `yaml:"SampleRate"`
MaxFileSize string `yaml:"MaxFileSize"`
BitRateKbs int `yaml:"BitRateKbs"`
SampleRateHz int `yaml:"SampleRateHz"`
MaxFileSizeMb int `yaml:"MaxFileSizeMb"`
CopyToAudiobookshelf bool `yaml:"CopyToAudiobookshelf"`
AudiobookshelfUrl string `yaml:"AudiobookshelfUrl"`
AudiobookshelfUser string `yaml:"AudiobookshelfUser"`
Expand All @@ -60,18 +60,20 @@ func Load() {
config.LogFileName = "abb_ia.log"
config.OutputDir = "output"
config.LogLevel = "INFO"
config.MaxSearchRows = 25
config.SearchRowsMax = 25
config.UseMock = false
config.SaveMock = false
config.SearchCondition = ""
config.ParrallelDownloads = 5
config.ParrallelEncoders = 5
config.ConcurrentDownloaders = 5
config.ConcurrentEncoders = 5
config.ReEncodeFiles = true
config.BitRate = "128k"
config.SampleRate = "44100"
config.MaxFileSize = "100 Mb"
config.BitRateKbs = 96
config.SampleRateHz = 44100
config.MaxFileSizeMb = 250
config.CopyToAudiobookshelf = true
config.AudiobookshelfUser = "admin"
config.AudiobookshelfPassword = ""
config.AudiobookshelfLibrary = "Internet Archive"
config.AudiobookshelfDir = "/mnt/NAS/Audiobooks/Internet Archive"
config.ShortenTitles = true
config.Genres = []string{
Expand Down Expand Up @@ -145,12 +147,12 @@ func (c *Config) GetLogLevel() string {
return c.LogLevel
}

func (c *Config) SetMaxSearchRows(r int) {
c.MaxSearchRows = r
func (c *Config) SetSearchRowsMax(r int) {
c.SearchRowsMax = r
}

func (c *Config) GetMaxSearchRows() int {
return c.MaxSearchRows
func (c *Config) GetSearchRowsMax() int {
return c.SearchRowsMax
}

func (c *Config) SetUseMock(b bool) {
Expand All @@ -177,20 +179,20 @@ func (c *Config) GetSearchCondition() string {
return c.SearchCondition
}

func (c *Config) SetParallelDownloads(n int) {
c.ParrallelDownloads = n
func (c *Config) SetConcurrentDownloaders(n int) {
c.ConcurrentDownloaders = n
}

func (c *Config) GetParallelDownloads() int {
return c.ParrallelDownloads
func (c *Config) GetConcurrentDownloaders() int {
return c.ConcurrentDownloaders
}

func (c *Config) SetParallelEncoders(n int) {
c.ParrallelEncoders = n
func (c *Config) SetConcurrentEncoders(n int) {
c.ConcurrentEncoders = n
}

func (c *Config) GetParallelEncoders() int {
return c.ParrallelEncoders
func (c *Config) GetConcurrentEncoders() int {
return c.ConcurrentEncoders
}

func (c *Config) SetReEncodeFiles(b bool) {
Expand All @@ -201,29 +203,28 @@ func (c *Config) IsReEncodeFiles() bool {
return c.ReEncodeFiles
}

func (c *Config) SetBitRate(b string) {
c.BitRate = b
func (c *Config) SetBitRate(b int) {
c.BitRateKbs = b
}

func (c *Config) GetBitRate() string {
return c.BitRate
func (c *Config) GetBitRate() int {
return c.BitRateKbs
}

func (c *Config) SetSampleRate(b string) {
c.SampleRate = b
func (c *Config) SetSampleRate(b int) {
c.SampleRateHz = b
}

func (c *Config) GetSampleRate() string {
return c.SampleRate
func (c *Config) GetSampleRate() int {
return c.SampleRateHz
}

func (c *Config) GetMaxFileSize() int64 {
maxFileSize, err := utils.HumanToBytes(c.MaxFileSize)
if err != nil {
logger.Error("Config Loader Can't parse MaxFileSize: " + err.Error() + ". Using default 100 Mb")
maxFileSize, _ = utils.HumanToBytes("100 Mb")
}
return maxFileSize
func (c *Config) SetMaxFileSizeMb(s int) {
c.MaxFileSizeMb = s
}

func (c *Config) GetMaxFileSizeMb() int {
return c.MaxFileSizeMb
}

func (c *Config) SetCopyToAudiobookshelf(b bool) {
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/buildController.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *BuildController) startBuild(cmd *dto.BuildCommand) {
// build audiobook parts
c.stopFlag = false
c.filesBuild = make([]fileBuild, len(c.ab.Parts))
jd := utils.NewJobDispatcher(config.Instance().GetParallelEncoders())
jd := utils.NewJobDispatcher(config.Instance().GetConcurrentEncoders())
for i := range c.ab.Parts {
jd.AddJob(i, c.buildAudiobookPart, c.ab, i)
}
Expand All @@ -106,6 +106,7 @@ func (c *BuildController) startBuild(cmd *dto.BuildCommand) {
// }
jd.Start()

c.stopFlag = true
c.mq.SendMessage(mq.BuildController, mq.Footer, &dto.SetBusyIndicator{Busy: false}, false)
c.mq.SendMessage(mq.BuildController, mq.Footer, &dto.UpdateStatus{Message: ""}, false)
c.mq.SendMessage(mq.BuildController, mq.BuildPage, &dto.BuildComplete{Audiobook: cmd.Audiobook}, true)
Expand Down
13 changes: 10 additions & 3 deletions internal/controller/chaptersController.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *ChaptersController) createChapters(cmd *dto.ChaptersCreate) {
offset += mp3.Duration()
chapterNo++
chapterFiles = []dto.Mp3File{}
if partSize >= config.Instance().GetMaxFileSize() || i == len(c.ab.Mp3Files)-1 {
if partSize >= int64(config.Instance().GetMaxFileSizeMb())*1024*1024 || i == len(c.ab.Mp3Files)-1 {
part := dto.Part{Number: partNo, Size: partSize, Duration: partDuration, Chapters: partChapters}
c.ab.Parts = append(c.ab.Parts, part)
partNo++
Expand All @@ -130,7 +130,11 @@ func (c *ChaptersController) searchReplaceDescription(cmd *dto.SearchReplaceDesc
ab := cmd.Audiobook
searchStr := cmd.SearchStr
replaceStr := cmd.ReplaceStr
re := regexp.MustCompile(searchStr)
re, err := regexp.Compile(searchStr)
if err != nil {
return
}

description := re.ReplaceAllString(ab.Description, replaceStr)
ab.Description = description
c.mq.SendMessage(mq.ChaptersController, mq.ChaptersPage, &dto.RefreshDescriptionCommand{Audiobook: cmd.Audiobook}, true)
Expand All @@ -140,7 +144,10 @@ func (c *ChaptersController) searchReplaceChapters(cmd *dto.SearchReplaceChapter
ab := cmd.Audiobook
searchStr := cmd.SearchStr
replaceStr := cmd.ReplaceStr
re := regexp.MustCompile(searchStr)
re, err := regexp.Compile(searchStr)
if err != nil {
return
}

for partNo, p := range ab.Parts {
for chapterNo, _ := range p.Chapters {
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/copyController.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ func (c *CopyController) startCopy(cmd *dto.CopyCommand) {

c.stopFlag = false
c.filesCopy = make([]fileCopy, len(c.ab.Parts))
jd := utils.NewJobDispatcher(config.Instance().GetParallelDownloads())
jd := utils.NewJobDispatcher(config.Instance().GetConcurrentDownloaders())
for i := range c.ab.Parts {
jd.AddJob(i, c.copyAudiobookPart, c.ab, i)
}
go c.updateTotalCopyProgress()
jd.Start()

c.stopFlag = true
c.mq.SendMessage(mq.CopyController, mq.Footer, &dto.SetBusyIndicator{Busy: false}, false)
c.mq.SendMessage(mq.CopyController, mq.Footer, &dto.UpdateStatus{Message: ""}, false)
c.mq.SendMessage(mq.CopyController, mq.BuildPage, &dto.CopyComplete{Audiobook: cmd.Audiobook}, true)
Expand Down
5 changes: 3 additions & 2 deletions internal/controller/downloadController.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func (c *DownloadController) startDownload(cmd *dto.DownloadCommand) {
c.mq.SendMessage(mq.DownloadController, mq.DownloadPage, &dto.DisplayBookInfoCommand{Audiobook: c.ab}, true)

// download files
ia := ia_client.New(config.Instance().GetMaxSearchRows(), config.Instance().IsUseMock(), config.Instance().IsSaveMock())
ia := ia_client.New(config.Instance().GetSearchRowsMax(), config.Instance().IsUseMock(), config.Instance().IsSaveMock())
c.stopFlag = false
c.files = make([]fileDownload, len(item.AudioFiles))
jd := utils.NewJobDispatcher(config.Instance().GetParallelDownloads())
jd := utils.NewJobDispatcher(config.Instance().GetConcurrentDownloaders())
for i, iaFile := range item.AudioFiles {
localFileName := utils.SanitizeFilePath(filepath.Join(item.Dir, iaFile.Name))
c.ab.Mp3Files = append(c.ab.Mp3Files, dto.Mp3File{Number: i, FileName: localFileName, Size: iaFile.Size, Duration: iaFile.Length})
Expand All @@ -94,6 +94,7 @@ func (c *DownloadController) startDownload(cmd *dto.DownloadCommand) {

jd.Start()

c.stopFlag = true
c.mq.SendMessage(mq.DownloadController, mq.Footer, &dto.SetBusyIndicator{Busy: false}, false)
c.mq.SendMessage(mq.DownloadController, mq.Footer, &dto.UpdateStatus{Message: ""}, false)
c.mq.SendMessage(mq.DownloadController, mq.DownloadPage, &dto.DownloadComplete{Audiobook: cmd.Audiobook}, true)
Expand Down
5 changes: 3 additions & 2 deletions internal/controller/encodingController.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *EncodingController) startEncoding(cmd *dto.EncodeCommand) {
// re-encode files
c.stopFlag = false
c.files = make([]fileEncode, len(c.ab.Mp3Files))
jd := utils.NewJobDispatcher(config.Instance().GetParallelEncoders())
jd := utils.NewJobDispatcher(config.Instance().GetConcurrentEncoders())
for i, f := range c.ab.Mp3Files {
jd.AddJob(i, c.encodeFile, i, c.ab.OutputDir, f.FileName)
}
Expand All @@ -85,6 +85,7 @@ func (c *EncodingController) startEncoding(cmd *dto.EncodeCommand) {

jd.Start()

c.stopFlag = true
c.mq.SendMessage(mq.EncodingController, mq.Footer, &dto.SetBusyIndicator{Busy: false}, false)
c.mq.SendMessage(mq.EncodingController, mq.Footer, &dto.UpdateStatus{Message: ""}, false)
c.mq.SendMessage(mq.EncodingController, mq.EncodingPage, &dto.EncodingComplete{Audiobook: cmd.Audiobook}, true)
Expand All @@ -105,7 +106,7 @@ func (c *EncodingController) encodeFile(fileId int, outputDir string, fileName s
// launch ffmpeg process
_, err := ffmpeg.NewFFmpeg().
Input(filePath, "-f mp3").
Output(tmpFile, fmt.Sprintf("-f mp3 -ab %s -ar %s -vn", config.Instance().GetBitRate(), config.Instance().GetSampleRate())).
Output(tmpFile, fmt.Sprintf("-f mp3 -ab %dk -ar %d -vn", config.Instance().GetBitRate(), config.Instance().GetSampleRate())).
Overwrite(true).
Params("-hide_banner -nostdin -nostats -loglevel fatal").
SendProgressTo("http://127.0.0.1:" + strconv.Itoa(port)).
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/searchController.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *SearchController) performSearch(cmd *dto.SearchCommand) {
logger.Info(mq.SearchController + " received " + cmd.String())
c.mq.SendMessage(mq.SearchController, mq.Footer, &dto.UpdateStatus{Message: "Fetching Internet Archive items..."}, false)
c.mq.SendMessage(mq.SearchController, mq.Footer, &dto.SetBusyIndicator{Busy: true}, false)
ia := ia_client.New(config.Instance().GetMaxSearchRows(), config.Instance().IsUseMock(), config.Instance().IsSaveMock())
ia := ia_client.New(config.Instance().GetSearchRowsMax(), config.Instance().IsUseMock(), config.Instance().IsSaveMock())
resp := ia.Search(cmd.SearchCondition, "audio")
if resp == nil {
logger.Error(mq.SearchController + ": Failed to perform IA search with condition: " + cmd.SearchCondition)
Expand Down
8 changes: 4 additions & 4 deletions internal/ui/buildPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func (p *BuildPage) displayBookInfo(ab *dto.Audiobook) {
p.infoPanel.appendRow("Title:", ab.Title)
p.infoPanel.appendRow("Author:", ab.Author)
p.infoPanel.appendRow("Duration:", utils.SecondsToTime(ab.TotalDuration))
p.infoPanel.appendRow("Size:", utils.BytesToHuman(ab.IAItem.TotalSize))
p.infoPanel.appendRow("Files", strconv.Itoa(len(ab.IAItem.AudioFiles)))
p.infoPanel.appendRow("Size:", utils.BytesToHuman(ab.TotalSize))
p.infoPanel.appendRow("Parts:", strconv.Itoa(len(ab.Parts)))

p.buildTable.clear()
p.buildTable.showHeader()
Expand Down Expand Up @@ -178,7 +178,7 @@ func (p *BuildPage) stopBuild() {
func (p *BuildPage) updateFileBuildProgress(dp *dto.BuildFileProgress) {
// update file progress
col := 4
w := p.buildTable.getColumnWidth(col) - 10
w := p.buildTable.getColumnWidth(col) - 5
progressText := fmt.Sprintf(" %3d%% ", dp.Percent)
barWidth := int((float32((w - len(progressText))) * float32(dp.Percent) / 100))
progressBar := strings.Repeat("━", barWidth) + strings.Repeat(" ", w-len(progressText)-barWidth)
Expand All @@ -201,7 +201,7 @@ func (p *BuildPage) updateTotalBuildProgress(dp *dto.BuildProgress) {
infoCell.Text = fmt.Sprintf(" [yellow]Time elapsed: [white]%10s | [yellow]Files: [white]%10s | [yellow]Speed: [white]%12s | [yellow]ETA: [white]%10s", dp.Elapsed, dp.Files, dp.Speed, dp.ETA)

col := 0
w := p.progressTable.getColumnWidth(col) - 10
w := p.progressTable.getColumnWidth(col) - 5
progressText := fmt.Sprintf(" %3d%% ", dp.Percent)
barWidth := int((float32((w - len(progressText))) * float32(dp.Percent) / 100))
progressBar := strings.Repeat("▒", barWidth) + strings.Repeat(" ", w-len(progressText)-barWidth)
Expand Down
16 changes: 8 additions & 8 deletions internal/ui/chaptersPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,19 @@ func (p *ChaptersPage) displayBookInfo(ab *dto.Audiobook) {
}

func (p *ChaptersPage) displayParts(ab *dto.Audiobook) {
if len(ab.Parts) > 1 {
p.chaptersTable.clear()
p.chaptersTable.showHeader()
for _, part := range ab.Parts {
p.addPart(&part)
}
p.chaptersTable.clear()
p.chaptersTable.showHeader()
for _, part := range ab.Parts {
p.addPart(&part)
}
p.mq.SendMessage(mq.ChaptersPage, mq.TUI, &dto.DrawCommand{Primitive: nil}, false)
}

func (p *ChaptersPage) addPart(part *dto.Part) {
number := strconv.Itoa(part.Number)
p.chaptersTable.appendSeparator("", "", "", "", "Part Number "+number)
if len(p.ab.Parts) > 1 {
number := strconv.Itoa(part.Number)
p.chaptersTable.appendSeparator("", "", "", "", "Part Number "+number)
}
for _, chapter := range part.Chapters {
p.addChapter(&chapter)
}
Expand Down
Loading

0 comments on commit f7bc816

Please sign in to comment.