mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-13 10:29:52 +01:00
Compare commits
4 Commits
bbf99fadb4
...
22179e38c8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
22179e38c8 | ||
|
|
0c2fb5c546 | ||
|
|
f0555e8d6a | ||
|
|
50e0defc28 |
2
go.mod
2
go.mod
@@ -21,5 +21,5 @@ 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/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
|
||||
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
|
||||
)
|
||||
|
||||
6
go.sum
6
go.sum
@@ -18,8 +18,6 @@ github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl
|
||||
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
|
||||
github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/klauspost/compress v1.15.3 h1:wmfu2iqj9q22SyMINp1uQ8C2/V4M1phJdmH9fG4nba0=
|
||||
github.com/klauspost/compress v1.15.3/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
||||
github.com/klauspost/compress v1.15.4 h1:1kn4/7MepF/CHmYub99/nNX8az0IJjfSOU/jbnTVfqQ=
|
||||
github.com/klauspost/compress v1.15.4/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@@ -60,8 +58,8 @@ golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
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-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60=
|
||||
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a h1:N2T1jUrTQE9Re6TFF5PhvEHXHCguynGhKjWVsIUt5cY=
|
||||
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a/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=
|
||||
|
||||
6
huso.go
6
huso.go
@@ -32,6 +32,8 @@ const (
|
||||
bucketUsers = "users"
|
||||
bucketAnime = "anime"
|
||||
bucketMedia = "media"
|
||||
bucketAppoint = "appoint"
|
||||
AppointSplit = "§"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -57,10 +59,10 @@ func main() {
|
||||
sc := make(chan os.Signal, 1)
|
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||
|
||||
logOut = NewRingBuf(1000)
|
||||
logOut = NewRingBuf(10101)
|
||||
|
||||
color.Notice.Printf("huso %s %s\n", husoVersion, runtime.Version())
|
||||
logOut.WriteLine(fmt.Sprintf("huso %s %s", husoVersion, runtime.Version()))
|
||||
logOut.WriteLine(fmt.Sprintf("🎉 huso %s %s", husoVersion, runtime.Version()))
|
||||
|
||||
jikanLimiter = rate.NewLimiter(rate.Every(time.Second), 1)
|
||||
|
||||
|
||||
13
klotz.go
13
klotz.go
@@ -8,9 +8,10 @@ type AnimeUser struct {
|
||||
}
|
||||
|
||||
type AnimeUserExtended struct {
|
||||
Anime int64 `json:"anime"`
|
||||
Users []WatchUser `json:"users"`
|
||||
Data Anime `json:"data"`
|
||||
Anime int64 `json:"anime"`
|
||||
Users []WatchUser `json:"users"`
|
||||
Data Anime `json:"data"`
|
||||
Appointments []Appointment `json:"appointments"`
|
||||
}
|
||||
|
||||
type Anime struct {
|
||||
@@ -52,6 +53,12 @@ type AnimeStudio struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Appointment struct {
|
||||
Anime int64 `json:"anime"`
|
||||
Time time.Time `json:"date"`
|
||||
Users []string `json:"users"`
|
||||
}
|
||||
|
||||
type WatchUser struct {
|
||||
Username string `json:"username"`
|
||||
MalID int64 `json:"malId"`
|
||||
|
||||
13
knecht.go
13
knecht.go
@@ -118,13 +118,13 @@ func GetUserData(username string) (*User, []byte, error) {
|
||||
|
||||
func GetSeasonDataAll() ([]Anime, []byte, error) {
|
||||
color.Infoln("Aktuelle Season abfragen...")
|
||||
logOut.WriteLine("Aktuelle Season abfragen...")
|
||||
logOut.WriteLine("📺 Aktuelle Season abfragen...")
|
||||
data, _, err := GetSeasonData(1)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
color.Infof("%d Anime auf %d Seiten\n", data.Pagination.Items.Total, data.Pagination.LastVisiblePage)
|
||||
logOut.WriteLine(fmt.Sprintf("%d Anime auf %d Seiten", data.Pagination.Items.Total, data.Pagination.LastVisiblePage))
|
||||
logOut.WriteLine(fmt.Sprintf("📺 %d Anime auf %d Seiten", data.Pagination.Items.Total, data.Pagination.LastVisiblePage))
|
||||
animes := make([]Anime, 0)
|
||||
// convert to anime
|
||||
for _, a := range data.Data {
|
||||
@@ -132,7 +132,7 @@ func GetSeasonDataAll() ([]Anime, []byte, error) {
|
||||
}
|
||||
for i := 2; data.Pagination.HasNextPage; i++ {
|
||||
color.Infof("Seite %d abfragen...\n", i)
|
||||
logOut.WriteLine(fmt.Sprintf("Seite %d abfragen...", i))
|
||||
logOut.WriteLine(fmt.Sprintf("📺 Seite %d abfragen...", i))
|
||||
newData, _, err := GetSeasonData(i)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -146,7 +146,7 @@ func GetSeasonDataAll() ([]Anime, []byte, error) {
|
||||
}
|
||||
}
|
||||
color.Infof("%d Anime bekommen\n", len(animes))
|
||||
logOut.WriteLine(fmt.Sprintf("%d Anime bekommen", len(animes)))
|
||||
logOut.WriteLine(fmt.Sprintf("📺 %d Anime bekommen", len(animes)))
|
||||
bytes, err := json.Marshal(animes)
|
||||
return animes, bytes, err
|
||||
}
|
||||
@@ -197,6 +197,11 @@ func GetDataJikan(apiAddr string) ([]byte, error) {
|
||||
ctx := context.Background()
|
||||
jikanLimiter.Wait(ctx)
|
||||
statusCode, body, err := fasthttp.Get(body, *jikanApiBaseUri+apiAddr)
|
||||
// retry bcs jikan kekw
|
||||
if statusCode == fasthttp.StatusInternalServerError {
|
||||
jikanLimiter.Wait(ctx)
|
||||
statusCode, body, err = fasthttp.Get(body, *jikanApiBaseUri+apiAddr)
|
||||
}
|
||||
if statusCode != fasthttp.StatusOK {
|
||||
return body, fmt.Errorf("unexpected response code: %s %d", *jikanApiBaseUri+apiAddr, statusCode)
|
||||
}
|
||||
|
||||
39
nuss.go
39
nuss.go
@@ -253,6 +253,39 @@ func ReadAnimeUsers() ([]AnimeUser, error) {
|
||||
return animes, err
|
||||
}
|
||||
|
||||
func ReadAppointments() ([]Appointment, error) {
|
||||
var appoints []Appointment
|
||||
err := db.View(
|
||||
func(tx *nutsdb.Tx) error {
|
||||
entries, err := tx.GetAll(bucketAppoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
appoints = make([]Appointment, 0)
|
||||
// iterate entries
|
||||
for _, e := range entries {
|
||||
// decode appointment list
|
||||
animeId, date, err := BytesToInt64AndDate(e.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// parse user list
|
||||
appointmentUsers, err := parseAppointmentUserList(e.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
appointment := Appointment{
|
||||
Anime: animeId,
|
||||
Time: date,
|
||||
Users: appointmentUsers,
|
||||
}
|
||||
appoints = append(appoints, appointment)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return appoints, err
|
||||
}
|
||||
|
||||
func DbClean() error {
|
||||
return db.Update(
|
||||
func(tx *nutsdb.Tx) error {
|
||||
@@ -297,3 +330,9 @@ func parseWatchUserList(data []byte) ([]WatchUser, error) {
|
||||
err := json.Unmarshal(data, &users)
|
||||
return users, err
|
||||
}
|
||||
|
||||
func parseAppointmentUserList(data []byte) ([]string, error) {
|
||||
var users []string
|
||||
err := json.Unmarshal(data, &users)
|
||||
return users, err
|
||||
}
|
||||
|
||||
12
ober.go
12
ober.go
@@ -20,6 +20,7 @@ func RunWebserv() {
|
||||
r.GET("/api/anime/{id}", Headers(AnimeGet))
|
||||
r.GET("/api/animesearch", Headers(AnimeSearchGet))
|
||||
r.GET("/api/appointment", Headers(AppointmentGet))
|
||||
r.GET("/api/log", Headers(LogGet))
|
||||
r.GET("/api/user/{user?}", Headers(UserGet))
|
||||
r.GET("/api/watch/{user?}", Headers(WatchGet))
|
||||
r.GET("/api/watchext/{user?}", Headers(WatchExtendedGet))
|
||||
@@ -140,6 +141,17 @@ func AppointmentGet(ctx *fasthttp.RequestCtx) {
|
||||
ctx.SetStatusCode(fasthttp.StatusNotImplemented)
|
||||
}
|
||||
|
||||
func LogGet(ctx *fasthttp.RequestCtx) {
|
||||
_, err := ctx.WriteString(logOut.String())
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
ctx.SetContentType("text/plain; charset=utf-8")
|
||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||
}
|
||||
|
||||
func UserGet(ctx *fasthttp.RequestCtx) {
|
||||
usrVal := ctx.UserValue("user")
|
||||
users := make([]User, 0)
|
||||
|
||||
@@ -21,12 +21,12 @@ func Arbeit() {
|
||||
_, bytes, err := GetSeasonDataAll()
|
||||
if err != nil {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
} else {
|
||||
err = seasoncache.Set(seasonApiJikan, bytes)
|
||||
if err != nil {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
}
|
||||
}
|
||||
// refresh animelist of users
|
||||
@@ -34,7 +34,7 @@ func Arbeit() {
|
||||
if err != nil {
|
||||
if err != nutsdb.ErrBucketEmpty {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func Arbeit() {
|
||||
newProgress, updated, err := FetchProgress(a.Anime, u.MalID, u.Username, u.Progress)
|
||||
if err != nil {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
continue
|
||||
}
|
||||
if newProgress == u.Progress {
|
||||
@@ -53,11 +53,11 @@ func Arbeit() {
|
||||
}
|
||||
// update db
|
||||
color.Infof("%s progress von %d: %d -> %d\n", u.Username, a.Anime, u.Progress, newProgress)
|
||||
logOut.WriteLine(fmt.Sprintf("%s progress von %d: %d -> %d", u.Username, a.Anime, u.Progress, newProgress))
|
||||
logOut.WriteLine(fmt.Sprintf("📜 %s progress von %d: %d -> %d", u.Username, a.Anime, u.Progress, newProgress))
|
||||
err = UpdateUserAnimeProgress(a.Anime, u.MalID, newProgress, updated)
|
||||
if err != nil {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,21 +78,21 @@ func LangeArbeit() {
|
||||
// check if no users registered
|
||||
if err != nutsdb.ErrBucketEmpty {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
}
|
||||
} else {
|
||||
for _, u := range regUsers {
|
||||
_, _, err = GetUserData(u.Username)
|
||||
if err != nil {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
continue
|
||||
}
|
||||
count++
|
||||
}
|
||||
}
|
||||
color.Infof("%d User aktualisiert\n", count)
|
||||
logOut.WriteLine(fmt.Sprintf("%d User aktualisiert", count))
|
||||
logOut.WriteLine(fmt.Sprintf("🔃 %d User aktualisiert", count))
|
||||
|
||||
count = 0
|
||||
// refresh anime cache with watched
|
||||
@@ -100,7 +100,7 @@ func LangeArbeit() {
|
||||
if err != nil {
|
||||
if err != nutsdb.ErrBucketEmpty {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
}
|
||||
} else {
|
||||
for _, a := range animesUsers {
|
||||
@@ -112,7 +112,7 @@ func LangeArbeit() {
|
||||
err = refreshAnime(a.Anime)
|
||||
if err != nil {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
continue
|
||||
}
|
||||
count++
|
||||
@@ -120,7 +120,7 @@ func LangeArbeit() {
|
||||
}
|
||||
|
||||
color.Infof("%d Anime aktualisiert\n", count)
|
||||
logOut.WriteLine(fmt.Sprintf("%d Anime aktualisiert", count))
|
||||
logOut.WriteLine(fmt.Sprintf("🔃 %d Anime aktualisiert", count))
|
||||
}
|
||||
|
||||
func SehrLangeArbeiten() {
|
||||
@@ -128,7 +128,7 @@ func SehrLangeArbeiten() {
|
||||
err := DbClean()
|
||||
if err != nil {
|
||||
color.Errorln(err.Error())
|
||||
logOut.WriteLine(err.Error())
|
||||
logOut.WriteError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
19
rechner.go
19
rechner.go
@@ -5,6 +5,8 @@ import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Sauce(malid int64, username string) string {
|
||||
@@ -24,3 +26,20 @@ func Int64ToByte(yes int64) []byte {
|
||||
n := binary.PutVarint(buf, yes)
|
||||
return buf[:n]
|
||||
}
|
||||
|
||||
func BytesToInt64AndDate(bytes []byte) (int64, time.Time, error) {
|
||||
split := strings.Split(string(bytes), AppointSplit)
|
||||
if len(split) != 2 {
|
||||
return 0, time.Time{}, fmt.Errorf("invalid appointment split %s", string(bytes))
|
||||
}
|
||||
num, err := strconv.ParseInt(split[0], 10, 64)
|
||||
if err != nil {
|
||||
return num, time.Time{}, err
|
||||
}
|
||||
appoint, err := time.Parse(time.RFC3339, split[1])
|
||||
return num, appoint, err
|
||||
}
|
||||
|
||||
func Int64AndDateToBytes(num int64, appoint time.Time) []byte {
|
||||
return []byte(fmt.Sprintf("%d%s%s", num, AppointSplit, appoint.Format(time.RFC3339)))
|
||||
}
|
||||
|
||||
6
ring.go
6
ring.go
@@ -33,6 +33,10 @@ func (rb *RingBuf) WriteLine(in string) (int, error) {
|
||||
return rb.Write([]byte(fmt.Sprintf("[%s]: %s\n", time.Now().Format("2006-01-02 15:04:05"), in)))
|
||||
}
|
||||
|
||||
func (rb *RingBuf) WriteError(err error) (int, error) {
|
||||
return rb.Write([]byte(fmt.Sprintf("[%s]: ⚠️ %s\n", time.Now().Format("2006-01-02 15:04:05"), err.Error())))
|
||||
}
|
||||
|
||||
// Write writes all data from input buf to RingBuf, overriding looped data
|
||||
func (rb *RingBuf) Write(buf []byte) (int, error) {
|
||||
rb.Lock()
|
||||
@@ -83,7 +87,7 @@ func (rb *RingBuf) Bytes() []byte {
|
||||
}
|
||||
return out
|
||||
}
|
||||
out := make([]byte, rb.writePos+1)
|
||||
out := make([]byte, rb.writePos)
|
||||
copy(out, rb.data[:rb.writePos])
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user