mirror of
https://github.com/skidoodle/ncore-stats.git
synced 2025-02-15 05:09:14 +01:00
refactor: profiles are now stored in json, added displaynames
This commit is contained in:
parent
8a994c3bb8
commit
6ffa3de9bb
4 changed files with 42 additions and 27 deletions
1
go.mod
1
go.mod
|
@ -8,5 +8,6 @@ require github.com/PuerkitoBio/goquery v1.10.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/cascadia v1.3.2 // indirect
|
github.com/andybalholm/cascadia v1.3.2 // indirect
|
||||||
|
github.com/joho/godotenv v1.5.1 // indirect
|
||||||
golang.org/x/net v0.30.0 // indirect
|
golang.org/x/net v0.30.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
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/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 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
|
||||||
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
|
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=
|
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-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
|
63
main.go
63
main.go
|
@ -7,45 +7,56 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProfileData struct {
|
type ProfileData struct {
|
||||||
Owner string `json:"owner"`
|
Owner string `json:"owner"`
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
Rank string `json:"rank"`
|
Rank string `json:"rank"`
|
||||||
Upload string `json:"upload"`
|
Upload string `json:"upload"`
|
||||||
CurrentUpload string `json:"current_upload"`
|
CurrentUpload string `json:"current_upload"`
|
||||||
CurrentDownload string `json:"current_download"`
|
CurrentDownload string `json:"current_download"`
|
||||||
Points string `json:"points"`
|
Points string `json:"points"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
profiles = map[string]string{}
|
profiles = map[string]string{}
|
||||||
jsonFile = "data.json"
|
jsonFile = "data.json"
|
||||||
|
baseUrl = "https://ncore.pro/profile.php?id="
|
||||||
nick string
|
nick string
|
||||||
pass string
|
pass string
|
||||||
client *http.Client
|
client *http.Client
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
godotenv.Load()
|
||||||
|
|
||||||
nick = os.Getenv("NICK")
|
nick = os.Getenv("NICK")
|
||||||
pass = os.Getenv("PASS")
|
pass = os.Getenv("PASS")
|
||||||
|
|
||||||
for i := 1; i <= 10; i++ {
|
file, err := os.Open("profiles.json")
|
||||||
profileKey := fmt.Sprintf("PROFILE_%d", i)
|
if err != nil {
|
||||||
if profileURL := os.Getenv(profileKey); profileURL != "" {
|
log.Fatal(err)
|
||||||
profiles[profileURL] = profileURL
|
}
|
||||||
}
|
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{}
|
client = &http.Client{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchProfile(url string) (*ProfileData, error) {
|
func fetchProfile(url string, displayName string) (*ProfileData, error) {
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -57,10 +68,11 @@ func fetchProfile(url string) (*ProfileData, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
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)
|
doc, err := goquery.NewDocumentFromReader(resp.Body)
|
||||||
|
@ -68,11 +80,8 @@ func fetchProfile(url string) (*ProfileData, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
owner := strings.TrimSpace(doc.Find(".fobox_fej").Text())
|
|
||||||
owner = strings.Replace(owner, " profilja", "", 1)
|
|
||||||
|
|
||||||
profile := &ProfileData{
|
profile := &ProfileData{
|
||||||
Owner: owner,
|
Owner: displayName,
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,10 +117,10 @@ func readExistingProfiles() ([]ProfileData, error) {
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
var profiles []ProfileData
|
var existingProfiles []ProfileData
|
||||||
byteValue, _ := io.ReadAll(file)
|
byteValue, _ := io.ReadAll(file)
|
||||||
err = json.Unmarshal(byteValue, &profiles)
|
err = json.Unmarshal(byteValue, &existingProfiles)
|
||||||
return profiles, err
|
return existingProfiles, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func logToJSON(profile *ProfileData) error {
|
func logToJSON(profile *ProfileData) error {
|
||||||
|
@ -134,14 +143,14 @@ func logToJSON(profile *ProfileData) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func dataHandler(w http.ResponseWriter, r *http.Request) {
|
func dataHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
profiles, err := readExistingProfiles()
|
existingProfiles, err := readExistingProfiles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Could not read data", http.StatusInternalServerError)
|
http.Error(w, "Could not read data", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
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) {
|
func serveHTML(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -149,13 +158,13 @@ func serveHTML(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ticker := time.NewTicker(24 * time.Hour)
|
ticker := time.NewTicker(time.Minute * 30)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
for _, url := range profiles {
|
for displayName, url := range profiles {
|
||||||
profile, err := fetchProfile(url)
|
profile, err := fetchProfile(url, displayName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
continue
|
continue
|
||||||
|
|
3
profiles.json
Normal file
3
profiles.json
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"displayName": "userId"
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue