Skip to content

Commit 2f316b9

Browse files
committed
Custom nyaa query per entry
1 parent dd8db8b commit 2f316b9

File tree

8 files changed

+66
-21
lines changed

8 files changed

+66
-21
lines changed

anilist_app.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ func AniListApp(app *cli.App) *cli.App {
119119
Name: "alt",
120120
Usage: "choose an alternative title",
121121
},
122+
cli.StringFlag{
123+
Name: "custom",
124+
Usage: "Adds custom nyaa search query for the selected entry",
125+
},
122126
},
123127
},
124128
cli.Command{

anilist_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"github.com/aqatl/mal/anilist"
67
"github.com/aqatl/mal/oauth2"
8+
"github.com/urfave/cli"
79
"os"
810
"time"
9-
"fmt"
10-
"github.com/urfave/cli"
1111
)
1212

1313
type AniList struct {

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type Config struct {
3434

3535
ALSelectedID int
3636
ALStatus anilist.MediaListStatus
37+
38+
NyaaAlts []NyaaAlt
3739
}
3840

3941
func NewConfig() *Config {

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package main
22

33
import (
4-
"os"
54
"fmt"
65
"github.com/fatih/color"
7-
"path/filepath"
86
"github.com/urfave/cli"
97
"log"
8+
"os"
9+
"path/filepath"
1010
)
1111

1212
var dataDir = filepath.Join(homeDir(), ".mal")

mal_app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/aqatl/cliwait"
1112
"github.com/aqatl/mal/mal"
1213
"github.com/atotto/clipboard"
1314
"github.com/fatih/color"
1415
"github.com/skratchdot/open-golang/open"
1516
"github.com/urfave/cli"
16-
"github.com/aqatl/cliwait"
1717
)
1818

1919
func MalApp(app *cli.App) *cli.App {

mal_cache.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package main
22

33
import (
4-
"os"
5-
"github.com/aqatl/mal/mal"
6-
"time"
4+
"bufio"
75
"encoding/xml"
6+
"fmt"
7+
"github.com/aqatl/cliwait"
8+
"github.com/aqatl/mal/mal"
9+
"github.com/urfave/cli"
10+
"golang.org/x/crypto/ssh/terminal"
811
"io"
12+
"io/ioutil"
913
"log"
10-
"github.com/urfave/cli"
11-
"bufio"
12-
"fmt"
13-
"syscall"
14+
"os"
1415
"strings"
15-
"io/ioutil"
16-
"golang.org/x/crypto/ssh/terminal"
17-
"github.com/aqatl/cliwait"
16+
"syscall"
17+
"time"
1818
)
1919

2020
func loadCredentials(ctx *cli.Context) string {

nyaa_cui.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ func malNyaaCui(ctx *cli.Context) error {
3636
)
3737
}
3838

39+
type NyaaAlt struct {
40+
Query string
41+
Id int
42+
}
43+
3944
func alNyaaCui(ctx *cli.Context) error {
4045
al, err := loadAniList(ctx)
4146
if err != nil {
@@ -48,14 +53,33 @@ func alNyaaCui(ctx *cli.Context) error {
4853
return fmt.Errorf("no entry found")
4954
}
5055

56+
if alt := ctx.String("custom"); alt != "" {
57+
addCustomAlt(alt+" "+strings.Join(ctx.Args(), " "), cfg)
58+
return nil
59+
}
60+
61+
var customAlt *string = nil
62+
for _, alt := range cfg.NyaaAlts {
63+
if alt.Id == entry.Id {
64+
customAlt = &alt.Query
65+
break
66+
}
67+
}
68+
5169
searchTerm := entry.Title.UserPreferred
5270
if ctx.Bool("alt") {
5371
fmt.Printf("Select desired title\n\n")
54-
if searchTerm = chooseStrFromSlice(sliceOfEntryTitles(entry)); searchTerm == "" {
72+
alts := sliceOfEntryTitles(entry)
73+
if customAlt != nil {
74+
alts = append(alts, *customAlt)
75+
}
76+
if searchTerm = chooseStrFromSlice(alts); searchTerm == "" {
5577
return fmt.Errorf("no alternative titles")
5678
}
5779
} else if ctx.NArg() > 0 {
5880
searchTerm = strings.Join(ctx.Args(), " ")
81+
} else if customAlt != nil {
82+
searchTerm = *customAlt
5983
}
6084

6185
return startNyaaCui(
@@ -66,6 +90,22 @@ func alNyaaCui(ctx *cli.Context) error {
6690
)
6791
}
6892

93+
func addCustomAlt(newAlt string, cfg *Config) {
94+
// Assumes the entry ID is valid
95+
defer cfg.Save()
96+
for i, alt := range cfg.NyaaAlts {
97+
if alt.Id == cfg.ALSelectedID {
98+
cfg.NyaaAlts[i].Query = newAlt
99+
return
100+
}
101+
}
102+
103+
cfg.NyaaAlts = append(cfg.NyaaAlts, NyaaAlt{
104+
Id: cfg.ALSelectedID,
105+
Query: newAlt,
106+
})
107+
}
108+
69109
func startNyaaCui(cfg *Config, searchTerm, displayedInfo, quality string) error {
70110
gui, err := gocui.NewGui(gocui.Output256)
71111
defer gui.Close()
@@ -162,7 +202,7 @@ func (nc *nyaaCui) Layout(gui *gocui.Gui) error {
162202
gui.SetCurrentView(ncResultsView)
163203
nc.ResultsView = v
164204

165-
//TODO Better/clearer results printing
205+
// TODO Better/clearer results printing
166206
nc.DisplayedIndexes = make([]int, 0, len(nc.Results))
167207
for i, result := range nc.Results {
168208
if nc.TitleFilter != nil && !nc.TitleFilter.MatchString(result.Title) {
@@ -325,7 +365,7 @@ func (nc *nyaaCui) Download(yIdx int) {
325365
cmd := exec.Command(nc.Cfg.TorrentClientPath, nc.Cfg.TorrentClientArgs...)
326366
cmd.Args = append(cmd.Args, link)
327367
if len(nc.Cfg.TorrentClientArgs) > 0 {
328-
cmd.Args = cmd.Args[1:] //Why they include app name in the arguments???
368+
cmd.Args = cmd.Args[1:] // Why they include app name in the arguments???
329369
}
330370
if err := cmd.Start(); err != nil {
331371
gocuiReturnError(nc.Gui, err)

templates.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main
22

33
import (
4+
"github.com/aqatl/mal/mal"
45
"math"
56
"text/template"
6-
"github.com/aqatl/mal/mal"
77
)
88

99
const PrettyListTemplate = `No{{printf "%57s" "Title"}}{{printf "%8s" "Eps"}}{{printf "%6s" "Score"}}{{printf "%7s" "ID"}}
@@ -28,7 +28,6 @@ var PrettyList = template.Must(
2828
)
2929

3030
type PrettyListData struct {
31-
List []*mal.Anime
31+
List []*mal.Anime
3232
SelectedID int
3333
}
34-

0 commit comments

Comments
 (0)