Skip to content

Commit

Permalink
Merge pull request #318 from KJHJason/dev
Browse files Browse the repository at this point in the history
Filters Feature
  • Loading branch information
KJHJason authored Oct 21, 2024
2 parents 7bdced9 + ee635fb commit dd4c29c
Show file tree
Hide file tree
Showing 48 changed files with 1,564 additions and 744 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug-report-en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ body:
- "1009 (HTML Error)"
- "1010 (Captcha Error)"
- "1011 (Start-up Error)"
- "1012 (Docker Error)"
validations:
required: true

Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug-report-jp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ body:
- "1009 (HTML Error)"
- "1010 (Captcha Error)"
- "1011 (Start-up Error)"
- "1012 (Docker Error)"
validations:
required: true

Expand Down
4 changes: 2 additions & 2 deletions backend/app/advanced_prefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func (a *App) GetFfmpegPath() string {
}

type UserAgentResponse struct {
UserAgent string
IsDefault bool
UserAgent string `json:"UserAgent"`
IsDefault bool `json:"IsDefault"`
}

func (a *App) GetUserAgent() UserAgentResponse {
Expand Down
4 changes: 2 additions & 2 deletions backend/app/app.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package app

import (
"container/list"
"context"
"time"

"github.com/KJHJason/Cultured-Downloader-Logic/gdrive"
"github.com/KJHJason/Cultured-Downloader-Logic/utils/threadsafe"
"github.com/KJHJason/Cultured-Downloader/backend/appdata"
"github.com/KJHJason/Cultured-Downloader/backend/notifier"
)
Expand All @@ -15,7 +15,7 @@ type App struct {
ctx context.Context
appData *appdata.AppData
lang string
downloadQueues list.List // doubly linked list of DownloadQueue
downloadQueues threadsafe.DoublyLinkedList[*DownloadQueue] // list.List // doubly linked list of DownloadQueue
queueTicker *time.Ticker
gdriveClient *gdrive.GDrive
notifier notifier.Notifier
Expand Down
77 changes: 54 additions & 23 deletions backend/app/download.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package app

import (
"container/list"
"context"
"sync"

"github.com/KJHJason/Cultured-Downloader-Logic/progress"
"github.com/KJHJason/Cultured-Downloader-Logic/utils/threadsafe"
)

type Input struct {
Expand Down Expand Up @@ -55,43 +55,69 @@ func (a *App) addNewDownloadQueue(ctx context.Context, cancelFunc context.Cancel
mu: sync.Mutex{},
inputs: dlInfo.inputs,
}
a.downloadQueues.PushBack(dlQueue)
a.downloadQueues.Append(dlQueue)
return dlQueue
}

func (a *App) getQueueEl(id int) (*list.Element, *DownloadQueue) {
if a.downloadQueues.Len() == 0 {
return nil, nil
func (a *App) getTraversalDirection(id int) (direction threadsafe.TraversalDirection, ok bool) {
queueLen := a.downloadQueues.Len()
if queueLen == 0 {
return -1, false
}
onlyOneQueue := queueLen == 1

// check if id is valid since we are using a counter based id system
// Retrieve the first and last queue
firstEl := a.downloadQueues.Front()
lastEl := a.downloadQueues.Back()
firstQueue := firstEl.Value.(*DownloadQueue)
lastQueue := lastEl.Value.(*DownloadQueue)
var lastEl *threadsafe.DoublyLinkedListNode[*DownloadQueue]
if onlyOneQueue {
lastEl = firstEl // since the head and tail will point to the same node
} else {
lastEl = a.downloadQueues.Back()
}

firstQueue, lastQueue := firstEl.Value, lastEl.Value
// check if id is valid since we are using a counter based id system
if id < firstQueue.id || id > lastQueue.id {
return nil, nil
return -1, false
}

if onlyOneQueue { // since the head and tail points to the same node...
// Just traverse from head.
return threadsafe.TraverseFromHead, true
}

// Decide which direction is the best to iterate through the list
// by comparing the distance between the first and last element of the list
var dlQueue *list.Element
var direction int
if id-firstQueue.id < lastQueue.id-id {
dlQueue = firstEl
direction = 1
// dlQueue = firstEl
return threadsafe.TraverseFromHead, true
} else {
dlQueue = lastEl
direction = -1
// dlQueue = lastEl
return threadsafe.TraverseFromTail, true
}
}

func (a *App) getQueueEl(id int) (*threadsafe.DoublyLinkedListNode[*DownloadQueue], *DownloadQueue) {
traversalDirection, ok := a.getTraversalDirection(id)
if !ok {
return nil, nil
}
traverseFromHead := traversalDirection == threadsafe.TraverseFromHead

var dlQueue *threadsafe.DoublyLinkedListNode[*DownloadQueue]
if traverseFromHead {
dlQueue = a.downloadQueues.Front()
} else {
dlQueue = a.downloadQueues.Back()
}

for dlQueue != nil {
el := dlQueue.Value.(*DownloadQueue)
el := dlQueue.Value
if el.id == id {
return dlQueue, el
}

if direction == 1 {
if traverseFromHead {
dlQueue = dlQueue.Next()
} else {
dlQueue = dlQueue.Prev()
Expand All @@ -101,13 +127,18 @@ func (a *App) getQueueEl(id int) (*list.Element, *DownloadQueue) {
}

func (a *App) DeleteQueue(id int) {
listEl, queue := a.getQueueEl(id)
if queue == nil || listEl == nil {
traversalDirection, ok := a.getTraversalDirection(id)
if !ok {
return
}

queue.CancelQueue()
a.downloadQueues.Remove(listEl)
a.downloadQueues.RemoveViaFn(traversalDirection, func(curValue *DownloadQueue) bool {
if curValue.id != id {
return false
}
curValue.CancelQueue()
return true
})
}

func (a *App) CancelQueue(id int) {
Expand All @@ -122,7 +153,7 @@ func (a *App) CancelQueue(id int) {
func (a *App) startNewQueues() {
// loop through the doubly linked list of download queues
for e := a.downloadQueues.Front(); e != nil; e = e.Next() {
dq := e.Value.(*DownloadQueue)
dq := e.Value
if active, finished := dq.GetStatus(); active || finished {
continue
}
Expand Down
45 changes: 13 additions & 32 deletions backend/app/download_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"fmt"
"strings"

cdlConst "github.com/KJHJason/Cultured-Downloader-Logic/constants"
"github.com/KJHJason/Cultured-Downloader-Logic/iofuncs"
"github.com/KJHJason/Cultured-Downloader-Logic/progress"
"github.com/KJHJason/Cultured-Downloader/backend/constants"
)

type FrontendDownloadQueue struct {
Expand Down Expand Up @@ -74,40 +72,14 @@ func formatFrontendDlDetails(dlProgressBars []*progress.DownloadProgressBar) []*
}
}

// Reverse the sub-slice of already finished download progress bars within
// Reverse the sub-slice of already finished download progress bars within
// dlDetails so that the recently finished download progress bar is at the top
for i, j := donePtr + 1, lastIdx; i <= j; i, j = i+1, j-1 {
for i, j := donePtr+1, lastIdx; i <= j; i, j = i+1, j-1 {
dlDetails[i], dlDetails[j] = dlDetails[j], dlDetails[i]
}
return dlDetails
}

func checkNestedProgBarForErrors(dlQueue *DownloadQueue) bool {
hasError := false
nestedProgBars := dlQueue.mainProgressBar.nestedProgBars

lastElIdx := len(nestedProgBars) - 1
for idx, nestedProgBar := range nestedProgBars {
if !hasError && nestedProgBar.HasError {
hasError = true
if dlQueue.website != constants.FANTIA {
// for those that doesn't have a captcha solver
continue
}

if nestedProgBar.ErrMsg != cdlConst.ERR_RECAPTCHA_STR {
continue
}

// check the next element if it has an error as the captcha error can be ignored if the next element has no error
if idx+1 <= lastElIdx && nestedProgBars[idx+1].HasError {
hasError = true
}
}
}
return hasError
}

func (a *App) GetFrontendDownloadDetails(id int) []*FrontendDownloadDetails {
_, dlQueue := a.getQueueEl(id)
if dlQueue == nil {
Expand All @@ -120,7 +92,7 @@ func (a *App) GetFrontendDownloadDetails(id int) []*FrontendDownloadDetails {
func (a *App) GetDownloadQueues() []FrontendDownloadQueue {
var queues []FrontendDownloadQueue
for e := a.downloadQueues.Back(); e != nil; e = e.Prev() {
val := e.Value.(*DownloadQueue)
val := e.Value

msg := val.mainProgressBar.GetBaseMsg()
if !val.mainProgressBar.GetIsSpinner() && strings.Contains(msg, "%d") {
Expand All @@ -133,14 +105,23 @@ func (a *App) GetDownloadQueues() []FrontendDownloadQueue {
errStringSlice[idx] = err.Error()
}

hasError := false
nestedProgBars := val.mainProgressBar.nestedProgBars
for _, nestedProgBar := range nestedProgBars {
if nestedProgBar.HasError {
hasError = true
break
}
}

queues = append(queues, FrontendDownloadQueue{
Id: val.id,
Website: val.website,
Msg: msg,
SuccessMsg: val.mainProgressBar.GetSuccessMsg(),
ErrMsg: val.mainProgressBar.GetErrorMsg(),
ErrSlice: errStringSlice,
HasError: checkNestedProgBarForErrors(val),
HasError: hasError,
Inputs: val.inputs,
ProgressBar: *val.mainProgressBar,
NestedProgressBar: val.mainProgressBar.nestedProgBars,
Expand Down
Loading

0 comments on commit dd4c29c

Please sign in to comment.