From 6ffa3de9bb41df4fa21bba34c633466b298a4e09 Mon Sep 17 00:00:00 2001 From: hasitotabla Date: Thu, 10 Oct 2024 16:01:08 +0200 Subject: [PATCH] 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" +}