From 9c6778a8c95d9c5235847248e8fac909a8449ced Mon Sep 17 00:00:00 2001 From: Doc Date: Tue, 12 Mar 2024 08:56:20 +0100 Subject: [PATCH] Init --- go.mod | 3 ++ main.go | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 go.mod create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3d4b121 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module scrapychan + +go 1.21.4 diff --git a/main.go b/main.go new file mode 100644 index 0000000..cdc311b --- /dev/null +++ b/main.go @@ -0,0 +1,112 @@ +package main + +import ( + "encoding/json" + "flag" + "io" + "log" + "net/http" + "os" + "strings" + "strconv" +) + +func main () { + // Setting up command flags + wdpath, _ := os.Getwd(); + + url := flag.String("u", "", "The url of the 4chan thread") + + dest := flag.String("o", wdpath, "Target dir of the conntent") + + verlog := flag.Bool("v", false, "Set logging to verbose") + + flag.Parse() + + // Getting the boardname + board := strings.Split(*url, "/")[3] + + // Get thread info from API + res, err := http.Get(*url + ".json") + if err != nil { + log.Fatal(err) + } else { + log.Println("Got thread data") + } + + // Check if API response is valid + if res.StatusCode > 299 { + log.Fatalf("Response failed with status code: %d and\n", res.StatusCode) + } else if (*verlog) { + log.Println("API response was ok") + } + + //Geting the data from the response + resbody, err := io.ReadAll(res.Body) + res.Body.Close() + if err != nil { + log.Fatal(err) + } else if (*verlog) { + log.Println("Got body of API response") + } + + // Var to save the JSON data + var jdata map[string]interface{} + + //Unmarshaling the API JSON respons + if err := json.Unmarshal(resbody, &jdata); err != nil { + log.Fatalln(err) + } else if (*verlog) { + log.Println("Unmarsheled API responsebody") + } + + // Iterating the posts from JSON data + for _, v := range jdata["posts"].([]interface{}) { + post := v.(map[string]interface{}) + + // Check if post contains media (Video or Image) + if post["ext"] != nil { + + + cdnurlstr := "https://i.4cdn.org/" + board + "/" + strconv.Itoa(int(post["tim"].(float64))) + post["ext"].(string) + + // Requesting the media from CDN + cdnres, err := http.Get(cdnurlstr) + if err != nil { + log.Fatal(err) + } + + // Check if respons was valid + if res.StatusCode > 299 { + log.Fatalf("Response failed with status code: %d and\n", res.StatusCode) + } else if (*verlog) { + log.Println("Got image/video " + strconv.Itoa(int(post["tim"].(float64))) + post["ext"].(string) + " data") + } + + // Read data form respons + cdnresbody, err := io.ReadAll(cdnres.Body) + cdnres.Body.Close() + if err != nil { + log.Fatal(err) + } else if (*verlog) { + log.Println("Successfully got data from responds body") + } + + + // Save the mediadata to file + err = os.WriteFile(*dest + "/" + board + "-" + strconv.Itoa(int(post["tim"].(float64))) + post["ext"].(string), cdnresbody, 0664 ) + if err != nil { + log.Fatal(err) + } else if (*verlog) { + log.Println("Successfully wrote image/video data to disk") + } + + } else if (*verlog) { + log.Println("Post " + strconv.Itoa(int(post["no"].(float64))) + " didn't include a image or video") + } + + } + + log.Println("DONE!!!") + +}