Add first working prototype
This commit is contained in:
parent
3e1c801069
commit
f374d84d0d
5 changed files with 133 additions and 0 deletions
4
.env.example
Normal file
4
.env.example
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
PIPED_USERNAME="johndoe"
|
||||||
|
PIPED_PASSWORD="supersecurepass"
|
||||||
|
PIPED_API_URL="https://pipedapi.kavin.rocks"
|
||||||
|
QUERY_WAIT_TIME="10000"
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@
|
||||||
/result
|
/result
|
||||||
/result-lib
|
/result-lib
|
||||||
.direnv
|
.direnv
|
||||||
|
.env
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,3 +1,5 @@
|
||||||
module git.ghoscht.com/ghoscht/piped-refresher
|
module git.ghoscht.com/ghoscht/piped-refresher
|
||||||
|
|
||||||
go 1.22.10
|
go 1.22.10
|
||||||
|
|
||||||
|
require github.com/joho/godotenv v1.5.1 // indirect
|
||||||
|
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -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=
|
124
main.go
Normal file
124
main.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue