Commit 6b6cbc5f61
Unsigned
Layout: unified · split
buildwindows added +4
| @@ -0,0 +1,4 @@ | |||
| 1 | @echo off | ||
| 2 | set GOOS=windows | ||
| 3 | set GOARCH=amd64 | ||
| 4 | go build -o curd.exe -ldflags="-s -w" -trimpath cmd\curd\main.go\ No newline at end of file | ||
cmd/curd/main.go +9 −3
| @@ -6,6 +6,7 @@ import ( | |||
| 6 | "os" | 6 | "os" |
| 7 | "path/filepath" | 7 | "path/filepath" |
| 8 | "strconv" | 8 | "strconv" |
| 9 | "runtime" | ||
| 9 | "sync" | 10 | "sync" |
| 10 | "time" | 11 | "time" |
| 11 | "github.com/wraient/curd/internal" | 12 | "github.com/wraient/curd/internal" |
| @@ -22,7 +23,14 @@ func main() { | |||
| 22 | var anime internal.Anime | 23 | var anime internal.Anime |
| 23 | var user internal.User | 24 | var user internal.User |
| 24 | 25 | ||
| 25 | configFilePath := os.ExpandEnv("$HOME/.config/curd/curd.conf") | 26 | var homeDir string |
| 27 | if runtime.GOOS == "windows" { | ||
| 28 | homeDir = os.Getenv("USERPROFILE") | ||
| 29 | } else { | ||
| 30 | homeDir = os.Getenv("HOME") | ||
| 31 | } | ||
| 32 | |||
| 33 | configFilePath := filepath.Join(homeDir, ".config", "curd", "curd.conf") | ||
| 26 | 34 | ||
| 27 | // load curd userCurdConfig | 35 | // load curd userCurdConfig |
| 28 | userCurdConfig, err := internal.LoadConfig(configFilePath) | 36 | userCurdConfig, err := internal.LoadConfig(configFilePath) |
| @@ -67,8 +75,6 @@ func main() { | |||
| 67 | 75 | ||
| 68 | anime.Ep.ContinueLast = *continueLast | 76 | anime.Ep.ContinueLast = *continueLast |
| 69 | 77 | ||
| 70 | |||
| 71 | |||
| 72 | if *updateScript{ | 78 | if *updateScript{ |
| 73 | repo := "wraient/curd" | 79 | repo := "wraient/curd" |
| 74 | fileName := "curd" | 80 | fileName := "curd" |
go.mod +1 −2
| @@ -3,6 +3,7 @@ module github.com/wraient/curd | |||
| 3 | go 1.23.2 | 3 | go 1.23.2 |
| 4 | 4 | ||
| 5 | require ( | 5 | require ( |
| 6 | github.com/Microsoft/go-winio v0.6.2 | ||
| 6 | github.com/charmbracelet/bubbletea v1.1.1 | 7 | github.com/charmbracelet/bubbletea v1.1.1 |
| 7 | github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2 | 8 | github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2 |
| 8 | ) | 9 | ) |
| @@ -13,7 +14,6 @@ require ( | |||
| 13 | github.com/charmbracelet/x/ansi v0.2.3 // indirect | 14 | github.com/charmbracelet/x/ansi v0.2.3 // indirect |
| 14 | github.com/charmbracelet/x/term v0.2.0 // indirect | 15 | github.com/charmbracelet/x/term v0.2.0 // indirect |
| 15 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect | 16 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect |
| 16 | github.com/gorilla/websocket v1.4.2 // indirect | ||
| 17 | github.com/lucasb-eyer/go-colorful v1.2.0 // indirect | 17 | github.com/lucasb-eyer/go-colorful v1.2.0 // indirect |
| 18 | github.com/mattn/go-isatty v0.0.20 // indirect | 18 | github.com/mattn/go-isatty v0.0.20 // indirect |
| 19 | github.com/mattn/go-localereader v0.0.1 // indirect | 19 | github.com/mattn/go-localereader v0.0.1 // indirect |
| @@ -22,7 +22,6 @@ require ( | |||
| 22 | github.com/muesli/cancelreader v0.2.2 // indirect | 22 | github.com/muesli/cancelreader v0.2.2 // indirect |
| 23 | github.com/muesli/termenv v0.15.2 // indirect | 23 | github.com/muesli/termenv v0.15.2 // indirect |
| 24 | github.com/rivo/uniseg v0.4.7 // indirect | 24 | github.com/rivo/uniseg v0.4.7 // indirect |
| 25 | golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect | ||
| 26 | golang.org/x/sync v0.8.0 // indirect | 25 | golang.org/x/sync v0.8.0 // indirect |
| 27 | golang.org/x/sys v0.24.0 // indirect | 26 | golang.org/x/sys v0.24.0 // indirect |
| 28 | golang.org/x/text v0.14.0 // indirect | 27 | golang.org/x/text v0.14.0 // indirect |
go.sum +2 −11
| @@ -1,7 +1,7 @@ | |||
| 1 | github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= | ||
| 2 | github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= | ||
| 1 | github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= | 3 | 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= | 4 | github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= |
| 3 | github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4= | ||
| 4 | github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= | ||
| 5 | github.com/charmbracelet/bubbletea v1.1.1 h1:KJ2/DnmpfqFtDNVTvYZ6zpPFL9iRCRr0qqKOCvppbPY= | 5 | github.com/charmbracelet/bubbletea v1.1.1 h1:KJ2/DnmpfqFtDNVTvYZ6zpPFL9iRCRr0qqKOCvppbPY= |
| 6 | github.com/charmbracelet/bubbletea v1.1.1/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4= | 6 | github.com/charmbracelet/bubbletea v1.1.1/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4= |
| 7 | github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw= | 7 | github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw= |
| @@ -12,8 +12,6 @@ github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4h | |||
| 12 | github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0= | 12 | github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0= |
| 13 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= | 13 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= |
| 14 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= | 14 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= |
| 15 | github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= | ||
| 16 | github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= | ||
| 17 | github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2 h1:9qOViOQGFIP5ar+2NorfAIsfuADEKXtklySC0zNnYf4= | 15 | github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2 h1:9qOViOQGFIP5ar+2NorfAIsfuADEKXtklySC0zNnYf4= |
| 18 | github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2/go.mod h1:nGaW7CGfNZnhtiFxMpc4OZdqIexGXjUlBnlmpZmjEKA= | 16 | github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2/go.mod h1:nGaW7CGfNZnhtiFxMpc4OZdqIexGXjUlBnlmpZmjEKA= |
| 19 | github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= | 17 | github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= |
| @@ -33,20 +31,13 @@ github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1n | |||
| 33 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | 31 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= |
| 34 | github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= | 32 | github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= |
| 35 | github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | 33 | github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= |
| 36 | golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= | ||
| 37 | golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= | ||
| 38 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
| 39 | golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= | 34 | golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= |
| 40 | golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | 35 | golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= |
| 41 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
| 42 | golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | 36 | golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
| 43 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | 37 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
| 44 | golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= | 38 | golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= |
| 45 | golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | 39 | golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= |
| 46 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
| 47 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
| 48 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | 40 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= |
| 49 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | 41 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= |
| 50 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
| 51 | gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU= | 42 | gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU= |
| 52 | gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= | 43 | gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c= |
internal/anilist.go +3 −2
| @@ -8,6 +8,7 @@ import ( | |||
| 8 | "io/ioutil" | 8 | "io/ioutil" |
| 9 | "net/http" | 9 | "net/http" |
| 10 | "os" | 10 | "os" |
| 11 | "path/filepath" | ||
| 11 | "strconv" | 12 | "strconv" |
| 12 | "strings" | 13 | "strings" |
| 13 | ) | 14 | ) |
| @@ -19,7 +20,7 @@ func FindKeyByValue(m map[string]string, value string) (string, error) { | |||
| 19 | return key, nil // Return the key and true if the value is found | 20 | return key, nil // Return the key and true if the value is found |
| 20 | } | 21 | } |
| 21 | } | 22 | } |
| 22 | return "", fmt.Errorf("No key with value %v", value) // Return empty string and false if the value is not found | 23 | return "", fmt.Errorf("no key with value %v", value) // Return empty string and false if the value is not found |
| 23 | } | 24 | } |
| 24 | 25 | ||
| 25 | // GetAnimeMap takes an AnimeList and returns a map with media.id as key and media.title.english as value. | 26 | // GetAnimeMap takes an AnimeList and returns a map with media.id as key and media.title.english as value. |
| @@ -272,7 +273,7 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) { | |||
| 272 | 273 | ||
| 273 | // Function to load a JSON file | 274 | // Function to load a JSON file |
| 274 | func LoadJSONFile(filePath string) (map[string]interface{}, error) { | 275 | func LoadJSONFile(filePath string) (map[string]interface{}, error) { |
| 275 | data, err := os.ReadFile(filePath) | 276 | data, err := os.ReadFile(filepath.Clean(filePath)) |
| 276 | if err != nil { | 277 | if err != nil { |
| 277 | return nil, fmt.Errorf("failed to read file: %w", err) | 278 | return nil, fmt.Errorf("failed to read file: %w", err) |
| 278 | } | 279 | } |
internal/curd.go +62 −42
| @@ -117,57 +117,65 @@ func ExitCurd() { | |||
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | func UpdateCurd(repo, fileName string) error { | 119 | func UpdateCurd(repo, fileName string) error { |
| 120 | // Get the path of the currently running executable | 120 | // Get the path of the currently running executable |
| 121 | executablePath, err := os.Executable() | 121 | executablePath, err := os.Executable() |
| 122 | if err != nil { | 122 | if err != nil { |
| 123 | return fmt.Errorf("unable to find current executable: %v", err) | 123 | return fmt.Errorf("unable to find current executable: %v", err) |
| 124 | } | 124 | } |
| 125 | 125 | ||
| 126 | // GitHub release URL for curd | 126 | // Adjust file name for Windows |
| 127 | url := fmt.Sprintf("https://github.com/%s/releases/latest/download/%s", repo, fileName) | 127 | if runtime.GOOS == "windows" { |
| 128 | fileName += ".exe" | ||
| 129 | } | ||
| 128 | 130 | ||
| 129 | // Temporary path for the downloaded curd.py | 131 | // GitHub release URL for curd |
| 130 | tmpPath := executablePath + ".tmp" | 132 | url := fmt.Sprintf("https://github.com/%s/releases/latest/download/%s", repo, fileName) |
| 131 | 133 | ||
| 132 | // Download the curd.py file | 134 | // Temporary path for the downloaded curd executable |
| 133 | resp, err := http.Get(url) | 135 | tmpPath := executablePath + ".tmp" |
| 134 | if err != nil { | ||
| 135 | return fmt.Errorf("failed to download file: %v", err) | ||
| 136 | } | ||
| 137 | defer resp.Body.Close() | ||
| 138 | 136 | ||
| 139 | // Check if the download was successful | 137 | // Download the curd executable |
| 140 | if resp.StatusCode != http.StatusOK { | 138 | resp, err := http.Get(url) |
| 141 | return fmt.Errorf("failed to download file: received status code %d", resp.StatusCode) | 139 | if err != nil { |
| 142 | } | 140 | return fmt.Errorf("failed to download file: %v", err) |
| 141 | } | ||
| 142 | defer resp.Body.Close() | ||
| 143 | 143 | ||
| 144 | // Create a new temporary file | 144 | // Check if the download was successful |
| 145 | out, err := os.Create(tmpPath) | 145 | if resp.StatusCode != http.StatusOK { |
| 146 | if err != nil { | 146 | return fmt.Errorf("failed to download file: received status code %d", resp.StatusCode) |
| 147 | return fmt.Errorf("failed to create temporary file: %v", err) | 147 | } |
| 148 | } | ||
| 149 | defer out.Close() | ||
| 150 | 148 | ||
| 151 | // Write the downloaded content to the temporary file | 149 | // Create a new temporary file |
| 152 | _, err = io.Copy(out, resp.Body) | 150 | out, err := os.Create(tmpPath) |
| 153 | if err != nil { | 151 | if err != nil { |
| 154 | return fmt.Errorf("failed to save downloaded file: %v", err) | 152 | return fmt.Errorf("failed to create temporary file: %v", err) |
| 155 | } | 153 | } |
| 154 | defer out.Close() | ||
| 155 | |||
| 156 | // Write the downloaded content to the temporary file | ||
| 157 | _, err = io.Copy(out, resp.Body) | ||
| 158 | if err != nil { | ||
| 159 | return fmt.Errorf("failed to save downloaded file: %v", err) | ||
| 160 | } | ||
| 156 | 161 | ||
| 157 | // Close and rename the temporary file to replace the current executable | 162 | // Close and rename the temporary file to replace the current executable |
| 158 | out.Close() | 163 | out.Close() |
| 159 | 164 | ||
| 160 | // Replace the current executable with the downloaded curd | 165 | // Replace the current executable with the downloaded curd |
| 161 | if err := os.Rename(tmpPath, executablePath); err != nil { | 166 | if err := os.Rename(tmpPath, executablePath); err != nil { |
| 162 | return fmt.Errorf("failed to replace the current executable: %v", err) | 167 | return fmt.Errorf("failed to replace the current executable: %v", err) |
| 163 | } | 168 | } |
| 164 | fmt.Println("Downloaded curd executable to", executablePath) | 169 | fmt.Println("Downloaded curd executable to", executablePath) |
| 165 | // Ensure the new file has executable permissions | ||
| 166 | if err := os.Chmod(executablePath, 0755); err != nil { | ||
| 167 | return fmt.Errorf("failed to set permissions on the new file: %v", err) | ||
| 168 | } | ||
| 169 | 170 | ||
| 170 | return nil | 171 | if runtime.GOOS != "windows" { |
| 172 | // Ensure the new file has executable permissions | ||
| 173 | if err := os.Chmod(executablePath, 0755); err != nil { | ||
| 174 | return fmt.Errorf("failed to set permissions on the new file: %v", err) | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | return nil | ||
| 171 | } | 179 | } |
| 172 | 180 | ||
| 173 | func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) { | 181 | func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) { |
| @@ -207,7 +215,15 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni | |||
| 207 | 215 | ||
| 208 | // Get user id, username and Anime list | 216 | // Get user id, username and Anime list |
| 209 | user.Id, user.Username, err = GetAnilistUserID(user.Token) | 217 | user.Id, user.Username, err = GetAnilistUserID(user.Token) |
| 218 | if err != nil { | ||
| 219 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) | ||
| 220 | ExitCurd() | ||
| 221 | } | ||
| 210 | anilistUserData, err := GetUserData(user.Token, user.Id) | 222 | anilistUserData, err := GetUserData(user.Token, user.Id) |
| 223 | if err != nil { | ||
| 224 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) | ||
| 225 | ExitCurd() | ||
| 226 | } | ||
| 211 | user.AnimeList = ParseAnimeList(anilistUserData) | 227 | user.AnimeList = ParseAnimeList(anilistUserData) |
| 212 | animeListMap := GetAnimeMap(user.AnimeList) | 228 | animeListMap := GetAnimeMap(user.AnimeList) |
| 213 | var anilistSelectedOption SelectionOption | 229 | var anilistSelectedOption SelectionOption |
| @@ -284,6 +300,10 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni | |||
| 284 | 300 | ||
| 285 | // find anime in animeList | 301 | // find anime in animeList |
| 286 | anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)) | 302 | anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)) |
| 303 | if err != nil { | ||
| 304 | Log(fmt.Sprintf("Failed to find anime in animeList: %v", err), logFile) | ||
| 305 | ExitCurd() | ||
| 306 | } | ||
| 287 | 307 | ||
| 288 | // If unable to get Allanime id automatically get manually | 308 | // If unable to get Allanime id automatically get manually |
| 289 | if anime.AllanimeId == "" { | 309 | if anime.AllanimeId == "" { |
internal/player.go +62 −11
| @@ -6,11 +6,27 @@ import ( | |||
| 6 | "encoding/json" | 6 | "encoding/json" |
| 7 | "fmt" | 7 | "fmt" |
| 8 | "net" | 8 | "net" |
| 9 | "os" | ||
| 9 | "os/exec" | 10 | "os/exec" |
| 11 | "path/filepath" | ||
| 12 | "runtime" | ||
| 13 | "github.com/Microsoft/go-winio" | ||
| 10 | ) | 14 | ) |
| 11 | var logFile = "debug.log" | 15 | var logFile = "debug.log" |
| 12 | 16 | ||
| 17 | func getMPVPath() (string, error) { | ||
| 18 | exePath, err := os.Executable() | ||
| 19 | if err != nil { | ||
| 20 | return "", err | ||
| 21 | } | ||
| 22 | exeDir := filepath.Dir(exePath) | ||
| 23 | mpvPath := filepath.Join(exeDir, "bin", "mpv.exe") // Adjust the relative path | ||
| 24 | return mpvPath, nil | ||
| 25 | } | ||
| 26 | |||
| 13 | func StartVideo(link string, args []string) (string, error) { | 27 | func StartVideo(link string, args []string) (string, error) { |
| 28 | var command *exec.Cmd | ||
| 29 | |||
| 14 | // Generate a random number for the socket path | 30 | // Generate a random number for the socket path |
| 15 | randomBytes := make([]byte, 4) | 31 | randomBytes := make([]byte, 4) |
| 16 | _, err := rand.Read(randomBytes) | 32 | _, err := rand.Read(randomBytes) |
| @@ -18,22 +34,46 @@ func StartVideo(link string, args []string) (string, error) { | |||
| 18 | Log("Failed to generate random number", logFile) | 34 | Log("Failed to generate random number", logFile) |
| 19 | return "", fmt.Errorf("failed to generate random number: %w", err) | 35 | return "", fmt.Errorf("failed to generate random number: %w", err) |
| 20 | } | 36 | } |
| 37 | |||
| 21 | randomNumber := fmt.Sprintf("%x", randomBytes) | 38 | randomNumber := fmt.Sprintf("%x", randomBytes) |
| 22 | 39 | ||
| 23 | // Create the mpv socket path with the random number | 40 | // Create the mpv socket path with the random number |
| 24 | mpvSocketPath := fmt.Sprintf("/tmp/curd/curd_mpvsocket_%s", randomNumber) | 41 | var mpvSocketPath string |
| 25 | argsStr := "" | 42 | if runtime.GOOS == "windows" { |
| 26 | if len(args) > 0 { | 43 | mpvSocketPath = fmt.Sprintf(`\\.\pipe\curd_mpvsocket_%s`, randomNumber) |
| 27 | argsStr = " " + joinArgs(args) | 44 | } else { |
| 45 | mpvSocketPath = fmt.Sprintf("/tmp/curd/curd_mpvsocket_%s", randomNumber) | ||
| 28 | } | 46 | } |
| 29 | 47 | ||
| 30 | // Build the complete command string | 48 | // Prepare arguments for mpv |
| 31 | command := fmt.Sprintf("mpv%s --no-terminal --really-quiet --input-ipc-server=%s %s", argsStr, mpvSocketPath, link) | 49 | var mpvArgs []string |
| 50 | mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link) | ||
| 51 | // Add any additional arguments passed | ||
| 52 | if len(args) > 0 { | ||
| 53 | mpvArgs = append(mpvArgs, args...) } | ||
| 54 | |||
| 55 | if runtime.GOOS == "windows" { | ||
| 56 | // Get the path to mpv.exe for Windows | ||
| 57 | mpvPath, err := getMPVPath() | ||
| 58 | if err != nil { | ||
| 59 | fmt.Println("Error: Failed to get MPV path") | ||
| 60 | Log("Failed to get mpv path.", logFile) | ||
| 61 | return "", err | ||
| 62 | } | ||
| 32 | 63 | ||
| 33 | // Execute the command using exec | 64 | // Create command for Windows |
| 34 | cmd := exec.Command("bash", "-c", command) | 65 | command = exec.Command(mpvPath, mpvArgs...) |
| 35 | cmd.Start() | 66 | } else { |
| 67 | // Create command for Unix-like systems | ||
| 68 | command = exec.Command("mpv", mpvArgs...) | ||
| 69 | } | ||
| 36 | 70 | ||
| 71 | // Start the mpv process | ||
| 72 | err = command.Start() | ||
| 73 | if err != nil { | ||
| 74 | fmt.Println("Error: Failed to start mpv process") | ||
| 75 | return "", fmt.Errorf("failed to start mpv: %w", err) | ||
| 76 | } | ||
| 37 | return mpvSocketPath, nil | 77 | return mpvSocketPath, nil |
| 38 | } | 78 | } |
| 39 | 79 | ||
| @@ -50,7 +90,15 @@ func joinArgs(args []string) string { | |||
| 50 | } | 90 | } |
| 51 | 91 | ||
| 52 | func MPVSendCommand(ipcSocketPath string, command []interface{}) (interface{}, error) { | 92 | func MPVSendCommand(ipcSocketPath string, command []interface{}) (interface{}, error) { |
| 53 | conn, err := net.Dial("unix", ipcSocketPath) | 93 | var conn net.Conn |
| 94 | var err error | ||
| 95 | |||
| 96 | if runtime.GOOS == "windows" { | ||
| 97 | // Use named pipe for Windows | ||
| 98 | conn, err = winio.DialPipe(ipcSocketPath, nil) | ||
| 99 | } else { | ||
| 100 | conn, err = net.Dial("unix", ipcSocketPath) | ||
| 101 | } | ||
| 54 | if err != nil { | 102 | if err != nil { |
| 55 | return nil, err | 103 | return nil, err |
| 56 | } | 104 | } |
| @@ -64,7 +112,10 @@ func MPVSendCommand(ipcSocketPath string, command []interface{}) (interface{}, e | |||
| 64 | } | 112 | } |
| 65 | 113 | ||
| 66 | // Send the command | 114 | // Send the command |
| 67 | conn.Write(append(commandStr, '\n')) | 115 | _, err = conn.Write(append(commandStr, '\n')) |
| 116 | if err != nil { | ||
| 117 | return nil, err | ||
| 118 | } | ||
| 68 | 119 | ||
| 69 | // Receive the response | 120 | // Receive the response |
| 70 | buf := make([]byte, 4096) | 121 | buf := make([]byte, 4096) |