Commit e8f271e504
Unsigned
Layout: unified · split
cmd/curd/main.go +90 −2
| @@ -1,7 +1,95 @@ | |||
| 1 | package main | 1 | package main |
| 2 | 2 | ||
| 3 | import "fmt" | 3 | import ( |
| 4 | "fmt" | ||
| 5 | "github.com/wraient/curd/internal" | ||
| 6 | "os" | ||
| 7 | ) | ||
| 8 | |||
| 9 | type Title struct { | ||
| 10 | Romanji_title string | ||
| 11 | English_title string | ||
| 12 | Japanese_title string | ||
| 13 | } | ||
| 14 | |||
| 15 | type Anime struct { | ||
| 16 | Tit Title | ||
| 17 | Ep Episode | ||
| 18 | Total_episodes int | ||
| 19 | Mal_id int | ||
| 20 | Allanime_id string | ||
| 21 | } | ||
| 22 | |||
| 23 | type SkipTimes struct { | ||
| 24 | Op int | ||
| 25 | Ed int | ||
| 26 | } | ||
| 27 | |||
| 28 | type Episode struct { | ||
| 29 | Tit Title | ||
| 30 | Number int | ||
| 31 | Skip_times SkipTimes | ||
| 32 | links []string | ||
| 33 | Is_filler bool | ||
| 34 | Is_recap bool | ||
| 35 | Aired string | ||
| 36 | Synopsis string | ||
| 37 | } | ||
| 4 | 38 | ||
| 5 | func main(){ | 39 | func main(){ |
| 6 | fmt.Println("Great future.") | 40 | internal.ClearScreen() |
| 41 | defer internal.RestoreScreen() | ||
| 42 | |||
| 43 | var userQuery string | ||
| 44 | logFile := "debug.log" | ||
| 45 | |||
| 46 | fmt.Println("Enter anime: ") | ||
| 47 | fmt.Scanln(&userQuery) // Scan input until a newline | ||
| 48 | // fmt.Println("You entered:", input) | ||
| 49 | animeList, err := internal.SearchAnime(string(userQuery), "sub") | ||
| 50 | |||
| 51 | if err != nil { | ||
| 52 | fmt.Println("Failed to get anime list") | ||
| 53 | os.Exit(1) | ||
| 54 | } | ||
| 55 | |||
| 56 | if len(animeList) == 0 { | ||
| 57 | fmt.Println("No results found.") | ||
| 58 | os.Exit(0) | ||
| 59 | } | ||
| 60 | if len(animeList) == 1 { | ||
| 61 | internal.Log("one result", logFile) | ||
| 62 | internal.Log(animeList, logFile) | ||
| 63 | } | ||
| 64 | |||
| 65 | selectedOption, err := internal.DynamicSelect(animeList) | ||
| 66 | |||
| 67 | // Output the selected anime and its internal key | ||
| 68 | if err != nil { | ||
| 69 | fmt.Println("No anime Selected") | ||
| 70 | os.Exit(0) | ||
| 71 | } | ||
| 72 | |||
| 73 | internal.Log(animeList, logFile) | ||
| 74 | internal.Log(selectedOption, logFile) | ||
| 75 | |||
| 76 | episodeList, err := internal.EpisodesList(selectedOption.Key, "sub") | ||
| 77 | |||
| 78 | if err != nil{ | ||
| 79 | fmt.Println("No episode list found") | ||
| 80 | } | ||
| 81 | |||
| 82 | internal.Log(episodeList, logFile) | ||
| 83 | var episodeNumber string | ||
| 84 | fmt.Printf("Enter the episode (%v episodes)", episodeList[len(episodeList)-1]) | ||
| 85 | fmt.Scanln(&episodeNumber) | ||
| 86 | |||
| 87 | link, err := internal.GetEpisodeURL(internal.GetDefaultAllanimeConfig(), selectedOption.Key, episodeNumber) | ||
| 88 | |||
| 89 | if err != nil { | ||
| 90 | fmt.Println("Failed to get episode link") | ||
| 91 | } | ||
| 92 | |||
| 93 | internal.Log(link, logFile) | ||
| 94 | |||
| 7 | }\ No newline at end of file | 95 | }\ No newline at end of file |
go.mod added +24
| @@ -0,0 +1,24 @@ | |||
| 1 | module github.com/wraient/curd | ||
| 2 | |||
| 3 | go 1.23.2 | ||
| 4 | |||
| 5 | require github.com/charmbracelet/bubbletea v1.1.1 | ||
| 6 | |||
| 7 | require ( | ||
| 8 | github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect | ||
| 9 | github.com/charmbracelet/lipgloss v0.13.0 // indirect | ||
| 10 | github.com/charmbracelet/x/ansi v0.2.3 // indirect | ||
| 11 | github.com/charmbracelet/x/term v0.2.0 // indirect | ||
| 12 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect | ||
| 13 | github.com/lucasb-eyer/go-colorful v1.2.0 // indirect | ||
| 14 | github.com/mattn/go-isatty v0.0.20 // indirect | ||
| 15 | github.com/mattn/go-localereader v0.0.1 // indirect | ||
| 16 | github.com/mattn/go-runewidth v0.0.15 // indirect | ||
| 17 | github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect | ||
| 18 | github.com/muesli/cancelreader v0.2.2 // indirect | ||
| 19 | github.com/muesli/termenv v0.15.2 // indirect | ||
| 20 | github.com/rivo/uniseg v0.4.7 // indirect | ||
| 21 | golang.org/x/sync v0.8.0 // indirect | ||
| 22 | golang.org/x/sys v0.24.0 // indirect | ||
| 23 | golang.org/x/text v0.14.0 // indirect | ||
| 24 | ) | ||
go.sum added +37
| @@ -0,0 +1,37 @@ | |||
| 1 | github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= | ||
| 2 | github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= | ||
| 3 | github.com/charmbracelet/bubbletea v1.1.1 h1:KJ2/DnmpfqFtDNVTvYZ6zpPFL9iRCRr0qqKOCvppbPY= | ||
| 4 | github.com/charmbracelet/bubbletea v1.1.1/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4= | ||
| 5 | github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw= | ||
| 6 | github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY= | ||
| 7 | github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY= | ||
| 8 | github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= | ||
| 9 | github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0= | ||
| 10 | github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0= | ||
| 11 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= | ||
| 12 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= | ||
| 13 | github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= | ||
| 14 | github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= | ||
| 15 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
| 16 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
| 17 | github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= | ||
| 18 | github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= | ||
| 19 | github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= | ||
| 20 | github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
| 21 | github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= | ||
| 22 | github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= | ||
| 23 | github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= | ||
| 24 | github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= | ||
| 25 | github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= | ||
| 26 | github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= | ||
| 27 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
| 28 | github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= | ||
| 29 | github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
| 30 | golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= | ||
| 31 | golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
| 32 | golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| 33 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| 34 | golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= | ||
| 35 | golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
| 36 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | ||
| 37 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | ||
internal/anime_list.go +26 −41
| @@ -1,42 +1,41 @@ | |||
| 1 | package main | 1 | package internal |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | "fmt" | 5 | "fmt" |
| 6 | "io/ioutil" | 6 | "io/ioutil" |
| 7 | "net/http" | 7 | "net/http" |
| 8 | "os" | 8 | // "strings" |
| 9 | "strings" | ||
| 10 | ) | 9 | ) |
| 11 | 10 | ||
| 12 | type Anime struct { | 11 | type anime struct { |
| 13 | ID string `json:"_id"` | 12 | ID string `json:"_id"` |
| 14 | Name string `json:"name"` | 13 | Name string `json:"name"` |
| 15 | AvailableEpisodes interface{} `json:"availableEpisodes"` | 14 | AvailableEpisodes interface{} `json:"availableEpisodes"` |
| 16 | } | 15 | } |
| 17 | 16 | ||
| 18 | type Response struct { | 17 | type response struct { |
| 19 | Data struct { | 18 | Data struct { |
| 20 | Shows struct { | 19 | Shows struct { |
| 21 | Edges []Anime `json:"edges"` | 20 | Edges []anime `json:"edges"` |
| 22 | } `json:"shows"` | 21 | } `json:"shows"` |
| 23 | } `json:"data"` | 22 | } `json:"data"` |
| 24 | } | 23 | } |
| 25 | 24 | ||
| 26 | func main() { | 25 | // func main() { |
| 27 | // Get environment variables | 26 | // // Get environment variables |
| 28 | mode := getEnv("ANI_CLI_MODE", "sub") | 27 | // mode := "sub" |
| 29 | 28 | ||
| 30 | // Query for the anime (from a file in this example) | 29 | // // Query for the anime (from a file in this example) |
| 31 | query := "One piece" | 30 | // query := "Death note" |
| 32 | 31 | ||
| 33 | // Search anime | 32 | // // Search anime |
| 34 | animeList := searchAnime(string(query), mode) | 33 | // animeList := searchAnime(string(query), mode) |
| 35 | fmt.Println(animeList) | 34 | // fmt.Println(animeList) |
| 36 | } | 35 | // } |
| 37 | 36 | ||
| 38 | // searchAnime performs the API call and fetches anime information | 37 | // searchAnime performs the API call and fetches anime information |
| 39 | func searchAnime(query, mode string) string { | 38 | func SearchAnime(query, mode string) (map[string]string, error) { |
| 40 | const ( | 39 | const ( |
| 41 | agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" | 40 | agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" |
| 42 | allanimeRef = "https://allanime.to" | 41 | allanimeRef = "https://allanime.to" |
| @@ -44,6 +43,9 @@ func searchAnime(query, mode string) string { | |||
| 44 | allanimeAPI = "https://api." + allanimeBase + "/api" | 43 | allanimeAPI = "https://api." + allanimeBase + "/api" |
| 45 | ) | 44 | ) |
| 46 | 45 | ||
| 46 | // Format and return the anime list | ||
| 47 | animeList := make(map[string]string) | ||
| 48 | |||
| 47 | 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 } } }` | 49 | 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 } } }` |
| 48 | 50 | ||
| 49 | // Build the request URL | 51 | // Build the request URL |
| @@ -53,7 +55,7 @@ func searchAnime(query, mode string) string { | |||
| 53 | req, err := http.NewRequest("GET", url, nil) | 55 | req, err := http.NewRequest("GET", url, nil) |
| 54 | if err != nil { | 56 | if err != nil { |
| 55 | fmt.Println("Error creating HTTP request:", err) | 57 | fmt.Println("Error creating HTTP request:", err) |
| 56 | return "" | 58 | return animeList, err |
| 57 | } | 59 | } |
| 58 | req.Header.Set("User-Agent", agent) | 60 | req.Header.Set("User-Agent", agent) |
| 59 | req.Header.Set("Referer", allanimeRef) | 61 | req.Header.Set("Referer", allanimeRef) |
| @@ -62,26 +64,24 @@ func searchAnime(query, mode string) string { | |||
| 62 | resp, err := client.Do(req) | 64 | resp, err := client.Do(req) |
| 63 | if err != nil { | 65 | if err != nil { |
| 64 | fmt.Println("Error making HTTP request:", err) | 66 | fmt.Println("Error making HTTP request:", err) |
| 65 | return "" | 67 | return animeList, err |
| 66 | } | 68 | } |
| 67 | defer resp.Body.Close() | 69 | defer resp.Body.Close() |
| 68 | 70 | ||
| 69 | body, err := ioutil.ReadAll(resp.Body) | 71 | body, err := ioutil.ReadAll(resp.Body) |
| 70 | if err != nil { | 72 | if err != nil { |
| 71 | fmt.Println("Error reading response body:", err) | 73 | fmt.Println("Error reading response body:", err) |
| 72 | return "" | 74 | return animeList, err |
| 73 | } | 75 | } |
| 74 | 76 | ||
| 75 | // Parse the JSON response | 77 | // Parse the JSON response |
| 76 | var response Response | 78 | var response response |
| 77 | err = json.Unmarshal(body, &response) | 79 | err = json.Unmarshal(body, &response) |
| 78 | if err != nil { | 80 | if err != nil { |
| 79 | fmt.Println("Error parsing JSON:", err) | 81 | fmt.Println("Error parsing JSON:", err) |
| 80 | return "" | 82 | return animeList, err |
| 81 | } | 83 | } |
| 82 | 84 | ||
| 83 | // Format and return the anime list | ||
| 84 | var animeList strings.Builder | ||
| 85 | for _, anime := range response.Data.Shows.Edges { | 85 | for _, anime := range response.Data.Shows.Edges { |
| 86 | // availableEpisodes, _ := anime.AvailableEpisodes.Int64() // Converts json.Number to int64 | 86 | // availableEpisodes, _ := anime.AvailableEpisodes.Int64() // Converts json.Number to int64 |
| 87 | 87 | ||
| @@ -97,23 +97,8 @@ func searchAnime(query, mode string) string { | |||
| 97 | default: | 97 | default: |
| 98 | episodesStr = "Unknown" | 98 | episodesStr = "Unknown" |
| 99 | } | 99 | } |
| 100 | 100 | animeList[anime.ID] = fmt.Sprintf("%s (%s episodes)", anime.Name, episodesStr) | |
| 101 | 101 | // animeList.WriteString(fmt.Sprintf("%s\t%s (%s episodes)\n", anime.ID, anime.Name, episodesStr)) | |
| 102 | animeList.WriteString(fmt.Sprintf("%s\t%s (%s episodes)\n", anime.ID, anime.Name, episodesStr)) | ||
| 103 | |||
| 104 | // In the formatting part | ||
| 105 | // animeList.WriteString(fmt.Sprintf("%s\t%s (%d episodes)NEWLINEFROMHERE\n", anime.ID, anime.Name, availableEpisodes)) | ||
| 106 | |||
| 107 | // animeList.WriteString(fmt.Sprintf("%s\t%s (%d episodes)NEWLINEFROMHERE\n", anime.ID, anime.Name, anime.AvailableEpisodes)) | ||
| 108 | } | 102 | } |
| 109 | return animeList.String() | 103 | return animeList, nil |
| 110 | } | 104 | } |
| 111 | |||
| 112 | // getEnv returns the value of an environment variable or a default value if it's not set | ||
| 113 | func getEnv(key, defaultValue string) string { | ||
| 114 | value := os.Getenv(key) | ||
| 115 | if value == "" { | ||
| 116 | return defaultValue | ||
| 117 | } | ||
| 118 | return value | ||
| 119 | }\ No newline at end of file | ||
internal/curd.go added +54
| @@ -0,0 +1,54 @@ | |||
| 1 | package internal | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "encoding/json" | ||
| 5 | "fmt" | ||
| 6 | "os" | ||
| 7 | "runtime" | ||
| 8 | "time" | ||
| 9 | // "log" | ||
| 10 | ) | ||
| 11 | |||
| 12 | // LogData logs the input data into a specified log file with the format [LOG] time lineNumber: logData | ||
| 13 | func Log(data interface{}, logFile string) error { | ||
| 14 | // Open or create the log file | ||
| 15 | file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) | ||
| 16 | if err != nil { | ||
| 17 | return err | ||
| 18 | } | ||
| 19 | defer file.Close() // Ensure the file is closed when done | ||
| 20 | |||
| 21 | // Attempt to marshal the data into JSON | ||
| 22 | jsonData, err := json.Marshal(data) | ||
| 23 | if err != nil { | ||
| 24 | return err | ||
| 25 | } | ||
| 26 | |||
| 27 | // Get the caller information | ||
| 28 | _, filename, lineNumber, ok := runtime.Caller(1) // Caller 1 gives the caller of LogData | ||
| 29 | if !ok { | ||
| 30 | return fmt.Errorf("unable to get caller information") | ||
| 31 | } | ||
| 32 | |||
| 33 | // Log the current time and the JSON representation along with caller info | ||
| 34 | currentTime := time.Now().Format("2006/01/02 15:04:05") | ||
| 35 | logMessage := fmt.Sprintf("[LOG] %s %s %d: %s\n", currentTime, filename, lineNumber, jsonData) | ||
| 36 | _, err = fmt.Fprint(file, logMessage) // Write to the file | ||
| 37 | if err != nil { | ||
| 38 | return err | ||
| 39 | } | ||
| 40 | |||
| 41 | return nil | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | // ClearScreen clears the terminal screen and saves the state | ||
| 46 | func ClearScreen() { | ||
| 47 | fmt.Print("\033[?1049h") // Switch to alternate screen buffer | ||
| 48 | fmt.Print("\033[H") // Move cursor to the top left | ||
| 49 | } | ||
| 50 | |||
| 51 | // RestoreScreen restores the original terminal state | ||
| 52 | func RestoreScreen() { | ||
| 53 | fmt.Print("\033[?1049l") // Switch back to the main screen buffer | ||
| 54 | }\ No newline at end of file | ||
internal/episode_list.go +21 −21
| @@ -1,4 +1,4 @@ | |||
| 1 | package main | 1 | package internal |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| @@ -7,10 +7,9 @@ import ( | |||
| 7 | "net/http" | 7 | "net/http" |
| 8 | "sort" | 8 | "sort" |
| 9 | "strconv" | 9 | "strconv" |
| 10 | "strings" | ||
| 11 | ) | 10 | ) |
| 12 | 11 | ||
| 13 | type EpisodesResponse struct { | 12 | type episodesResponse struct { |
| 14 | Data struct { | 13 | Data struct { |
| 15 | Show struct { | 14 | Show struct { |
| 16 | ID string `json:"_id"` | 15 | ID string `json:"_id"` |
| @@ -19,20 +18,20 @@ type EpisodesResponse struct { | |||
| 19 | } `json:"data"` | 18 | } `json:"data"` |
| 20 | } | 19 | } |
| 21 | 20 | ||
| 22 | func main() { | 21 | // func main() { |
| 23 | // Get environment variables | 22 | // // Get environment variables |
| 24 | // Read the ID from the file | 23 | // // Read the ID from the file |
| 25 | id := "ReooPAxPMsHM4KPMY" | 24 | // id := "ReooPAxPMsHM4KPMY" |
| 26 | 25 | ||
| 27 | // Fetch episodes list | 26 | // // Fetch episodes list |
| 28 | episodeList := episodesList(string(id), "sub") | 27 | // episodeList := episodesList(string(id), "sub") |
| 29 | 28 | ||
| 30 | // Write the episode list to a file | 29 | // // Write the episode list to a file |
| 31 | fmt.Println(episodeList) | 30 | // fmt.Println(episodeList) |
| 32 | } | 31 | // } |
| 33 | 32 | ||
| 34 | // episodesList performs the API call and fetches the episodes list | 33 | // episodesList performs the API call and fetches the episodes list |
| 35 | func episodesList(showID, mode string) string { | 34 | func EpisodesList(showID, mode string) ([]string, error) { |
| 36 | const ( | 35 | const ( |
| 37 | agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" | 36 | agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" |
| 38 | allanimeRef = "https://allanime.to" | 37 | allanimeRef = "https://allanime.to" |
| @@ -44,12 +43,13 @@ func episodesList(showID, mode string) string { | |||
| 44 | 43 | ||
| 45 | // Build the request URL | 44 | // Build the request URL |
| 46 | url := fmt.Sprintf("%s?variables={\"showId\":\"%s\"}&query=%s", allanimeAPI, showID, episodesListGql) | 45 | url := fmt.Sprintf("%s?variables={\"showId\":\"%s\"}&query=%s", allanimeAPI, showID, episodesListGql) |
| 46 | episodes := []string{} | ||
| 47 | 47 | ||
| 48 | // Make the HTTP request | 48 | // Make the HTTP request |
| 49 | req, err := http.NewRequest("GET", url, nil) | 49 | req, err := http.NewRequest("GET", url, nil) |
| 50 | if err != nil { | 50 | if err != nil { |
| 51 | fmt.Println("Error creating HTTP request:", err) | 51 | fmt.Println("Error creating HTTP request:", err) |
| 52 | return "" | 52 | return episodes, err |
| 53 | } | 53 | } |
| 54 | req.Header.Set("User-Agent", agent) | 54 | req.Header.Set("User-Agent", agent) |
| 55 | req.Header.Set("Referer", allanimeRef) | 55 | req.Header.Set("Referer", allanimeRef) |
| @@ -58,27 +58,27 @@ func episodesList(showID, mode string) string { | |||
| 58 | resp, err := client.Do(req) | 58 | resp, err := client.Do(req) |
| 59 | if err != nil { | 59 | if err != nil { |
| 60 | fmt.Println("Error making HTTP request:", err) | 60 | fmt.Println("Error making HTTP request:", err) |
| 61 | return "" | 61 | return episodes, err |
| 62 | } | 62 | } |
| 63 | defer resp.Body.Close() | 63 | defer resp.Body.Close() |
| 64 | 64 | ||
| 65 | body, err := ioutil.ReadAll(resp.Body) | 65 | body, err := ioutil.ReadAll(resp.Body) |
| 66 | if err != nil { | 66 | if err != nil { |
| 67 | fmt.Println("Error reading response body:", err) | 67 | fmt.Println("Error reading response body:", err) |
| 68 | return "" | 68 | return episodes, err |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | // Parse the JSON response | 71 | // Parse the JSON response |
| 72 | var response EpisodesResponse | 72 | var response episodesResponse |
| 73 | err = json.Unmarshal(body, &response) | 73 | err = json.Unmarshal(body, &response) |
| 74 | if err != nil { | 74 | if err != nil { |
| 75 | fmt.Println("Error parsing JSON:", err) | 75 | fmt.Println("Error parsing JSON:", err) |
| 76 | return "" | 76 | return episodes, err |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | // Extract and sort the episodes | 79 | // Extract and sort the episodes |
| 80 | episodes := extractEpisodes(response.Data.Show.AvailableEpisodesDetail, mode) | 80 | episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, mode) |
| 81 | return strings.Join(episodes, "\n") | 81 | return episodes, nil |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | // extractEpisodes extracts the episodes list from the availableEpisodesDetail field | 84 | // extractEpisodes extracts the episodes list from the availableEpisodesDetail field |
| @@ -100,7 +100,7 @@ func extractEpisodes(availableEpisodesDetail map[string]interface{}, mode string | |||
| 100 | // Convert to string and return | 100 | // Convert to string and return |
| 101 | var episodesStr []string | 101 | var episodesStr []string |
| 102 | for _, ep := range episodes { | 102 | for _, ep := range episodes { |
| 103 | episodesStr = append(episodesStr, fmt.Sprintf("%.1f", ep)) | 103 | episodesStr = append(episodesStr, fmt.Sprintf("%v", ep)) |
| 104 | } | 104 | } |
| 105 | return episodesStr | 105 | return episodesStr |
| 106 | } | 106 | } |
internal/episode_url.go +30 −31
| @@ -1,4 +1,4 @@ | |||
| 1 | package main | 1 | package internal |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| @@ -6,13 +6,12 @@ import ( | |||
| 6 | "io/ioutil" | 6 | "io/ioutil" |
| 7 | "net/http" | 7 | "net/http" |
| 8 | "net/url" | 8 | "net/url" |
| 9 | "os" | ||
| 10 | "regexp" | 9 | "regexp" |
| 11 | "strings" | 10 | "strings" |
| 12 | "unicode" | 11 | "unicode" |
| 13 | ) | 12 | ) |
| 14 | 13 | ||
| 15 | type Config struct { | 14 | type config struct { |
| 16 | Agent string | 15 | Agent string |
| 17 | AllanimeRefr string | 16 | AllanimeRefr string |
| 18 | AllanimeBase string | 17 | AllanimeBase string |
| @@ -21,7 +20,7 @@ type Config struct { | |||
| 21 | Quality string | 20 | Quality string |
| 22 | } | 21 | } |
| 23 | 22 | ||
| 24 | type AllanimeResponse struct { | 23 | type allanimeResponse struct { |
| 25 | Data struct { | 24 | Data struct { |
| 26 | Episode struct { | 25 | Episode struct { |
| 27 | SourceUrls []struct { | 26 | SourceUrls []struct { |
| @@ -31,13 +30,13 @@ type AllanimeResponse struct { | |||
| 31 | } `json:"data"` | 30 | } `json:"data"` |
| 32 | } | 31 | } |
| 33 | 32 | ||
| 34 | func getDefaultConfig() Config { | 33 | func GetDefaultAllanimeConfig() config { |
| 35 | return Config{ | 34 | return config{ |
| 36 | Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0", | 35 | Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0", |
| 37 | AllanimeRefr: "https://allanime.to", | 36 | AllanimeRefr: "https://allanime.to", |
| 38 | AllanimeBase: "allanime.day", | 37 | AllanimeBase: "allanime.day", |
| 39 | AllanimeAPI: "https://api.allanime.day", | 38 | AllanimeAPI: "https://api.allanime.day", |
| 40 | Mode: "dub", | 39 | Mode: "sub", |
| 41 | Quality: "best", | 40 | Quality: "best", |
| 42 | } | 41 | } |
| 43 | } | 42 | } |
| @@ -125,7 +124,7 @@ func extractLinks(provider_id string) map[string]interface{} { | |||
| 125 | // Returns: | 124 | // Returns: |
| 126 | // - []string: a list of links for specified episode. | 125 | // - []string: a list of links for specified episode. |
| 127 | // - error: an error if the episode is not found or if there is an issue during the search. | 126 | // - error: an error if the episode is not found or if there is an issue during the search. |
| 128 | func getEpisodeURL(config Config, id, epNo string) ([]string, error) { | 127 | func GetEpisodeURL(config config, id, epNo string) ([]string, error) { |
| 129 | query := `query($showId:String!,$translationType:VaildTranslationTypeEnumType!,$episodeString:String!){episode(showId:$showId,translationType:$translationType,episodeString:$episodeString){episodeString sourceUrls}}` | 128 | query := `query($showId:String!,$translationType:VaildTranslationTypeEnumType!,$episodeString:String!){episode(showId:$showId,translationType:$translationType,episodeString:$episodeString){episodeString sourceUrls}}` |
| 130 | 129 | ||
| 131 | variables := map[string]string{ | 130 | variables := map[string]string{ |
| @@ -168,7 +167,7 @@ func getEpisodeURL(config Config, id, epNo string) ([]string, error) { | |||
| 168 | responseStr := string(body) | 167 | responseStr := string(body) |
| 169 | 168 | ||
| 170 | // Unmarshal the JSON data into the struct | 169 | // Unmarshal the JSON data into the struct |
| 171 | var response AllanimeResponse | 170 | var response allanimeResponse |
| 172 | err = json.Unmarshal([]byte(responseStr), &response) | 171 | err = json.Unmarshal([]byte(responseStr), &response) |
| 173 | if err != nil { | 172 | if err != nil { |
| 174 | fmt.Println("Error parsing JSON: ", err) | 173 | fmt.Println("Error parsing JSON: ", err) |
| @@ -198,25 +197,25 @@ func getEpisodeURL(config Config, id, epNo string) ([]string, error) { | |||
| 198 | return allinks, nil | 197 | return allinks, nil |
| 199 | } | 198 | } |
| 200 | 199 | ||
| 201 | func main() { | 200 | // func main() { |
| 202 | config := getDefaultConfig() | 201 | // config := getDefaultConfig() |
| 203 | 202 | ||
| 204 | id := "ReooPAxPMsHM4KPMY" // One piece | 203 | // id := "ReooPAxPMsHM4KPMY" // One piece |
| 205 | // id := "RezHft5pjutwWcE3B" // Death note | 204 | // // id := "RezHft5pjutwWcE3B" // Death note |
| 206 | epNo := "945" | 205 | // epNo := "945" |
| 207 | 206 | ||
| 208 | links, err := getEpisodeURL(config, id, epNo) | 207 | // links, err := getEpisodeURL(config, id, epNo) |
| 209 | if err != nil { | 208 | // if err != nil { |
| 210 | fmt.Printf("Error: %v\n", err) | 209 | // fmt.Printf("Error: %v\n", err) |
| 211 | if strings.Contains(err.Error(), "no source URLs found") { | 210 | // if strings.Contains(err.Error(), "no source URLs found") { |
| 212 | fmt.Println("Episode not released!") | 211 | // fmt.Println("Episode not released!") |
| 213 | } | 212 | // } |
| 214 | os.Exit(1) | 213 | // os.Exit(1) |
| 215 | } | 214 | // } |
| 216 | 215 | ||
| 217 | // Print all found links | 216 | // // Print all found links |
| 218 | // fmt.Println("links:", links) | 217 | // // fmt.Println("links:", links) |
| 219 | for _, link := range links { | 218 | // for _, link := range links { |
| 220 | fmt.Println(link) | 219 | // fmt.Println(link) |
| 221 | } | 220 | // } |
| 222 | }\ No newline at end of file | 221 | // }\ No newline at end of file |
internal/selection_menu.go added +123
| @@ -0,0 +1,123 @@ | |||
| 1 | package internal | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "strings" | ||
| 6 | tea "github.com/charmbracelet/bubbletea" | ||
| 7 | ) | ||
| 8 | |||
| 9 | // SelectionOption holds the label and the internal key | ||
| 10 | type SelectionOption struct { | ||
| 11 | Label string | ||
| 12 | Key string | ||
| 13 | } | ||
| 14 | |||
| 15 | // Model represents the application state for the selection prompt | ||
| 16 | type Model struct { | ||
| 17 | options map[string]string // Now using a map for options | ||
| 18 | filter string | ||
| 19 | filteredKeys []SelectionOption | ||
| 20 | selected int | ||
| 21 | } | ||
| 22 | |||
| 23 | // Init initializes the model | ||
| 24 | func (m Model) Init() tea.Cmd { | ||
| 25 | return nil | ||
| 26 | } | ||
| 27 | |||
| 28 | // Update handles user input and updates the model | ||
| 29 | func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| 30 | switch msg := msg.(type) { | ||
| 31 | case tea.KeyMsg: | ||
| 32 | switch msg.String() { | ||
| 33 | case "ctrl+c", "q": | ||
| 34 | return m, tea.Quit | ||
| 35 | case "backspace": | ||
| 36 | if len(m.filter) > 0 { | ||
| 37 | m.filter = m.filter[:len(m.filter)-1] | ||
| 38 | } | ||
| 39 | case "down": | ||
| 40 | if m.selected < len(m.filteredKeys)-1 { | ||
| 41 | m.selected++ | ||
| 42 | } | ||
| 43 | case "up": | ||
| 44 | if m.selected > 0 { | ||
| 45 | m.selected-- | ||
| 46 | } | ||
| 47 | case "enter": | ||
| 48 | return m, tea.Quit // Or return the selected option | ||
| 49 | default: | ||
| 50 | if msg.String() != "" { | ||
| 51 | m.filter += msg.String() | ||
| 52 | } | ||
| 53 | } | ||
| 54 | m.filterOptions() | ||
| 55 | } | ||
| 56 | return m, nil | ||
| 57 | } | ||
| 58 | |||
| 59 | // View renders the UI | ||
| 60 | func (m Model) View() string { | ||
| 61 | var b strings.Builder | ||
| 62 | |||
| 63 | b.WriteString("Search (Press Ctrl+C to quit):\n\n") | ||
| 64 | b.WriteString("Filter: " + m.filter + "\n\n") | ||
| 65 | |||
| 66 | if len(m.filteredKeys) == 0 { | ||
| 67 | b.WriteString("No matches found.\n") | ||
| 68 | } else { | ||
| 69 | for i, option := range m.filteredKeys { | ||
| 70 | if i == m.selected { | ||
| 71 | b.WriteString(fmt.Sprintf("▶ %s\n", option.Label)) | ||
| 72 | } else { | ||
| 73 | b.WriteString(fmt.Sprintf(" %s\n", option.Label)) | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | return b.String() | ||
| 79 | } | ||
| 80 | |||
| 81 | // filterOptions filters options based on the search term | ||
| 82 | func (m *Model) filterOptions() { | ||
| 83 | m.filteredKeys = nil | ||
| 84 | for key, value := range m.options { | ||
| 85 | if strings.Contains(strings.ToLower(value), strings.ToLower(m.filter)) { // Check values instead of keys | ||
| 86 | m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key}) // Set Label to value | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | // DynamicSelect displays a full-screen selection prompt with search | ||
| 92 | func DynamicSelect(options map[string]string) (SelectionOption, error) { | ||
| 93 | ClearScreen() | ||
| 94 | defer RestoreScreen() | ||
| 95 | model := &Model{ // Use a pointer to Model | ||
| 96 | options: options, | ||
| 97 | filteredKeys: make([]SelectionOption, 0), // Initialize filteredKeys | ||
| 98 | } | ||
| 99 | |||
| 100 | // Populate filteredKeys initially | ||
| 101 | model.filterOptions() | ||
| 102 | |||
| 103 | // Create a new Program instance | ||
| 104 | p := tea.NewProgram(model) | ||
| 105 | |||
| 106 | // Run the program and capture the final model and any error | ||
| 107 | finalModel, err := p.Run() | ||
| 108 | if err != nil { | ||
| 109 | return SelectionOption{}, err | ||
| 110 | } | ||
| 111 | |||
| 112 | // Type assert to get the specific model type | ||
| 113 | finalSelectionModel, ok := finalModel.(*Model) | ||
| 114 | if !ok { | ||
| 115 | return SelectionOption{}, fmt.Errorf("unexpected model type") | ||
| 116 | } | ||
| 117 | |||
| 118 | // Return the selected option after quitting | ||
| 119 | if finalSelectionModel.selected < len(finalSelectionModel.filteredKeys) { | ||
| 120 | return finalSelectionModel.filteredKeys[finalSelectionModel.selected], nil | ||
| 121 | } | ||
| 122 | return SelectionOption{}, nil | ||
| 123 | } | ||