From 6ffa3de9bb41df4fa21bba34c633466b298a4e09 Mon Sep 17 00:00:00 2001 From: hasitotabla Date: Thu, 10 Oct 2024 16:01:08 +0200 Subject: [PATCH 1/4] refactor: profiles are now stored in json, added displaynames --- go.mod | 1 + go.sum | 2 ++ main.go | 63 +++++++++++++++++++++++++++++---------------------- profiles.json | 3 +++ 4 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 profiles.json diff --git a/go.mod b/go.mod index 0eadf15..873d4c4 100644 --- a/go.mod +++ b/go.mod @@ -8,5 +8,6 @@ require github.com/PuerkitoBio/goquery v1.10.0 require ( github.com/andybalholm/cascadia v1.3.2 // indirect + github.com/joho/godotenv v1.5.1 // indirect golang.org/x/net v0.30.0 // indirect ) diff --git a/go.sum b/go.sum index e25fd97..367fe40 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/PuerkitoBio/goquery v1.10.0 h1:6fiXdLuUvYs2OJSvNRqlNPoBm6YABE226xrbav github.com/PuerkitoBio/goquery v1.10.0/go.mod h1:TjZZl68Q3eGHNBA8CWaxAN7rOU1EbDz3CWuolcO5Yu4= github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= diff --git a/main.go b/main.go index 7d0b9f7..55c4397 100644 --- a/main.go +++ b/main.go @@ -7,45 +7,56 @@ import ( "log" "net/http" "os" - "strings" "time" "github.com/PuerkitoBio/goquery" + "github.com/joho/godotenv" ) type ProfileData struct { - Owner string `json:"owner"` - Timestamp time.Time `json:"timestamp"` - Rank string `json:"rank"` - Upload string `json:"upload"` - CurrentUpload string `json:"current_upload"` - CurrentDownload string `json:"current_download"` - Points string `json:"points"` + Owner string `json:"owner"` + Timestamp time.Time `json:"timestamp"` + Rank string `json:"rank"` + Upload string `json:"upload"` + CurrentUpload string `json:"current_upload"` + CurrentDownload string `json:"current_download"` + Points string `json:"points"` } var ( profiles = map[string]string{} jsonFile = "data.json" + baseUrl = "https://ncore.pro/profile.php?id=" nick string pass string client *http.Client ) func init() { + godotenv.Load() + nick = os.Getenv("NICK") pass = os.Getenv("PASS") - for i := 1; i <= 10; i++ { - profileKey := fmt.Sprintf("PROFILE_%d", i) - if profileURL := os.Getenv(profileKey); profileURL != "" { - profiles[profileURL] = profileURL - } + file, err := os.Open("profiles.json") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + jsonBytes, _ := io.ReadAll(file) + + var tempProfiles map[string]string = make(map[string]string) + json.Unmarshal(jsonBytes, &tempProfiles) + + for k, v := range tempProfiles { + profiles[k] = baseUrl + v } client = &http.Client{} } -func fetchProfile(url string) (*ProfileData, error) { +func fetchProfile(url string, displayName string) (*ProfileData, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err @@ -57,10 +68,11 @@ func fetchProfile(url string) (*ProfileData, error) { if err != nil { return nil, err } + defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("failed to fetch profile: %s", url) + return nil, fmt.Errorf("failed to fetch profile: %s", displayName) } doc, err := goquery.NewDocumentFromReader(resp.Body) @@ -68,11 +80,8 @@ func fetchProfile(url string) (*ProfileData, error) { return nil, err } - owner := strings.TrimSpace(doc.Find(".fobox_fej").Text()) - owner = strings.Replace(owner, " profilja", "", 1) - profile := &ProfileData{ - Owner: owner, + Owner: displayName, Timestamp: time.Now(), } @@ -108,10 +117,10 @@ func readExistingProfiles() ([]ProfileData, error) { } defer file.Close() - var profiles []ProfileData + var existingProfiles []ProfileData byteValue, _ := io.ReadAll(file) - err = json.Unmarshal(byteValue, &profiles) - return profiles, err + err = json.Unmarshal(byteValue, &existingProfiles) + return existingProfiles, err } func logToJSON(profile *ProfileData) error { @@ -134,14 +143,14 @@ func logToJSON(profile *ProfileData) error { } func dataHandler(w http.ResponseWriter, r *http.Request) { - profiles, err := readExistingProfiles() + existingProfiles, err := readExistingProfiles() if err != nil { http.Error(w, "Could not read data", http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(profiles) + json.NewEncoder(w).Encode(existingProfiles) } func serveHTML(w http.ResponseWriter, r *http.Request) { @@ -149,13 +158,13 @@ func serveHTML(w http.ResponseWriter, r *http.Request) { } func main() { - ticker := time.NewTicker(24 * time.Hour) + ticker := time.NewTicker(time.Minute * 30) defer ticker.Stop() go func() { for { - for _, url := range profiles { - profile, err := fetchProfile(url) + for displayName, url := range profiles { + profile, err := fetchProfile(url, displayName) if err != nil { log.Println(err) continue diff --git a/profiles.json b/profiles.json new file mode 100644 index 0000000..07d39cb --- /dev/null +++ b/profiles.json @@ -0,0 +1,3 @@ +{ + "displayName": "userId" +} From f1eb603813da1f33cffeb2dd8e05ae70558b6ac7 Mon Sep 17 00:00:00 2001 From: hasitotabla Date: Thu, 10 Oct 2024 16:05:17 +0200 Subject: [PATCH 2/4] chore: removed unused clusterfuckery env variable --- .env.example | 1 - 1 file changed, 1 deletion(-) diff --git a/.env.example b/.env.example index 93eeffc..35cc35d 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,2 @@ NICK= PASS= -PROFILE_1=https://ncore.pro/profile.php?id=1577943 From 2aa7b29b23c96dad902ca440b4cbf089e6a23439 Mon Sep 17 00:00:00 2001 From: hasitotabla Date: Thu, 10 Oct 2024 16:52:17 +0200 Subject: [PATCH 3/4] fit: add .env and profiles.json to dockerfile bumta alb --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index 19f7c98..f676ff1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,6 +7,8 @@ FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /app/ COPY --from=builder /build/trackncore . +COPY --from=builder /build/.env . +COPY --from=builder /build/profiles.json . COPY --from=builder /build/index.html . EXPOSE 3000 CMD ["./trackncore"] From 8ea02ee188afd938d00770db0ffff064858ff4d7 Mon Sep 17 00:00:00 2001 From: skidoodle Date: Thu, 10 Oct 2024 17:07:08 +0200 Subject: [PATCH 4/4] bara bra grejjor --- data/.gitkeep | 0 docker-compose.dev.yaml | 12 ++++++++++++ docker-compose.yaml | 2 +- go.mod | 6 ++++-- main.go | 5 +++-- 5 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 data/.gitkeep create mode 100644 docker-compose.dev.yaml diff --git a/data/.gitkeep b/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml new file mode 100644 index 0000000..3a451f9 --- /dev/null +++ b/docker-compose.dev.yaml @@ -0,0 +1,12 @@ +services: + trackncore: + build: . + container_name: trackncore + restart: unless-stopped + ports: + - "3000:3000" + volumes: + - data:/app/data + +volumes: + data: diff --git a/docker-compose.yaml b/docker-compose.yaml index 25bff95..1f6569e 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,7 +6,7 @@ services: ports: - "3000:3000" volumes: - - data:/app + - data:/app/data volumes: data: diff --git a/go.mod b/go.mod index 873d4c4..e3b5421 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,12 @@ go 1.23 toolchain go1.23.2 -require github.com/PuerkitoBio/goquery v1.10.0 +require ( + github.com/PuerkitoBio/goquery v1.10.0 + github.com/joho/godotenv v1.5.1 +) require ( github.com/andybalholm/cascadia v1.3.2 // indirect - github.com/joho/godotenv v1.5.1 // indirect golang.org/x/net v0.30.0 // indirect ) diff --git a/main.go b/main.go index 55c4397..7147755 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,8 @@ type ProfileData struct { var ( profiles = map[string]string{} - jsonFile = "data.json" + jsonFile = "./data/data.json" + profilesFile = "profiles.json" baseUrl = "https://ncore.pro/profile.php?id=" nick string pass string @@ -38,7 +39,7 @@ func init() { nick = os.Getenv("NICK") pass = os.Getenv("PASS") - file, err := os.Open("profiles.json") + file, err := os.Open(profilesFile) if err != nil { log.Fatal(err) }