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 (
7 "net/http" 7 "net/http"
8 "os" 8 "os"
9 "path/filepath" 9 "path/filepath"
10 "net/url"
10 // "strings" 11 // "strings"
11) 12)
12 13
@@ -39,9 +40,9 @@ type response struct {
39// fmt.Println(animeList) 40// fmt.Println(animeList)
40// } 41// }
41 42
42// searchAnime performs the API call and fetches anime information
43func SearchAnime(query, mode string) (map[string]string, error) { 43func SearchAnime(query, mode string) (map[string]string, error) {
44 userCurdConfig := GetGlobalConfig() 44 userCurdConfig := GetGlobalConfig()
45 var logFile string
45 if userCurdConfig == nil { 46 if userCurdConfig == nil {
46 logFile = os.ExpandEnv("$HOME/.local/share/curd/debug.log") 47 logFile = os.ExpandEnv("$HOME/.local/share/curd/debug.log")
47 } else { 48 } else {
@@ -54,18 +55,47 @@ func SearchAnime(query, mode string) (map[string]string, error) {
54 allanimeAPI = "https://api." + allanimeBase + "/api" 55 allanimeAPI = "https://api." + allanimeBase + "/api"
55 ) 56 )
56 57
57 // Format and return the anime list 58 // Prepare the anime list
58 animeList := make(map[string]string) 59 animeList := make(map[string]string)
59 60
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 }
61 91
62 // Build the request URL 92 // 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))
64 94
65 // Make the HTTP request 95 // Make the HTTP request
66 req, err := http.NewRequest("GET", url, nil) 96 req, err := http.NewRequest("GET", url, nil)
67 if err != nil { 97 if err != nil {
68 Log(fmt.Sprint("Error creating HTTP request:", err), logFile) 98 Log(fmt.Sprintf("Error creating HTTP request: %v", err), logFile)
69 return animeList, err 99 return animeList, err
70 } 100 }
71 req.Header.Set("User-Agent", agent) 101 req.Header.Set("User-Agent", agent)
@@ -74,7 +104,7 @@ func SearchAnime(query, mode string) (map[string]string, error) {
74 client := &http.Client{} 104 client := &http.Client{}
75 resp, err := client.Do(req) 105 resp, err := client.Do(req)
76 if err != nil { 106 if err != nil {
77 Log(fmt.Sprint("Error making HTTP request:", err), logFile) 107 Log(fmt.Sprintf("Error making HTTP request: %v", err), logFile)
78 return animeList, err 108 return animeList, err
79 } 109 }
80 defer resp.Body.Close() 110 defer resp.Body.Close()
@@ -82,11 +112,11 @@ func SearchAnime(query, mode string) (map[string]string, error) {
82 // Read the response body 112 // Read the response body
83 body, err := io.ReadAll(resp.Body) 113 body, err := io.ReadAll(resp.Body)
84 if err != nil { 114 if err != nil {
85 Log(fmt.Sprint("Error reading response body:", err), logFile) 115 Log(fmt.Sprintf("Error reading response body: %v", err), logFile)
86 return animeList, err 116 return animeList, err
87 } 117 }
88 118
89 // Debug: Log the response status and first part of body 119 // Debug: Log the response status and first part of the body
90 Log(fmt.Sprintf("Response Status: %s", resp.Status), logFile) 120 Log(fmt.Sprintf("Response Status: %s", resp.Status), logFile)
91 Log(fmt.Sprintf("Response Body (first 500 chars): %s", string(body[:min(len(body), 500)])), logFile) 121 Log(fmt.Sprintf("Response Body (first 500 chars): %s", string(body[:min(len(body), 500)])), logFile)
92 122
@@ -99,17 +129,8 @@ func SearchAnime(query, mode string) (map[string]string, error) {
99 } 129 }
100 130
101 for _, anime := range response.Data.Shows.Edges { 131 for _, anime := range response.Data.Shows.Edges {
102 // availableEpisodes, _ := anime.AvailableEpisodes.Int64() // Converts json.Number to int64
103
104 var episodesStr string 132 var episodesStr string
105
106 // Log(anime.AvailableEpisodes, logFile)
107 // episodesStr = anime.AvailableEpisodes.sub
108
109 //anime = {"dub":0,"raw":0,"sub":4}
110
111 if episodes, ok := anime.AvailableEpisodes.(map[string]interface{}); ok { 133 if episodes, ok := anime.AvailableEpisodes.(map[string]interface{}); ok {
112 // Log(episodes, logFile)
113 if subEpisodes, ok := episodes["sub"].(float64); ok { 134 if subEpisodes, ok := episodes["sub"].(float64); ok {
114 episodesStr = fmt.Sprintf("%d", int(subEpisodes)) 135 episodesStr = fmt.Sprintf("%d", int(subEpisodes))
115 } else { 136 } else {
@@ -117,9 +138,7 @@ func SearchAnime(query, mode string) (map[string]string, error) {
117 episodesStr = "Unknown" 138 episodesStr = "Unknown"
118 } 139 }
119 } 140 }
120
121 animeList[anime.ID] = fmt.Sprintf("%s (%s episodes)", anime.Name, episodesStr) 141 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))
123 } 142 }
124 return animeList, nil 143 return animeList, nil
125} 144}