From c70bf198058eda4723df141608bc897dbe5d9e63 Mon Sep 17 00:00:00 2001 From: Jason <38664452+uchks@users.noreply.github.com> Date: Sun, 10 Dec 2023 23:34:37 -0600 Subject: [PATCH] Modularize --- go.mod | 15 +++++++++ go.sum | 20 ++++++++++++ main.go | 83 +++++++++++++++++++++++------------------------ network.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++ terminal.go | 25 ++++++++++++++ 5 files changed, 193 insertions(+), 43 deletions(-) create mode 100644 go.sum create mode 100644 network.go create mode 100644 terminal.go diff --git a/go.mod b/go.mod index bc0de39..86d65a8 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,18 @@ module main.go go 1.21.4 + +require ( + github.com/atotto/clipboard v0.1.4 + github.com/cheggaaa/pb/v3 v3.1.4 +) + +require ( + github.com/VividCortex/ewma v1.2.0 // indirect + github.com/fatih/color v1.15.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + golang.org/x/sys v0.6.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0a23bfb --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= +github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/cheggaaa/pb/v3 v3.1.4 h1:DN8j4TVVdKu3WxVwcRKu0sG00IIU6FewoABZzXbRQeo= +github.com/cheggaaa/pb/v3 v3.1.4/go.mod h1:6wVjILNBaXMs8c21qRiaUM8BR82erfgau1DQ4iUXmSA= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index ec0796d..6c7e3e2 100644 --- a/main.go +++ b/main.go @@ -1,70 +1,67 @@ +// main.go package main import ( "bufio" "fmt" - "io" - "net/http" + "github.com/atotto/clipboard" "os" "strings" ) func main() { - var url string + clear() - // Check if a URL argument is provided - if len(os.Args) > 1 { - url = os.Args[1] - } else { - // No URL argument, prompt user for URL - url = promptForURL() + url := getURLFromArgsOrPrompt() + if !isValidQiwiURL(url) { + return + } + newLink, fileName := processURL(url) + if newLink == "" { + return } - // Process the URL - processURL(url) + handleDownloadDecision(newLink, fileName) } -func promptForURL() string { +func getURLFromArgsOrPrompt() string { + if len(os.Args) > 1 { + return os.Args[1] + } + reader := bufio.NewReader(os.Stdin) fmt.Print("Enter download link: ") url, _ := reader.ReadString('\n') return strings.TrimSpace(url) } -func processURL(url string) { - resp, err := http.Get(url) - if err != nil { - fmt.Println("Error while sending request:", err) - return +func isValidQiwiURL(url string) bool { + if strings.HasPrefix(url, "https://qiwi.gg/folder") { + fmt.Println("Error: Folder handling isn't implemented at the moment.") + return false + } else if !strings.HasPrefix(url, "https://qiwi.gg/file") { + fmt.Println("Error: Invalid URL. Only qiwi.gg/file URLs are supported.") + return false } - defer resp.Body.Close() - - body, err := io.ReadAll(resp.Body) - if err != nil { - fmt.Println("Error while reading response:", err) - return - } - bodyString := string(body) - - slug := extractBetween(bodyString, "\\\"slug\\\":\\\"", "\\\"") - fileName := extractBetween(bodyString, "\\\"fileName\\\":\\\"", "\\\"") - ext := fileName[strings.LastIndex(fileName, ".")+1:] - - fmt.Printf("https://qiwi.lol/%s.%s\n", slug, ext) + return true } -// extractBetween finds a substring between two delimiters -func extractBetween(s, start, end string) string { - startIndex := strings.Index(s, start) - if startIndex == -1 { - return "" - } - startIndex += len(start) +func confirmDownload() bool { + reader := bufio.NewReader(os.Stdin) + fmt.Print("Do you want to download the file? (y/n): ") + response, _ := reader.ReadString('\n') + return strings.HasPrefix(strings.TrimSpace(strings.ToLower(response)), "y") +} - endIndex := strings.Index(s[startIndex:], end) - if endIndex == -1 { - return "" +func handleDownloadDecision(newLink, fileName string) { + if confirmDownload() { + downloadFile(newLink, fileName) + } else { + err := clipboard.WriteAll(newLink) + if err != nil { + fmt.Println("Failed to copy to clipboard:", err) + } else { + fmt.Println("Download link copied to clipboard.") + } } - - return s[startIndex : startIndex+endIndex] } diff --git a/network.go b/network.go new file mode 100644 index 0000000..14106de --- /dev/null +++ b/network.go @@ -0,0 +1,93 @@ +// network.go +package main + +import ( + "fmt" + "github.com/cheggaaa/pb/v3" + "io" + "net/http" + "os" + "strings" +) + +func processURL(url string) (string, string) { + resp, err := http.Get(url) + if err != nil { + fmt.Println("Error while sending request:", err) + return "", "" + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error while reading response:", err) + return "", "" + } + bodyString := string(body) + + slug, fileName := extractSlugAndFileName(bodyString) + if slug == "" || fileName == "" { + fmt.Println("Error: Unable to extract slug or file name.") + return "", "" + } + + ext := extractExtension(fileName) + newLink := fmt.Sprintf("https://qiwi.lol/%s.%s", slug, ext) + fmt.Println("New Download Link:", newLink) + + return newLink, fileName +} + +func extractSlugAndFileName(bodyString string) (string, string) { + slug := extractBetween(bodyString, "\\\"slug\\\":\\\"", "\\\"") + fileName := extractBetween(bodyString, "\\\"fileName\\\":\\\"", "\\\"") + return slug, fileName +} + +func extractExtension(fileName string) string { + return fileName[strings.LastIndex(fileName, ".")+1:] +} + +func extractBetween(s, start, end string) string { + startIndex := strings.Index(s, start) + if startIndex == -1 { + return "" + } + startIndex += len(start) + + endIndex := strings.Index(s[startIndex:], end) + if endIndex == -1 { + return "" + } + + return s[startIndex : startIndex+endIndex] +} + +func downloadFile(url, fileName string) { + resp, err := http.Get(url) + if err != nil { + fmt.Println("Error while downloading the file:", err) + return + } + defer resp.Body.Close() + + out, err := os.Create(fileName) + if err != nil { + fmt.Println("Error while creating the file:", err) + return + } + defer out.Close() + + size := resp.ContentLength + bar := pb.Full.Start64(size) + barReader := bar.NewProxyReader(resp.Body) + + _, err = io.Copy(out, barReader) + if err != nil { + fmt.Println("Error while writing to the file:", err) + return + } + + bar.Finish() + fmt.Printf("File successfully downloaded: '%s'\n", fileName) +} diff --git a/terminal.go b/terminal.go new file mode 100644 index 0000000..56db9fb --- /dev/null +++ b/terminal.go @@ -0,0 +1,25 @@ +// terminal.go +package main + +import ( + "fmt" + "os" + "os/exec" + "runtime" +) + +func clear() { + var cmd *exec.Cmd + switch runtime.GOOS { + case "windows": + cmd = exec.Command("cmd", "/c", "cls") + default: + cmd = exec.Command("clear") + } + + cmd.Stdout = os.Stdout + err := cmd.Run() + if err != nil { + fmt.Println("Failed to clear terminal:", err) + } +}