Skip to content

Commit

Permalink
feat: added days filter to 'auto' subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
pspiagicw committed Apr 4, 2024
1 parent 1e01f1a commit 67477c5
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ gox install github.com/pspiagicw/sinister@latest
- It will update the database and download the latest videos.
- It is designed to be used in a cron job or a systemd timer.
- It can be used to keep your video library up to date.
- It can filter videos according to flags provided.
- See `sinister auto --help` for more information.

![auto](./gifs/auto.gif)

Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased(v0.1.0)

### Added
- Added filter to the `auto` subcommand.
- Added LICENSE, README.md and gifs.
- Added 'auto' subcommand
- Added 'mark' subcommand.
Expand Down
4 changes: 4 additions & 0 deletions pkg/argparse/argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ type Opts struct {

// Subcommand fields
NoSpinner bool

// Auto fields
Days int
NoSync bool
}

func Parse(version string) *Opts {
Expand Down
18 changes: 18 additions & 0 deletions pkg/feed/feed.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package feed

import (
"time"

"github.com/pspiagicw/goreland"
)

type Feed struct {
Author Author `xml:"author"`
Entries []Entry `xml:"entry"`
Expand All @@ -20,3 +26,15 @@ type Entry struct {
type Link struct {
URL string `xml:"href,attr"`
}

func (e Entry) Date() time.Time {
layout := time.RFC3339

t, err := time.Parse(layout, e.Published)

if err != nil {
goreland.LogFatal("Error while parsing date: %v", err)
}

return t
}
5 changes: 5 additions & 0 deletions pkg/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func HelpAuto() {
[]string{
"Download unwatched videos automatically.",
})

pelp.Flags(
"flag",
[]string{"days", "no-sync"},
[]string{"Maximum number of days to download", "Don't sync"})
}
func HelpBinge() {
pelp.Print("Select a bunch of videos to download.")
Expand Down
46 changes: 43 additions & 3 deletions pkg/tui/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package tui

import (
"flag"
"fmt"
"time"

"github.com/pspiagicw/goreland"
"github.com/pspiagicw/sinister/pkg/argparse"
"github.com/pspiagicw/sinister/pkg/database"
"github.com/pspiagicw/sinister/pkg/feed"
"github.com/pspiagicw/sinister/pkg/help"
)

func parseAutoOpts(opts *argparse.Opts) {
flag := flag.NewFlagSet("sinister auto", flag.ExitOnError)
flag.IntVar(&opts.Days, "days", 0, "Maximum number of days to download")
flag.BoolVar(&opts.NoSync, "no-sync", false, "Disable spinner")
flag.Usage = help.HelpAuto
flag.Parse(opts.Args[1:])
}
Expand All @@ -21,18 +26,53 @@ func Auto(opts *argparse.Opts) {

goreland.LogInfo("Starting auto mode!!")
goreland.LogInfo("Synching with feeds...")
performUpdate(opts)

if !opts.NoSync {
performUpdate(opts)
}

goreland.LogSuccess("Synching with feeds completed!")
goreland.LogInfo("Downloading videos...")

entries := database.QueryAll()

entries = filterEntries(entries, opts)

goreland.LogInfo("%d videos matched your filter", len(entries))

for _, entry := range entries {
if entry.Watched == 0 {
goreland.LogInfo("Downloading %s", entry.Title)

if softConfirm(fmt.Sprintf("Download %s by %s ?", entry.Title, entry.Author.Name)) {
performDownload(opts, &entry)
}

}

goreland.LogSuccess("All downloads completed!")
}
func filterEntries(entries []feed.Entry, opts *argparse.Opts) []feed.Entry {
filtered := make([]feed.Entry, 0)

for _, entry := range entries {

t := entry.Date()

if validDate(t, opts) && entry.Watched == 0 {
filtered = append(filtered, entry)
}
}

return filtered
}

func validDate(t time.Time, opts *argparse.Opts) bool {
now := time.Now()

nDaysAgo := now.AddDate(0, 0, -opts.Days)

if t.After(nDaysAgo) {
return true
}

return false
}
24 changes: 24 additions & 0 deletions pkg/tui/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ func confirmDownload() {
goreland.LogFatal("User cancelled the download.")
}
}
func confirm(message, error string) {
confirm := false

prompt := survey.Confirm{
Message: message,
}

survey.AskOne(&prompt, &confirm)
if !confirm {
goreland.LogFatal(error)
}

}
func softConfirm(message string) bool {
confirm := false

prompt := survey.Confirm{
Message: message,
}

survey.AskOne(&prompt, &confirm)

return confirm
}
func promptSelection(label string, options []string) int {
prompt := &survey.Select{
Message: label,
Expand Down

0 comments on commit 67477c5

Please sign in to comment.