piped-refresher/main.go

125 lines
2.8 KiB
Go
Raw Normal View History

2024-12-28 18:58:20 +01:00
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)
}
}