Commit 1698b06bc8

1698b06bc85b567b7cb696bec04bd68b65ad4924

parent: 2e6e6ae0f3

Unsigned

Wraient <wraient.911@gmail.com> · 2024-11-06 06:07 UTC

Fix: If the anime has semi-colon in the name it fucks up the syntax, fixed.

Layout: unified · split

VERSION.txt +1 −1
@@ -1 +1 @@
10.0.8
\ No newline at end of file
10.0.10
\ No newline at end of file
internal/anime_list.go +38 −19
@@ -7,6 +7,7 @@ import (
77 "net/http"
88 "os"
99 "path/filepath"
10 "net/url"
1011 // "strings"
1112)
1213
@@ -39,9 +40,9 @@ type response struct {
3940// fmt.Println(animeList)
4041// }
4142
42// searchAnime performs the API call and fetches anime information
4343func SearchAnime(query, mode string) (map[string]string, error) {
4444 userCurdConfig := GetGlobalConfig()
45 var logFile string
4546 if userCurdConfig == nil {
4647 logFile = os.ExpandEnv("$HOME/.local/share/curd/debug.log")
4748 } else {
@@ -54,18 +55,47 @@ func SearchAnime(query, mode string) (map[string]string, error) {
5455 allanimeAPI = "https://api." + allanimeBase + "/api"
5556 )
5657
57 // Format and return the anime list
58 // Prepare the anime list
5859 animeList := make(map[string]string)
5960
60 searchGql := `query( $search: SearchInput $limit: Int $page: Int $translationType: VaildTranslationTypeEnumType $countryOrigin: VaildCountryOriginEnumType ) { shows( search: $search limit: $limit page: $page translationType: $translationType countryOrigin: $countryOrigin ) { edges { _id name availableEpisodes __typename } } }`
61 searchGql := `query($search: SearchInput, $limit: Int, $page: Int, $translationType: VaildTranslationTypeEnumType, $countryOrigin: VaildCountryOriginEnumType) {
62 shows(search: $search, limit: $limit, page: $page, translationType: $translationType, countryOrigin: $countryOrigin) {
63 edges {
64 _id
65 name
66 availableEpisodes
67 __typename
68 }
69 }
70 }`
71
72 // Prepare the GraphQL variables
73 variables := map[string]interface{}{
74 "search": map[string]interface{}{
75 "allowAdult": false,
76 "allowUnknown": false,
77 "query": query,
78 },
79 "limit": 40,
80 "page": 1,
81 "translationType": mode,
82 "countryOrigin": "ALL",
83 }
84
85 // Marshal the variables to JSON
86 variablesJSON, err := json.Marshal(variables)
87 if err != nil {
88 Log(fmt.Sprintf("Error encoding variables to JSON: %v", err), logFile)
89 return animeList, err
90 }
6191
6292 // Build the request URL
63 url := fmt.Sprintf("%s?variables={\"search\":{\"allowAdult\":false,\"allowUnknown\":false,\"query\":\"%s\"},\"limit\":40,\"page\":1,\"translationType\":\"%s\",\"countryOrigin\":\"ALL\"}&query=%s", allanimeAPI, query, mode, searchGql)
93 url := fmt.Sprintf("%s?variables=%s&query=%s", allanimeAPI, url.QueryEscape(string(variablesJSON)), url.QueryEscape(searchGql))
6494
6595 // Make the HTTP request
6696 req, err := http.NewRequest("GET", url, nil)
6797 if err != nil {
68 Log(fmt.Sprint("Error creating HTTP request:", err), logFile)
98 Log(fmt.Sprintf("Error creating HTTP request: %v", err), logFile)
6999 return animeList, err
70100 }
71101 req.Header.Set("User-Agent", agent)
@@ -74,7 +104,7 @@ func SearchAnime(query, mode string) (map[string]string, error) {
74104 client := &http.Client{}
75105 resp, err := client.Do(req)
76106 if err != nil {
77 Log(fmt.Sprint("Error making HTTP request:", err), logFile)
107 Log(fmt.Sprintf("Error making HTTP request: %v", err), logFile)
78108 return animeList, err
79109 }
80110 defer resp.Body.Close()
@@ -82,11 +112,11 @@ func SearchAnime(query, mode string) (map[string]string, error) {
82112 // Read the response body
83113 body, err := io.ReadAll(resp.Body)
84114 if err != nil {
85 Log(fmt.Sprint("Error reading response body:", err), logFile)
115 Log(fmt.Sprintf("Error reading response body: %v", err), logFile)
86116 return animeList, err
87117 }
88118
89 // Debug: Log the response status and first part of body
119 // Debug: Log the response status and first part of the body
90120 Log(fmt.Sprintf("Response Status: %s", resp.Status), logFile)
91121 Log(fmt.Sprintf("Response Body (first 500 chars): %s", string(body[:min(len(body), 500)])), logFile)
92122
@@ -99,17 +129,8 @@ func SearchAnime(query, mode string) (map[string]string, error) {
99129 }
100130
101131 for _, anime := range response.Data.Shows.Edges {
102 // availableEpisodes, _ := anime.AvailableEpisodes.Int64() // Converts json.Number to int64
103
104132 var episodesStr string
105
106 // Log(anime.AvailableEpisodes, logFile)
107 // episodesStr = anime.AvailableEpisodes.sub
108
109 //anime = {"dub":0,"raw":0,"sub":4}
110
111133 if episodes, ok := anime.AvailableEpisodes.(map[string]interface{}); ok {
112 // Log(episodes, logFile)
113134 if subEpisodes, ok := episodes["sub"].(float64); ok {
114135 episodesStr = fmt.Sprintf("%d", int(subEpisodes))
115136 } else {
@@ -117,9 +138,7 @@ func SearchAnime(query, mode string) (map[string]string, error) {
117138 episodesStr = "Unknown"
118139 }
119140 }
120
121141 animeList[anime.ID] = fmt.Sprintf("%s (%s episodes)", anime.Name, episodesStr)
122 // animeList.WriteString(fmt.Sprintf("%s\t%s (%s episodes)\n", anime.ID, anime.Name, episodesStr))
123142 }
124143 return animeList, nil
125144}