Skip to content

Commit

Permalink
Implemented nyaa result classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aQaTL committed Aug 24, 2018
1 parent 1df4a94 commit 067d761
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
43 changes: 29 additions & 14 deletions nyaa_cui.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"regexp"

"github.com/aqatl/mal/dialog"
"github.com/aqatl/mal/nyaa_scraper"
ns "github.com/aqatl/mal/nyaa_scraper"
"github.com/fatih/color"
"github.com/jroimartin/gocui"
"github.com/urfave/cli"
Expand Down Expand Up @@ -75,8 +75,8 @@ func startNyaaCui(cfg *Config, searchTerm, displayedInfo string) error {

SearchTerm: searchTerm,
DisplayedInfo: displayedInfo,
Category: nyaa_scraper.AnimeEnglishTranslated,
Filter: nyaa_scraper.TrustedOnly,
Category: ns.AnimeEnglishTranslated,
Filter: ns.TrustedOnly,
}
gui.SetManager(nc)
nc.setGuiKeyBindings(gui)
Expand Down Expand Up @@ -109,10 +109,10 @@ type nyaaCui struct {

SearchTerm string
DisplayedInfo string
Category nyaa_scraper.NyaaCategory
Filter nyaa_scraper.NyaaFilter
Category ns.NyaaCategory
Filter ns.NyaaFilter

Results []nyaa_scraper.NyaaEntry
Results []ns.NyaaEntry
MaxResults int
MaxPages int
LoadedPages int
Expand All @@ -128,6 +128,10 @@ var cyan = color.New(color.FgCyan).SprintFunc()
var blue = color.New(color.FgBlue).SprintFunc()
var green = color.New(color.FgGreen).SprintFunc()

var boldRed = color.New(color.FgRed).Add(color.Bold).SprintFunc()
var boldGreen = color.New(color.FgGreen).Add(color.Bold).SprintFunc()
var boldYellow = color.New(color.FgYellow).Add(color.Bold).SprintFunc()

func (nc *nyaaCui) Layout(gui *gocui.Gui) error {
w, h := gui.Size()

Expand All @@ -153,8 +157,19 @@ func (nc *nyaaCui) Layout(gui *gocui.Gui) error {
if nc.TitleFilter != nil && !nc.TitleFilter.MatchString(result.Title) {
continue
}

title := result.Title
switch result.Class {
case ns.Default:
title = boldYellow(title)
case ns.Trusted:
title = boldGreen(title)
case ns.Danger:
title = boldRed(title)
}

fmt.Fprintln(v,
result.Title,
title,
red(result.Size),
cyan(result.DateAdded.Format("15:04 02-01-2006")),
green(result.Seeders),
Expand Down Expand Up @@ -243,10 +258,10 @@ func (nc *nyaaCui) GetEditor() func(v *gocui.View, key gocui.Key, ch rune, mod g
}

func (nc *nyaaCui) Reload() {
var resultPage nyaa_scraper.NyaaResultPage
var resultPage ns.NyaaResultPage
var searchErr error
f := func() {
resultPage, searchErr = nyaa_scraper.Search(nc.SearchTerm, nc.Category, nc.Filter)
resultPage, searchErr = ns.Search(nc.SearchTerm, nc.Category, nc.Filter)
}
jobDone, err := dialog.StuffLoader(dialog.FitMessage(nc.Gui, "Loading "+nc.SearchTerm), f)
if err != nil {
Expand Down Expand Up @@ -306,7 +321,7 @@ func (nc *nyaaCui) LoadNextPage() {
}
nc.LoadedPages++
go func() {
resultPage, _ := nyaa_scraper.SearchSpecificPage(
resultPage, _ := ns.SearchSpecificPage(
nc.SearchTerm,
nc.Category,
nc.Filter,
Expand All @@ -330,30 +345,30 @@ func (nc *nyaaCui) LoadNextPage() {
}

func (nc *nyaaCui) ChangeCategory() {
selIdxChan, cleanUp, err := dialog.ListSelect(nc.Gui, "Select category", nyaa_scraper.Categories)
selIdxChan, cleanUp, err := dialog.ListSelect(nc.Gui, "Select category", ns.Categories)
if err != nil {
gocuiReturnError(nc.Gui, err)
}
go func() {
idx, ok := <-selIdxChan
nc.Gui.Update(cleanUp)
if ok {
nc.Category = nyaa_scraper.Categories[idx]
nc.Category = ns.Categories[idx]
nc.Reload()
}
}()
}

func (nc *nyaaCui) ChangeFilter() {
selIdxChan, cleanUp, err := dialog.ListSelect(nc.Gui, "Select filter", nyaa_scraper.Filters)
selIdxChan, cleanUp, err := dialog.ListSelect(nc.Gui, "Select filter", ns.Filters)
if err != nil {
gocuiReturnError(nc.Gui, err)
}
go func() {
idx, ok := <-selIdxChan
nc.Gui.Update(cleanUp)
if ok {
nc.Filter = nyaa_scraper.Filters[idx]
nc.Filter = ns.Filters[idx]
nc.Reload()
}
}()
Expand Down
29 changes: 25 additions & 4 deletions nyaa_scraper/nyaa.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ func GetNyaaCategory(major, minor int) NyaaCategory {

type NyaaFilter struct {
Name string
Val uint8
Val uint8
}

var (
NoFilter = NyaaFilter{"No filter", 0}
NoRemakes = NyaaFilter{"No remakes", 1}
NoFilter = NyaaFilter{"No filter", 0}
NoRemakes = NyaaFilter{"No remakes", 1}
TrustedOnly = NyaaFilter{"Trusted only", 2}
)

Expand All @@ -121,8 +121,28 @@ var Filters = []NyaaFilter{
TrustedOnly,
}

type NyaaClass uint8

const (
Default NyaaClass = iota
Trusted
Danger
)

func ParseNyaaClass(str string) NyaaClass {
switch strings.ToLower(str) {
case "success":
return Trusted
case "danger":
return Danger
default:
return Default
}
}

type NyaaEntry struct {
Category NyaaCategory
Class NyaaClass
Title string
TorrentLink string
MagnetLink string
Expand Down Expand Up @@ -188,10 +208,11 @@ func SearchSpecificPage(query string, category NyaaCategory, filter NyaaFilter,
return resultPage, nil
}

//TODO parsing green/neutral/red highlighted rows
func parseNyaaEntry(sel *goquery.Selection) *NyaaEntry {
entry := NyaaEntry{}

entry.Class = ParseNyaaClass(sel.AttrOr("class", "default"))

currChild := sel.Children().First()
category := currChild.Find("a").AttrOr("href", "")
major, _ := strconv.Atoi(category[4:5])
Expand Down

0 comments on commit 067d761

Please sign in to comment.