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 (
|
||||
github.com/andybalholm/cascadia v1.3.2 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // 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/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=
|
||||
|
|
47
main.go
47
main.go
|
@ -7,10 +7,10 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type ProfileData struct {
|
||||
|
@ -26,26 +26,37 @@ type ProfileData struct {
|
|||
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
|
||||
|
|
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