Implement reading ids from pocketbase
This commit is contained in:
81
main.go
81
main.go
@@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -23,11 +25,22 @@ const (
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger.Init(logger.LevelInfo)
|
||||
|
||||
envfile := flag.String("envfile", ".env", "")
|
||||
inputfile := flag.String("if", "", "")
|
||||
pocketbase := flag.Bool("pb", false, "")
|
||||
flag.Parse()
|
||||
logger.InitFlag()
|
||||
|
||||
pocketbaseids := make([]string, 0)
|
||||
if *pocketbase {
|
||||
ids, err := FromPocketbase(pocketbaseUrl, "445220")
|
||||
if err != nil {
|
||||
logger.Error("Error getting workshop ids: %v", err)
|
||||
return
|
||||
}
|
||||
logger.Info("Got %d workshop ids", len(ids))
|
||||
pocketbaseids = append(pocketbaseids, ids...)
|
||||
}
|
||||
|
||||
args := flag.Args()
|
||||
if *inputfile != "" {
|
||||
@@ -46,8 +59,8 @@ func main() {
|
||||
args = append(args, sargs...)
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
logger.Error("No args specified, please pass space delimited list of workshop ids for downloading")
|
||||
if len(args) == 0 && len(pocketbaseids) == 0 {
|
||||
logger.Error("No args specified, please pass space delimited list of workshop ids for downloading or use -pb to get ids from pocketbase")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -107,6 +120,9 @@ func main() {
|
||||
for _, arg := range args {
|
||||
scriptContents = append(scriptContents, fmt.Sprintf("workshop_download_item %s %s", app, arg))
|
||||
}
|
||||
for _, id := range pocketbaseids {
|
||||
scriptContents = append(scriptContents, fmt.Sprintf("workshop_download_item %s %s", app, id))
|
||||
}
|
||||
scriptContents = append(scriptContents, "quit")
|
||||
|
||||
scriptFileHandle, err := os.OpenFile(steamcmdScriptPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0755)
|
||||
@@ -157,3 +173,60 @@ func loadEnv(fpath string) (map[string]string, error) {
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
type PocketbaseResponse struct {
|
||||
Page int64 `json:"page"`
|
||||
PerPage int64 `json:"perPage"`
|
||||
TotalPages int64 `json:"totalPages"`
|
||||
TotalItems int64 `json:"totalItems"`
|
||||
Items []WorkshopItem `json:"items"`
|
||||
}
|
||||
|
||||
type WorkshopItem struct {
|
||||
CollectionID string `json:"collectionId"`
|
||||
CollectionName string `json:"collectionName"`
|
||||
ID string `json:"id"`
|
||||
Gameid string `json:"gameid"`
|
||||
Workshopid string `json:"workshopid"`
|
||||
Title string `json:"title"`
|
||||
Link string `json:"link"`
|
||||
LastUpdated string `json:"lastUpdated"`
|
||||
Subscribed string `json:"subscribed"`
|
||||
Created string `json:"created"`
|
||||
Updated string `json:"updated"`
|
||||
}
|
||||
|
||||
var pocketbaseUrl = "https://pocketbase-scratch.site.quack-lab.dev/api/collections/steam_workshop/records"
|
||||
|
||||
func FromPocketbase(url string, appId string) ([]string, error) {
|
||||
url = fmt.Sprintf("%s?filter=(gameid='%s')&fields=workshopid&perPage=1000", pocketbaseUrl, appId)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var res PocketbaseResponse
|
||||
err = json.Unmarshal(body, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Deduplicate
|
||||
ids := make(map[string]struct{})
|
||||
for _, entry := range res.Items {
|
||||
ids[entry.Workshopid] = struct{}{}
|
||||
}
|
||||
idsarr := make([]string, 0, len(ids))
|
||||
for id := range ids {
|
||||
idsarr = append(idsarr, id)
|
||||
}
|
||||
|
||||
return idsarr, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user