Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: try golang #59

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions mkbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/user"
"path/filepath"
"sync"
)

type ImageData map[string]string

type Response struct {
Version int `json:"version,omitempty"`
Data map[string]struct {
Dhd string `json:"dhd"`
}
}

const DATA_URL = "https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s"

func downloadImages(key string, rawURL string, downloadsPath string, wg *sync.WaitGroup, channel chan<- string) {
defer wg.Done()

resp, err := http.Get(rawURL)

if err != nil {
res := fmt.Sprintf("error fetching data: %v", err)
channel <- "ERR:" + res
return
}

defer resp.Body.Close()

imageFileName := filepath.Join(downloadsPath, key+".jpg")
outFile, err := os.Create(imageFileName)
if err != nil {
res := fmt.Sprintf("failed to create file: %v", err)
channel <- "ERR:" + res
return
}

defer outFile.Close()

_, err = io.Copy(outFile, resp.Body)
if err != nil {
res := fmt.Sprintf("failed to save image: %v", err)
channel <- "ERR:" + res
return
}

channel <- fmt.Sprintf("️🖼️ Saved image to %s", imageFileName)
}

func main() {

resp, err := http.Get(DATA_URL)

if err != nil {
panic(err)
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}

response := Response{}

unmarshalErr := json.Unmarshal(body, &response)

if unmarshalErr != nil {
fmt.Println("Error parsing data:", unmarshalErr)
panic(unmarshalErr)
}

usr, err := user.Current()

if err != nil {
fmt.Println("Error getting user:", err)
return
}

downloadsPath := filepath.Join(usr.HomeDir, "Downloads", "MKBSD")

// Create the folder in the Downloads directory
err = os.MkdirAll(downloadsPath, 0755) // 0755 is the permission for the new folder
if err != nil {
fmt.Println("Error creating directory:", err)
return
}

fmt.Println("Folder created successfully at:", downloadsPath)

channel := make(chan string)

var wg sync.WaitGroup

for key, v := range response.Data {
if v.Dhd != "" {
wg.Add(1)
go downloadImages(key, v.Dhd, downloadsPath, &wg, channel)
}
}

go func() {
wg.Wait()
close(channel)
fmt.Println("Bye 👋🏽")
}()

for res := range channel {
fmt.Println(res)
}

}