Skip to content

Commit 28a87cb

Browse files
committed
Support all scoring systems
1 parent 48c19f1 commit 28a87cb

File tree

11 files changed

+250
-53
lines changed

11 files changed

+250
-53
lines changed

anilist/anilist.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ const ALDomain = "https://anilist.co"
1818
var InvalidToken = errors.New("Invalid token")
1919

2020
// TODO downloading only given list like watching/completed
21-
func QueryUserLists(userId int, token oauth2.OAuthToken) ([]MediaListGroup, error) {
21+
func QueryUserLists(userId int, scoreFormat ScoreFormat, token oauth2.OAuthToken) ([]MediaListGroup, error) {
2222
vars := make(map[string]interface{})
2323
vars["userID"] = userId
24+
vars["scoreFormat"] = scoreFormat
2425

2526
resp := struct {
2627
MediaListCollection `json:"MediaListCollection"`

anilist/animation_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"github.com/aqatl/cliwait"
66
)
77

8-
func QueryUserListsWaitAnimation(userId int, token oauth2.OAuthToken) ([]MediaListGroup, error) {
8+
func QueryUserListsWaitAnimation(userId int, scoreFormat ScoreFormat, token oauth2.OAuthToken) ([]MediaListGroup, error) {
99
var mlg []MediaListGroup
1010
var err error
1111
cliwait.DoFuncWithWaitAnimation("Queyring user list", func() {
12-
mlg, err = QueryUserLists(userId, token)
12+
mlg, err = QueryUserLists(userId, scoreFormat, token)
1313
})
1414
return mlg, err
1515
}

anilist/gql_queries.go

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

33
var queryUserAnimeList = `
4-
query UserList ($userID: Int) {
4+
query UserList ($userID: Int, $scoreFormat: ScoreFormat) {
55
MediaListCollection (userId: $userID, type: ANIME) {
66
lists {
77
entries {
88
id
99
status
10-
score(format: POINT_10)
10+
score(format: $scoreFormat)
1111
progress
1212
repeat
1313
updatedAt
@@ -81,6 +81,9 @@ query {
8181
donatorTier
8282
moderatorStatus
8383
updatedAt
84+
mediaListOptions {
85+
scoreFormat
86+
}
8487
}
8588
}
8689
`

anilist/gql_types.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,37 @@ import (
55
)
66

77
type User struct {
8-
Id int `json:"id"`
9-
Name string `json:"name"`
10-
About string `json:"about"`
11-
BannerImage string `json:"bannerImage"`
12-
Stats UserStats `json:"stats"`
13-
UnreadNotificationCount int `json:"unreadNotificationCount"`
14-
SiteUrl string `json:"siteUrl"`
15-
DonatorTier int `json:"donatorTier"`
16-
ModeratorStatus string `json:"moderatorStatus"`
17-
UpdatedAt int `json:"updatedAt"`
8+
Id int `json:"id"`
9+
Name string `json:"name"`
10+
About string `json:"about"`
11+
BannerImage string `json:"bannerImage"`
12+
Stats UserStats `json:"stats"`
13+
UnreadNotificationCount int `json:"unreadNotificationCount"`
14+
SiteUrl string `json:"siteUrl"`
15+
DonatorTier int `json:"donatorTier"`
16+
ModeratorStatus string `json:"moderatorStatus"`
17+
UpdatedAt int `json:"updatedAt"`
18+
MediaListOptions MediaListOptions `json:"mediaListOptions"`
1819
}
1920

2021
type UserStats struct {
2122
WatchedTime int `json:"watchedTime"`
2223
}
2324

25+
type MediaListOptions struct {
26+
ScoreFormat ScoreFormat `json:"scoreFormat"`
27+
}
28+
29+
type ScoreFormat string
30+
31+
const (
32+
Point100 ScoreFormat = "POINT_100"
33+
Point10Decimal ScoreFormat = "POINT_10_DECIMAL"
34+
Point10 ScoreFormat = "POINT_10"
35+
Point5 ScoreFormat = "POINT_5"
36+
Point3 ScoreFormat = "POINT_3"
37+
)
38+
2439
type MediaListCollection struct {
2540
Lists []MediaListGroup `json:"lists"`
2641
}
@@ -36,7 +51,7 @@ type MediaListGroup struct {
3651
type MediaListEntry struct {
3752
ListId int `json:"id"`
3853
Status MediaListStatus `json:"status"`
39-
Score int `json:"score"`
54+
Score float32 `json:"score"`
4055
Progress int `json:"progress"`
4156
Repeat int `json:"repeat"`
4257
UpdatedAt int `json:"updatedAt"`

anilist_app.go

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,12 @@ func aniListDefaultAction(ctx *cli.Context) error {
330330
fmt.Printf("%*s%*.*s%8s%6s\n",
331331
numberFieldWidth, "No", titleWidth, titleWidth, "Title", "Eps", "Score")
332332
fmt.Println(strings.Repeat("=", cfg.ListWidth))
333-
pattern := "%*d%*.*s%8s%6d\n"
333+
var pattern string
334+
if al.User.MediaListOptions.ScoreFormat == anilist.Point10Decimal {
335+
pattern = "%*d%*.*s%8s%6.1f\n"
336+
} else {
337+
pattern = "%*d%*.*s%8s%6.f\n"
338+
}
334339
var entry *anilist.MediaListEntry
335340
for i := visibleEntries - 1; i >= 0; i-- {
336341
entry = &list[i]
@@ -393,7 +398,7 @@ func alSetEntryEpisodes(ctx *cli.Context) error {
393398
}
394399

395400
fmt.Println("Updated successfully")
396-
alPrintEntryDetailsAfterUpdatedEpisodes(entry, epsBefore)
401+
alPrintEntryDetailsAfterUpdatedEpisodes(entry, epsBefore, al.User.MediaListOptions.ScoreFormat)
397402
return nil
398403
}
399404

@@ -437,7 +442,7 @@ func alSetEntryStatus(ctx *cli.Context) error {
437442
}
438443

439444
fmt.Println("Updated successfully")
440-
alPrintEntryDetails(entry)
445+
alPrintEntryDetails(entry, al.User.MediaListOptions.ScoreFormat)
441446
return nil
442447
}
443448

@@ -451,9 +456,9 @@ func alSetEntryScore(ctx *cli.Context) error {
451456
return err
452457
}
453458

454-
score, err := strconv.Atoi(ctx.Args().First())
455-
if err != nil || score < 0 || score > 10 {
456-
return fmt.Errorf("invalid score; valid range: <0;10>")
459+
score, err := parseScore(ctx.Args().First(), al.User.MediaListOptions.ScoreFormat)
460+
if err != nil {
461+
return err
457462
}
458463

459464
entry.Score = score
@@ -466,10 +471,49 @@ func alSetEntryScore(ctx *cli.Context) error {
466471
}
467472

468473
fmt.Println("Updated successfully")
469-
alPrintEntryDetails(entry)
474+
alPrintEntryDetails(entry, al.User.MediaListOptions.ScoreFormat)
470475
return nil
471476
}
472477

478+
func parseScore(score string, scoreFormat anilist.ScoreFormat) (float32, error) {
479+
maxScore := 0
480+
switch scoreFormat {
481+
case anilist.Point10Decimal:
482+
maxScore = 10
483+
decimalPartIdx := strings.Index(score, ".")
484+
if decimalPartIdx == -1 {
485+
break
486+
} else if decimalPartIdx == len(score)-1 {
487+
score = score[:decimalPartIdx]
488+
break
489+
}
490+
491+
decimalPart := score[(decimalPartIdx + 1):]
492+
if len(decimalPart) > 1 {
493+
return 0, fmt.Errorf("invalid score; up to 1 decimal place allowed")
494+
}
495+
496+
parsedScore, err := strconv.ParseFloat(score, 32)
497+
if err != nil || parsedScore < 0.0 || parsedScore > 10.0 {
498+
return 0, fmt.Errorf("invalid score; valid range: <0;10>")
499+
}
500+
return float32(parsedScore), err
501+
case anilist.Point100:
502+
maxScore = 100
503+
case anilist.Point10:
504+
maxScore = 10
505+
case anilist.Point5:
506+
maxScore = 5
507+
case anilist.Point3:
508+
maxScore = 3
509+
}
510+
parsedScore, err := strconv.Atoi(score)
511+
if err != nil || parsedScore < 0 || parsedScore > maxScore {
512+
return 0, fmt.Errorf("invalid score; valid range: <0;%d>", maxScore)
513+
}
514+
return float32(parsedScore), err
515+
}
516+
473517
func alDeleteEntry(ctx *cli.Context) error {
474518
al, entry, _, err := loadAniListFull(ctx)
475519
if err != nil {
@@ -481,7 +525,7 @@ func alDeleteEntry(ctx *cli.Context) error {
481525
}
482526

483527
fmt.Println("Entry deleted successfully")
484-
alPrintEntryDetails(entry)
528+
alPrintEntryDetails(entry, al.User.MediaListOptions.ScoreFormat)
485529

486530
al.List = al.List.DeleteById(entry.ListId)
487531
return saveAniListAnimeLists(al)
@@ -519,7 +563,7 @@ func alSelectEntry(ctx *cli.Context) error {
519563
}
520564
}
521565
if matchedEntry != nil {
522-
alSaveSelection(cfg, matchedEntry)
566+
alSaveSelection(cfg, matchedEntry, al.User.MediaListOptions.ScoreFormat)
523567
return nil
524568
}
525569

@@ -534,25 +578,25 @@ func alSelectRandomEntry(ctx *cli.Context) error {
534578

535579
planToWatchList := alGetList(al, anilist.Planning)
536580
idx := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(planToWatchList))
537-
alSaveSelection(LoadConfig(), &planToWatchList[idx])
581+
alSaveSelection(LoadConfig(), &planToWatchList[idx], al.User.MediaListOptions.ScoreFormat)
538582

539583
return nil
540584
}
541585

542-
func alSaveSelection(cfg *Config, entry *anilist.MediaListEntry) {
586+
func alSaveSelection(cfg *Config, entry *anilist.MediaListEntry, scoreFormat anilist.ScoreFormat) {
543587
cfg.ALSelectedID = entry.Id
544588
cfg.Save()
545589

546590
fmt.Println("Selected entry:")
547-
alPrintEntryDetails(entry)
591+
alPrintEntryDetails(entry, scoreFormat)
548592
}
549593

550594
func alShowSelectedEntry(ctx *cli.Context) error {
551-
_, entry, _, err := loadAniListFull(ctx)
595+
al, entry, _, err := loadAniListFull(ctx)
552596
if err != nil {
553597
return err
554598
}
555-
alPrintEntryDetails(entry)
599+
alPrintEntryDetails(entry, al.User.MediaListOptions.ScoreFormat)
556600
return nil
557601
}
558602

@@ -586,7 +630,7 @@ func alNyaaWebsite(ctx *cli.Context) error {
586630
}
587631

588632
fmt.Println("Searched for:")
589-
alPrintEntryDetails(entry)
633+
alPrintEntryDetails(entry, al.User.MediaListOptions.ScoreFormat)
590634
return nil
591635
}
592636

@@ -631,7 +675,7 @@ func alOpenWebsite(ctx *cli.Context) error {
631675
}
632676

633677
fmt.Println("Opened website for:")
634-
alPrintEntryDetails(entry)
678+
alPrintEntryDetails(entry, al.User.MediaListOptions.ScoreFormat)
635679
fmt.Fprintf(color.Output, "URL: %v\n", color.CyanString("%v", entryUrl))
636680
} else {
637681
fmt.Println("Nothing to open")
@@ -848,7 +892,7 @@ func alCopyIntoClipboard(ctx *cli.Context) error {
848892
}
849893

850894
func alOpenEntrySite(ctx *cli.Context) error {
851-
_, entry, cfg, err := loadAniListFull(ctx)
895+
al, entry, cfg, err := loadAniListFull(ctx)
852896
if err != nil {
853897
return err
854898
}
@@ -860,20 +904,20 @@ func alOpenEntrySite(ctx *cli.Context) error {
860904
open.StartWith(uri, path)
861905
}
862906
fmt.Println("Opened website for:")
863-
alPrintEntryDetails(entry)
907+
alPrintEntryDetails(entry, al.User.MediaListOptions.ScoreFormat)
864908

865909
return nil
866910
}
867911

868912
func alOpenMalSite(ctx *cli.Context) error {
869-
_, entry, cfg, err := loadAniListFull(ctx)
913+
al, entry, cfg, err := loadAniListFull(ctx)
870914
if err != nil {
871915
return err
872916
}
873917

874918
openMalSite(cfg, entry.IdMal)
875919
fmt.Println("Opened website for:")
876-
alPrintEntryDetails(entry)
920+
alPrintEntryDetails(entry, al.User.MediaListOptions.ScoreFormat)
877921

878922
return nil
879923
}

0 commit comments

Comments
 (0)