From f374d84d0d059f8e9eb81d81132876b53c75c411 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sat, 28 Dec 2024 18:58:20 +0100 Subject: [PATCH] Add first working prototype --- .env.example | 4 ++ .gitignore | 1 + go.mod | 2 + go.sum | 2 + main.go | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 .env.example create mode 100644 go.sum create mode 100644 main.go diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..188a44d --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +PIPED_USERNAME="johndoe" +PIPED_PASSWORD="supersecurepass" +PIPED_API_URL="https://pipedapi.kavin.rocks" +QUERY_WAIT_TIME="10000" diff --git a/.gitignore b/.gitignore index e049ed5..c7666b9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /result /result-lib .direnv +.env diff --git a/go.mod b/go.mod index 99d32a5..e34ea69 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.ghoscht.com/ghoscht/piped-refresher go 1.22.10 + +require github.com/joho/godotenv v1.5.1 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d61b19e --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/main.go b/main.go new file mode 100644 index 0000000..fc02136 --- /dev/null +++ b/main.go @@ -0,0 +1,124 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "github.com/joho/godotenv" + "log" + "math/rand/v2" + "net/http" + "os" + "strconv" + "time" +) + +var loginPath string = "/login" +var subscriptionsPath string = "/subscriptions" + +type AuthResult struct { + Token string +} + +type Subscription struct { + Url string + Name string + Avatar string + Verified bool +} + +func login(basePath string, username string, password string) AuthResult { + loginUrl := basePath + loginPath + login := map[string]string{"username": username, "password": password} + + jsonLogin, _ := json.Marshal(login) + + res, err := http.Post(loginUrl, "application/json", bytes.NewBuffer(jsonLogin)) + if err != nil { + panic(err) + } + defer res.Body.Close() + + var authResult AuthResult + authResultDecoder := json.NewDecoder(res.Body) + err = authResultDecoder.Decode(&authResult) + if err != nil { + panic(err) + } + return authResult +} + +func getSubscriptions(basePath string, authToken string) []Subscription { + subscriptionsUrl := basePath + subscriptionsPath + + // Create a new request using http + req, err := http.NewRequest("GET", subscriptionsUrl, nil) + if err != nil { + panic(err) + } + + req.Header.Add("Authorization", authToken) + + client := &http.Client{} + res, err := client.Do(req) + if err != nil { + log.Println("Error on response.\n[ERROR] -", err) + } + defer res.Body.Close() + + var subsResult []Subscription + subsResultDecoder := json.NewDecoder(res.Body) + err = subsResultDecoder.Decode(&subsResult) + if err != nil { + panic(err) + } + return subsResult +} + +func visit(url string, authToken string) { + req, err := http.NewRequest("GET", url, nil) + if err != nil { + panic(err) + } + + req.Header.Add("Authorization", authToken) + + client := &http.Client{} + res, err := client.Do(req) + if err != nil { + log.Println("Error on response.\n[ERROR] -", err) + } + defer res.Body.Close() + fmt.Println(res.Status) +} + +func main() { + err := godotenv.Load() + + if err != nil { + fmt.Println("Couldn't find a .env file, assuming already present environment variables") + } + + var username string = os.Getenv("PIPED_USERNAME") // piped username + var password string = os.Getenv("PIPED_PASSWORD") // piped password + var basePath string = os.Getenv("PIPED_API_URL") // piped API URL + maxWaitTime, err := strconv.Atoi(os.Getenv("QUERY_WAIT_TIME")) // in milliseconds, change for potential ban evasion + if err != nil { + panic(err) + } + + fmt.Printf("Logged in as %s on Piped instance %s\n\n", username, basePath) + + authResult := login(basePath, username, password) + subscriptions := getSubscriptions(basePath, authResult.Token) + + for i := range subscriptions { + fmt.Println(subscriptions[i].Name) + visit(basePath+subscriptions[i].Url, authResult.Token) + + randomWaitTime := time.Duration(rand.IntN(maxWaitTime)) * time.Millisecond + fmt.Println("Waiting for", randomWaitTime) + fmt.Println("") + time.Sleep(randomWaitTime) + } +}