mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-14 10:39:53 +01:00
POST watching
This commit is contained in:
28
klotz.go
28
klotz.go
@@ -127,18 +127,7 @@ type SeasonMal struct {
|
||||
} `json:"season"`
|
||||
}
|
||||
|
||||
type SeasonJikan struct {
|
||||
Pagination struct {
|
||||
LastVisiblePage int `json:"last_visible_page"`
|
||||
HasNextPage bool `json:"has_next_page"`
|
||||
CurrentPage int `json:"current_page"`
|
||||
Items struct {
|
||||
Count int `json:"count"`
|
||||
Total int `json:"total"`
|
||||
PerPage int `json:"per_page"`
|
||||
} `json:"items"`
|
||||
} `json:"pagination"`
|
||||
Data []struct {
|
||||
type SeasonAnimeJikan struct {
|
||||
MalID int64 `json:"mal_id"`
|
||||
URL string `json:"url"`
|
||||
Images struct {
|
||||
@@ -246,5 +235,18 @@ type SeasonJikan struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"demographics"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type SeasonJikan struct {
|
||||
Pagination struct {
|
||||
LastVisiblePage int `json:"last_visible_page"`
|
||||
HasNextPage bool `json:"has_next_page"`
|
||||
CurrentPage int `json:"current_page"`
|
||||
Items struct {
|
||||
Count int `json:"count"`
|
||||
Total int `json:"total"`
|
||||
PerPage int `json:"per_page"`
|
||||
} `json:"items"`
|
||||
} `json:"pagination"`
|
||||
Data []SeasonAnimeJikan `json:"data"`
|
||||
}
|
||||
|
||||
56
nuss.go
56
nuss.go
@@ -1,10 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/xujiajun/nutsdb"
|
||||
)
|
||||
@@ -31,6 +29,41 @@ func SaveUser(user *UserData) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func AddUserToAnime(username string, userId, animeId int64) (*Anime, error) {
|
||||
var anime Anime
|
||||
err := db.Update(
|
||||
func(tx *nutsdb.Tx) error {
|
||||
keyBytes := Int64ToByte(animeId)
|
||||
e, err := tx.Get(bucketAnime, keyBytes)
|
||||
var users []WatchUser
|
||||
if err != nil {
|
||||
users = make([]WatchUser, 1)
|
||||
} else {
|
||||
// parse user list
|
||||
users, err = parseUserList(e.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// add user
|
||||
users = append(users, WatchUser{
|
||||
Username: username,
|
||||
MalID: userId,
|
||||
})
|
||||
anime = Anime{
|
||||
Anime: animeId,
|
||||
Users: users,
|
||||
}
|
||||
newData, err := json.Marshal(users)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Put(bucketAnime, keyBytes, newData, nutsdb.Persistent)
|
||||
})
|
||||
return &anime, err
|
||||
}
|
||||
|
||||
func ReadAnimes() ([]Anime, error) {
|
||||
var animes []Anime
|
||||
err := db.View(
|
||||
@@ -41,20 +74,19 @@ func ReadAnimes() ([]Anime, error) {
|
||||
}
|
||||
animes = make([]Anime, len(entries))
|
||||
// iterate entries
|
||||
for _, entry := range entries {
|
||||
for _, e := range entries {
|
||||
// decode anime list
|
||||
malId, c := binary.Varint(entry.Key)
|
||||
if c <= 0 {
|
||||
return fmt.Errorf("int64 decode error: %s %s", entry.Key, entry.Value)
|
||||
animeId, err := BytesToInt64(e.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// parse user list
|
||||
var users []WatchUser
|
||||
err = json.Unmarshal(entry.Value, &users)
|
||||
users, err := parseUserList(e.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
anime := Anime{
|
||||
Anime: malId,
|
||||
Anime: animeId,
|
||||
Users: users,
|
||||
}
|
||||
animes = append(animes, anime)
|
||||
@@ -102,3 +134,9 @@ func DbDelete(bucket, key string, val []byte) error {
|
||||
return tx.Delete(bucket, keyBytes)
|
||||
})
|
||||
}
|
||||
|
||||
func parseUserList(data []byte) ([]WatchUser, error) {
|
||||
var users []WatchUser
|
||||
err := json.Unmarshal(data, &users)
|
||||
return users, err
|
||||
}
|
||||
|
||||
55
ober.go
55
ober.go
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -45,14 +44,11 @@ func Season(ctx *fasthttp.RequestCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = ctx.Write(data)
|
||||
err = writeResponseBody(ctx, data)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.SetContentType("application/json; charset=utf-8")
|
||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||
}
|
||||
|
||||
func WatchGet(ctx *fasthttp.RequestCtx) {
|
||||
@@ -126,7 +122,7 @@ func Register(ctx *fasthttp.RequestCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
calcSauce := fmt.Sprintf("%x", sha512.Sum512([]byte(registerSecret+strconv.FormatInt(register.MalID, 10)+register.Username)))
|
||||
calcSauce := Sauce(register.MalID, register.Username)
|
||||
if calcSauce != strings.ToLower(register.Sauce) {
|
||||
ctx.WriteString("Möge die Sauce mit dir sein")
|
||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||
@@ -174,7 +170,8 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
|
||||
return
|
||||
}
|
||||
username := fmt.Sprintf("%s", ctx.UserValue("user"))
|
||||
if !GheddoAuth(username, string(auth)) {
|
||||
legit, userId := GheddoAuth(username, string(auth))
|
||||
if !legit {
|
||||
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
@@ -188,10 +185,21 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, anime := range animes {
|
||||
// TODO check if anime is in season
|
||||
|
||||
// iterate sent animes
|
||||
for _, anime := range animes {
|
||||
// check if anime is in season
|
||||
_, err = SearchSeason(anime.Anime)
|
||||
if err == nil {
|
||||
// anime exitsts => save
|
||||
animeData, err := AddUserToAnime(username, userId, anime.Anime)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
anime.Users = animeData.Users
|
||||
}
|
||||
|
||||
// TODO
|
||||
detail, _, err := GetAnimeDetailData(anime.Anime)
|
||||
if err != nil {
|
||||
ctx.WriteString(err.Error())
|
||||
@@ -199,15 +207,36 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
|
||||
return
|
||||
}
|
||||
fmt.Printf("%+v\n", detail)
|
||||
|
||||
data, err := json.Marshal(animes)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
err = writeResponseBody(ctx, data)
|
||||
if err != nil {
|
||||
addErrorToCtx(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GheddoAuth(username, auth string) bool {
|
||||
func GheddoAuth(username, auth string) (bool, int64) {
|
||||
user, err := ReadUser(username)
|
||||
if err != nil {
|
||||
return false
|
||||
return false, 0
|
||||
}
|
||||
return user.Secret == auth
|
||||
return user.Secret == auth, user.MalID
|
||||
}
|
||||
|
||||
func writeResponseBody(ctx *fasthttp.RequestCtx, bytes []byte) error {
|
||||
_, err := ctx.Write(bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.SetContentType("application/json; charset=utf-8")
|
||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
||||
return err
|
||||
}
|
||||
|
||||
func addErrorToCtx(ctx *fasthttp.RequestCtx, err error) {
|
||||
|
||||
13
raser.go
13
raser.go
@@ -1,13 +0,0 @@
|
||||
package main
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func GetSeasonCache() (*SeasonJikan, error) {
|
||||
data, err := cache.Get(seasonApiJikan)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var seasonData SeasonJikan
|
||||
err = json.Unmarshal(data, &seasonData)
|
||||
return &seasonData, err
|
||||
}
|
||||
26
rechner.go
Normal file
26
rechner.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Sauce(malid int64, username string) string {
|
||||
return fmt.Sprintf("%x", sha512.Sum512([]byte(registerSecret+strconv.FormatInt(malid, 10)+username)))
|
||||
}
|
||||
|
||||
func BytesToInt64(bytes []byte) (int64, error) {
|
||||
yes, c := binary.Varint(bytes)
|
||||
if c <= 0 {
|
||||
return yes, fmt.Errorf("int64 decode error: %s", bytes)
|
||||
}
|
||||
return yes, nil
|
||||
}
|
||||
|
||||
func Int64ToByte(yes int64) []byte {
|
||||
buf := make([]byte, binary.MaxVarintLen64)
|
||||
n := binary.PutVarint(buf, yes)
|
||||
return buf[:n]
|
||||
}
|
||||
32
schaffer.go
Normal file
32
schaffer.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
func GetSeasonCache() (*SeasonJikan, error) {
|
||||
data, err := cache.Get(seasonApiJikan)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var seasonData SeasonJikan
|
||||
err = json.Unmarshal(data, &seasonData)
|
||||
return &seasonData, err
|
||||
}
|
||||
|
||||
func SearchSeason(malId int64) (*SeasonAnimeJikan, error) {
|
||||
season, err := GetSeasonCache()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if season.Pagination.Items.Count == 0 {
|
||||
return nil, errors.New("no seasonal anime")
|
||||
}
|
||||
for _, a := range season.Data {
|
||||
if a.MalID == malId {
|
||||
return &a, err
|
||||
}
|
||||
}
|
||||
return nil, errors.New("anime not found")
|
||||
}
|
||||
Reference in New Issue
Block a user