Compare commits

...

8 Commits

Author SHA1 Message Date
daru
17a754b918 Anime lists did not age well 😵‍💫 2022-08-25 23:32:31 +02:00
daru
730f91e74e Anime lists didn't age well 😵‍💫 2022-08-25 23:31:18 +02:00
daru
dbd34abea1 Anime list fetching not broken but ugly insted ✔️ 2022-08-25 22:54:21 +02:00
daru
beed95eed9 LET THERE BE ORDER 2022-08-19 18:05:54 +02:00
daru
074a4a88f0 go 1.19 2022-08-03 19:09:33 +02:00
daru
fa765859e4 ☮️ 2022-07-31 19:00:48 +02:00
daru
1f59f13bfc Get your codebase togehter 2022-07-31 14:55:31 +02:00
daru
92660df667 💣🩹 2022-07-31 14:50:56 +02:00
8 changed files with 81 additions and 35 deletions

12
go.mod
View File

@@ -1,17 +1,17 @@
module github.com/ultrasn0w/huso
go 1.18
go 1.19
require (
github.com/allegro/bigcache/v3 v3.0.2
github.com/bwmarrin/discordgo v0.25.0
github.com/bwmarrin/discordgo v0.26.0
github.com/fasthttp/router v1.4.11
github.com/go-sql-driver/mysql v1.6.0
github.com/gookit/color v1.5.1
github.com/klauspost/compress v1.15.9
github.com/valyala/fasthttp v1.38.0
github.com/valyala/fasthttp v1.39.0
github.com/valyala/quicktemplate v1.7.0
github.com/xujiajun/nutsdb v0.9.0
github.com/xujiajun/nutsdb v0.10.0
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9
)
@@ -25,6 +25,6 @@ require (
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
github.com/xujiajun/mmap-go v1.0.1 // indirect
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 // indirect
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
)

21
go.sum
View File

@@ -4,8 +4,8 @@ github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/bwmarrin/discordgo v0.25.0 h1:NXhdfHRNxtwso6FPdzW2i3uBvvU7UIQTghmV2T4nqAs=
github.com/bwmarrin/discordgo v0.25.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/bwmarrin/discordgo v0.26.0 h1:/AdFmxHXSHInYAZ7K0O3VEIXlVjGpztk/nuCr9o+JCs=
github.com/bwmarrin/discordgo v0.26.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
@@ -40,8 +40,9 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.30.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus=
github.com/valyala/fasthttp v1.38.0 h1:yTjSSNjuDi2PPvXY2836bIwLmiTS2T4T9p1coQshpco=
github.com/valyala/fasthttp v1.38.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
github.com/valyala/fasthttp v1.39.0 h1:lW8mGeM7yydOqZKmwyMTaz/PH/A+CLgtmmcjv+OORfU=
github.com/valyala/fasthttp v1.39.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
github.com/valyala/quicktemplate v1.7.0 h1:LUPTJmlVcb46OOUY3IeD9DojFpAVbsG+5WFTcjMJzCM=
github.com/valyala/quicktemplate v1.7.0/go.mod h1:sqKJnoaOF88V07vkO+9FL8fb9uZg/VPSJnLYn+LmLk8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
@@ -50,15 +51,15 @@ github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1z
github.com/xujiajun/gorouter v1.2.0/go.mod h1:yJrIta+bTNpBM/2UT8hLOaEAFckO+m/qmR3luMIQygM=
github.com/xujiajun/mmap-go v1.0.1 h1:7Se7ss1fLPPRW+ePgqGpCkfGIZzJV6JPq9Wq9iv/WHc=
github.com/xujiajun/mmap-go v1.0.1/go.mod h1:CNN6Sw4SL69Sui00p0zEzcZKbt+5HtEnYUsc6BKKRMg=
github.com/xujiajun/nutsdb v0.9.0 h1:vy8rjDp0Sk/SnTAqg61i+G4NIN/3tBKSdZ6rIyKYVIo=
github.com/xujiajun/nutsdb v0.9.0/go.mod h1:8ZdTTF0cEQO+wN940htfHYKswFql2iB6Osckx+GmOoU=
github.com/xujiajun/nutsdb v0.10.0 h1:kSxd7MyZiAVQM2I79FK74WneGI+uaHsUdak8dbjzKJc=
github.com/xujiajun/nutsdb v0.10.0/go.mod h1:8ZdTTF0cEQO+wN940htfHYKswFql2iB6Osckx+GmOoU=
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b h1:jKG9OiL4T4xQN3IUrhUpc1tG+HfDXppkgVcrAiiaI/0=
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b/go.mod h1:AZd87GYJlUzl82Yab2kTjx1EyXSQCAfZDhpTo1SQC4k=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 h1:vJ2V3lFLg+bBhgroYuRfyN583UzVveQmIXjc8T/y3to=
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
@@ -72,10 +73,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220405210540-1e041c57c461/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8 h1:dyU22nBWzrmTQxtNrr4dzVOvaw35nUYE279vF9UmsI8=
golang.org/x/sys v0.0.0-20220727055044-e65921a090b8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 h1:TyKJRhyo17yWxOMCTHKWrc5rddHORMlnZ/j57umaUd8=
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

11
huso.go
View File

@@ -20,7 +20,7 @@ import (
)
const (
husoVersion = "1.6"
husoVersion = "1.7.1"
registerSecret = "綾波レイ"
seasonStrJikan = "seasons/"
userApiJikan = "users/"
@@ -67,6 +67,7 @@ var (
logOut *RingBuf
buildTime string
buildCommit string
lastAnnounce *SomewhatThreadSafeTime
)
func main() {
@@ -76,7 +77,7 @@ func main() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
logOut = NewRingBuf(10101)
logOut = NewRingBuf(101010)
color.Notice.Printf("huso %s built on %s with %s\n", husoVersion, buildTime, runtime.Version())
color.Notice.Printf("Last git commit: %s\n", buildCommit)
@@ -107,7 +108,7 @@ func main() {
log.Fatal(err)
}
defer searchCache.Close()
animeListCache, err = bigcache.NewBigCache(bigcache.DefaultConfig(42 * time.Minute))
animeListCache, err = bigcache.NewBigCache(bigcache.DefaultConfig(15 * time.Minute))
if err != nil {
log.Fatal(err)
}
@@ -126,6 +127,10 @@ func main() {
}
defer db.Close()
lastAnnounce = &SomewhatThreadSafeTime{
stamp: time.Now().Add(-1 * time.Hour),
}
color.Infoln("NutsDB connected")
logOut.WriteLine("🗃️ NutsDB connected")

View File

@@ -1,6 +1,9 @@
package main
import "time"
import (
"sync"
"time"
)
type AnimeUser struct {
Anime int64 `json:"anime"`
@@ -55,9 +58,10 @@ type AnimeStudio struct {
}
type Appointment struct {
Anime int64 `json:"anime"`
Time time.Time `json:"date"`
Users []string `json:"users"`
Anime int64 `json:"anime"`
Time time.Time `json:"date"`
Users []string `json:"users"`
Announced bool `json:"announced"`
}
type WatchUser struct {
@@ -327,3 +331,8 @@ type MovieChart struct {
AvgScore float64 `json:"avgScore"`
UserCount int `json:"userCount"`
}
type SomewhatThreadSafeTime struct {
sync.Mutex
stamp time.Time
}

View File

@@ -432,9 +432,10 @@ func ReadAppointments() ([]Appointment, error) {
return err
}
appointment := Appointment{
Anime: animeId,
Time: date,
Users: appointmentUsers,
Anime: animeId,
Time: date,
Users: appointmentUsers,
Announced: false,
}
appoints = append(appoints, appointment)
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"sort"
"strconv"
"strings"
"time"
@@ -643,6 +644,12 @@ func watchGetLogic(ctx *fasthttp.RequestCtx) ([]AnimeUser, error) {
}
animesUsers = filteredAnimes
}
sort.Slice(animesUsers, func(i, j int) bool { return animesUsers[i].Anime < animesUsers[j].Anime })
sort.SliceStable(animesUsers, func(i, j int) bool {
return AverageEpPerUser(animesUsers[i].Users) > AverageEpPerUser(animesUsers[j].Users)
})
return animesUsers, err
}

View File

@@ -12,7 +12,7 @@ import (
)
func Arbeiten() {
for range time.Tick(20 * time.Minute) {
for range time.Tick(10 * time.Minute) {
Arbeit()
}
}
@@ -27,8 +27,11 @@ func Arbeit() {
}
} else {
cleared := 0
lastAnnounce.Lock()
defer lastAnnounce.Unlock()
newCheckDate := time.Now()
for _, a := range appoints {
if a.Time.Add(22 * time.Hour).Before(time.Now()) {
if a.Time.Add(22 * time.Hour).Before(newCheckDate) {
// appointment expired
keyBytes := Int64AndDateToBytes(a.Anime, a.Time)
err = DbDelete(bucketAppoint, string(keyBytes))
@@ -38,11 +41,14 @@ func Arbeit() {
} else {
cleared++
}
} else if time.Now().Add(40*time.Minute).Before(a.Time) && time.Now().Add(60*time.Minute).After(a.Time) {
// This has not happened and is happening soon
go AnnounceBomb(a.Anime, a.Time.Unix(), time.Until(a.Time.Add(-25*time.Minute)))
} else {
if lastAnnounce.stamp.Add(time.Hour).Before(a.Time) && newCheckDate.Add(time.Hour).After(a.Time) {
// This has not happened and is happening soon
go AnnounceBomb(a.Anime, a.Time.Unix(), time.Until(a.Time.Add(-25*time.Minute)))
}
}
}
lastAnnounce.stamp = newCheckDate
if cleared > 0 {
color.Infof("Cleared %d expired appointments\n", cleared)
logOut.WriteLine(fmt.Sprintf("♻️ Cleared %d expired appointments", cleared))
@@ -58,8 +64,13 @@ func Arbeit() {
}
return
}
// ACTUALLY STUPID ENOUGH TO FIX A LOT OF PROBLEMS
animeListCache.Reset()
color.Infoln("Animelisten abfragen...")
logOut.WriteLine("📜 Animelisten abfragen...")
//logOut.WriteLine("📜 Animelisten abfragen...")
count := 0
// iterate anime
for _, a := range animesUsers {
@@ -111,8 +122,11 @@ func Arbeit() {
count++
}
}
color.Infof("%d Sachen aktualisiert\n", count)
logOut.WriteLine(fmt.Sprintf("📜 %d Sachen aktualisiert", count))
if count > 0 {
color.Infof("%d Sachen aktualisiert\n", count)
logOut.WriteLine(fmt.Sprintf("📜 %d Sachen aktualisiert", count))
}
}
func BissleArbeiten() {

View File

@@ -76,3 +76,14 @@ func DecompressZstd(src []byte) ([]byte, error) {
_, err = io.Copy(&buf, decoder)
return buf.Bytes(), err
}
func AverageEpPerUser(users []WatchUser) float64 {
if len(users) == 0 {
return 0.0
}
res := 0.0
for _, u := range users {
res += float64(u.Progress)
}
return res / float64(len(users))
}