Skip to content

Commit

Permalink
Fix: If the anime has semi-colon in the name it fucks up the syntax, …
Browse files Browse the repository at this point in the history
…fixed.
  • Loading branch information
Wraient committed Nov 6, 2024
1 parent 2e6e6ae commit 1698b06
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.8
0.0.10
57 changes: 38 additions & 19 deletions internal/anime_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
"net/url"
// "strings"
)

Expand Down Expand Up @@ -39,9 +40,9 @@ type response struct {
// fmt.Println(animeList)
// }

// searchAnime performs the API call and fetches anime information
func SearchAnime(query, mode string) (map[string]string, error) {
userCurdConfig := GetGlobalConfig()
var logFile string
if userCurdConfig == nil {
logFile = os.ExpandEnv("$HOME/.local/share/curd/debug.log")
} else {
Expand All @@ -54,18 +55,47 @@ func SearchAnime(query, mode string) (map[string]string, error) {
allanimeAPI = "https://api." + allanimeBase + "/api"
)

// Format and return the anime list
// Prepare the anime list
animeList := make(map[string]string)

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 } } }`
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
}
}
}`

// Prepare the GraphQL variables
variables := map[string]interface{}{
"search": map[string]interface{}{
"allowAdult": false,
"allowUnknown": false,
"query": query,
},
"limit": 40,
"page": 1,
"translationType": mode,
"countryOrigin": "ALL",
}

// Marshal the variables to JSON
variablesJSON, err := json.Marshal(variables)
if err != nil {
Log(fmt.Sprintf("Error encoding variables to JSON: %v", err), logFile)
return animeList, err
}

// Build the request URL
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)
url := fmt.Sprintf("%s?variables=%s&query=%s", allanimeAPI, url.QueryEscape(string(variablesJSON)), url.QueryEscape(searchGql))

// Make the HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
Log(fmt.Sprint("Error creating HTTP request:", err), logFile)
Log(fmt.Sprintf("Error creating HTTP request: %v", err), logFile)
return animeList, err
}
req.Header.Set("User-Agent", agent)
Expand All @@ -74,19 +104,19 @@ func SearchAnime(query, mode string) (map[string]string, error) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Log(fmt.Sprint("Error making HTTP request:", err), logFile)
Log(fmt.Sprintf("Error making HTTP request: %v", err), logFile)
return animeList, err
}
defer resp.Body.Close()

// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
Log(fmt.Sprint("Error reading response body:", err), logFile)
Log(fmt.Sprintf("Error reading response body: %v", err), logFile)
return animeList, err
}

// Debug: Log the response status and first part of body
// Debug: Log the response status and first part of the body
Log(fmt.Sprintf("Response Status: %s", resp.Status), logFile)
Log(fmt.Sprintf("Response Body (first 500 chars): %s", string(body[:min(len(body), 500)])), logFile)

Expand All @@ -99,27 +129,16 @@ func SearchAnime(query, mode string) (map[string]string, error) {
}

for _, anime := range response.Data.Shows.Edges {
// availableEpisodes, _ := anime.AvailableEpisodes.Int64() // Converts json.Number to int64

var episodesStr string

// Log(anime.AvailableEpisodes, logFile)
// episodesStr = anime.AvailableEpisodes.sub

//anime = {"dub":0,"raw":0,"sub":4}

if episodes, ok := anime.AvailableEpisodes.(map[string]interface{}); ok {
// Log(episodes, logFile)
if subEpisodes, ok := episodes["sub"].(float64); ok {
episodesStr = fmt.Sprintf("%d", int(subEpisodes))
} else {
Log(subEpisodes, logFile)
episodesStr = "Unknown"
}
}

animeList[anime.ID] = fmt.Sprintf("%s (%s episodes)", anime.Name, episodesStr)
// animeList.WriteString(fmt.Sprintf("%s\t%s (%s episodes)\n", anime.ID, anime.Name, episodesStr))
}
return animeList, nil
}
Expand Down

0 comments on commit 1698b06

Please sign in to comment.