mirror of
https://github.com/skidoodle/ncore-stats.git
synced 2025-02-15 05:09:14 +01:00
Merge pull request #1 from hasitotabla/main
refactor: profiles are now stored in json, added displaynames
This commit is contained in:
commit
3498545d5c
9 changed files with 62 additions and 31 deletions
|
@ -1,3 +1,2 @@
|
|||
NICK=
|
||||
PASS=
|
||||
PROFILE_1=https://ncore.pro/profile.php?id=1577943
|
||||
|
|
|
@ -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"]
|
||||
|
|
0
data/.gitkeep
Normal file
0
data/.gitkeep
Normal file
12
docker-compose.dev.yaml
Normal file
12
docker-compose.dev.yaml
Normal file
|
@ -0,0 +1,12 @@
|
|||
services:
|
||||
trackncore:
|
||||
build: .
|
||||
container_name: trackncore
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "3000:3000"
|
||||
volumes:
|
||||
- data:/app/data
|
||||
|
||||
volumes:
|
||||
data:
|
|
@ -6,7 +6,7 @@ services:
|
|||
ports:
|
||||
- "3000:3000"
|
||||
volumes:
|
||||
- data:/app
|
||||
- data:/app/data
|
||||
|
||||
volumes:
|
||||
data:
|
||||
|
|
5
go.mod
5
go.mod
|
@ -4,7 +4,10 @@ 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
|
||||
|
|
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/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=
|
||||
|
|
50
main.go
50
main.go
|
@ -7,10 +7,10 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type ProfileData struct {
|
||||
|
@ -25,27 +25,39 @@ 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
|
||||
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(profilesFile)
|
||||
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 +69,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 +81,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 +118,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 +144,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 +159,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
|
||||
|
|
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