piped-refresher/main.go

176 lines
4.1 KiB
Go
Raw Normal View History

2024-12-28 18:58:20 +01:00
package main
import (
"bytes"
"encoding/json"
"fmt"
2024-12-28 22:01:45 +01:00
"github.com/go-co-op/gocron/v2"
2024-12-28 18:58:20 +01:00
"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)
}
2024-12-28 21:01:37 +01:00
// Randomize subscription order
func randomize[S any](slice []S) {
for i := range slice {
j := rand.IntN(i + 1)
slice[i], slice[j] = slice[j], slice[i]
}
}
2024-12-28 22:01:45 +01:00
// Update Piped videos for all subscriptions given the Piped instance and the matching login data
func update(basePath string, maxWaitTime int, username string, password string) {
authResult := login(basePath, username, password)
subscriptions := getSubscriptions(basePath, authResult.Token)
randomize(subscriptions)
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)
}
}
2024-12-28 18:58:20 +01:00
func main() {
err := godotenv.Load()
if err != nil {
fmt.Println("Couldn't find a .env file, assuming already present environment variables")
}
2024-12-31 14:14:22 +01:00
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
2024-12-28 22:01:45 +01:00
var cronSchedule string = os.Getenv("QUERY_CRON") // cron schedule how often update should be triggered
2024-12-28 18:58:20 +01:00
maxWaitTime, err := strconv.Atoi(os.Getenv("QUERY_WAIT_TIME")) // in milliseconds, change for potential ban evasion
if err != nil {
panic(err)
}
2024-12-28 22:01:45 +01:00
fmt.Printf("Logging in as %s on Piped instance %s\n", username, basePath)
2024-12-28 18:58:20 +01:00
2024-12-28 22:01:45 +01:00
var infinityDuration time.Duration = 42069 * time.Hour // basically infinity
2024-12-28 18:58:20 +01:00
2024-12-28 22:01:45 +01:00
// create a scheduler
s, err := gocron.NewScheduler(gocron.WithStopTimeout(infinityDuration)) // we don't want the lengthy update task to time out
if err != nil {
panic(err)
}
2024-12-28 22:01:45 +01:00
// add a job to the scheduler
_, err = s.NewJob(
gocron.CronJob(
cronSchedule,
false,
),
gocron.NewTask(
func() {
update(basePath, maxWaitTime, username, password)
},
),
gocron.WithSingletonMode(gocron.LimitModeReschedule),
)
if err != nil {
2024-12-31 14:14:22 +01:00
fmt.Println("Error creating task")
2024-12-28 22:01:45 +01:00
panic(err)
}
2024-12-28 18:58:20 +01:00
2024-12-28 22:01:45 +01:00
// start the scheduler
s.Start()
fmt.Print("Starting scheduler...\n\n")
2024-12-31 14:14:22 +01:00
// sleep for infinity and respond to scheduled tasks
for {
time.Sleep(infinityDuration)
2024-12-28 18:58:20 +01:00
}
}