Commit c324653056

c3246530568f8d0509d2dc4506567905faeb779d

parent: 49488fa4ff

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-27 11:38 UTC

Rate anime with rofi

Layout: unified · split

cmd/curd/main.go +1 −4
@@ -400,11 +400,8 @@ func main() {
400 // internal.CurdOut(anime.Ep.Number, anime.TotalEpisodes, &userCurdConfig) 400 // internal.CurdOut(anime.Ep.Number, anime.TotalEpisodes, &userCurdConfig)
401 if anime.Ep.Number-1 == anime.TotalEpisodes && userCurdConfig.ScoreOnCompletion { 401 if anime.Ep.Number-1 == anime.TotalEpisodes && userCurdConfig.ScoreOnCompletion {
402 anime.Ep.Number = anime.Ep.Number - 1 402 anime.Ep.Number = anime.Ep.Number - 1
403 var userScore float64
404 internal.CurdOut("Completed anime.") 403 internal.CurdOut("Completed anime.")
405 fmt.Println("Rate this anime: ") 404 internal.RateAnime(user.Token, anime.AnilistId)
406 fmt.Scanln(&userScore)
407 internal.RateAnime(user.Token, anime.AnilistId, userScore)
408 internal.LocalDeleteAnime(databaseFile, anime.AnilistId, anime.AllanimeId) 405 internal.LocalDeleteAnime(databaseFile, anime.AnilistId, anime.AllanimeId)
409 internal.ExitCurd(nil) 406 internal.ExitCurd(nil)
410 } 407 }
internal/anilist.go +19 −2
@@ -349,7 +349,24 @@ func UpdateAnimeProgress(token string, mediaID, progress int) error {
349} 349}
350 350
351// Function to rate an anime on AniList 351// Function to rate an anime on AniList
352func RateAnime(token string, mediaID int, score float64) error { 352func RateAnime(token string, mediaID int) error {
353 var score float64
354 var err error
355
356 if GetGlobalConfig().RofiSelection {
357 userInput, err := GetUserInputFromRofi("Enter a score for the anime (0-10):")
358 if err != nil {
359 return err
360 }
361 score, err = strconv.ParseFloat(userInput, 64)
362 if err != nil {
363 return err
364 }
365 } else {
366 fmt.Println("Rate this anime: ")
367 fmt.Scanln(&score)
368 }
369
353 url := "https://graphql.anilist.co" 370 url := "https://graphql.anilist.co"
354 query := ` 371 query := `
355 mutation($mediaId: Int, $score: Float) { 372 mutation($mediaId: Int, $score: Float) {
@@ -370,7 +387,7 @@ func RateAnime(token string, mediaID int, score float64) error {
370 "Content-Type": "application/json", 387 "Content-Type": "application/json",
371 } 388 }
372 389
373 _, err := makePostRequest(url, query, variables, headers) 390 _, err = makePostRequest(url, query, variables, headers)
374 if err != nil { 391 if err != nil {
375 return err 392 return err
376 } 393 }
internal/rofi.go +24
@@ -1,7 +1,10 @@
1package internal 1package internal
2 2
3import ( 3import (
4 "bytes"
5 "fmt"
4 "os/exec" 6 "os/exec"
7 "strings"
5) 8)
6 9
7func GetTokenFromRofi() (string, error) { 10func GetTokenFromRofi() (string, error) {
@@ -29,3 +32,24 @@ func GetTokenFromRofi() (string, error) {
29 32
30 return token, nil 33 return token, nil
31} 34}
35
36// GetUserInputFromRofi prompts the user for input using Rofi with a custom message
37func GetUserInputFromRofi(message string) (string, error) {
38 // Create the Rofi command
39 cmd := exec.Command("rofi", "-dmenu", "-p", "Input", "-mesg", message)
40
41 // Set up pipes for output
42 var out bytes.Buffer
43 cmd.Stdout = &out
44
45 // Run the command
46 err := cmd.Run()
47 if err != nil {
48 return "", fmt.Errorf("failed to run Rofi: %w", err)
49 }
50
51 // Get the entered input
52 userInput := strings.TrimSpace(out.String())
53
54 return userInput, nil
55}\ No newline at end of file