mirror of
https://github.com/ultrasn0w/huso.git
synced 2025-12-14 17:19: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"`
|
} `json:"season"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SeasonJikan struct {
|
type SeasonAnimeJikan 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 {
|
|
||||||
MalID int64 `json:"mal_id"`
|
MalID int64 `json:"mal_id"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Images struct {
|
Images struct {
|
||||||
@@ -246,5 +235,18 @@ type SeasonJikan struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
} `json:"demographics"`
|
} `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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/xujiajun/nutsdb"
|
"github.com/xujiajun/nutsdb"
|
||||||
)
|
)
|
||||||
@@ -31,6 +29,41 @@ func SaveUser(user *UserData) error {
|
|||||||
return err
|
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) {
|
func ReadAnimes() ([]Anime, error) {
|
||||||
var animes []Anime
|
var animes []Anime
|
||||||
err := db.View(
|
err := db.View(
|
||||||
@@ -41,20 +74,19 @@ func ReadAnimes() ([]Anime, error) {
|
|||||||
}
|
}
|
||||||
animes = make([]Anime, len(entries))
|
animes = make([]Anime, len(entries))
|
||||||
// iterate entries
|
// iterate entries
|
||||||
for _, entry := range entries {
|
for _, e := range entries {
|
||||||
// decode anime list
|
// decode anime list
|
||||||
malId, c := binary.Varint(entry.Key)
|
animeId, err := BytesToInt64(e.Key)
|
||||||
if c <= 0 {
|
if err != nil {
|
||||||
return fmt.Errorf("int64 decode error: %s %s", entry.Key, entry.Value)
|
return err
|
||||||
}
|
}
|
||||||
// parse user list
|
// parse user list
|
||||||
var users []WatchUser
|
users, err := parseUserList(e.Value)
|
||||||
err = json.Unmarshal(entry.Value, &users)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
anime := Anime{
|
anime := Anime{
|
||||||
Anime: malId,
|
Anime: animeId,
|
||||||
Users: users,
|
Users: users,
|
||||||
}
|
}
|
||||||
animes = append(animes, anime)
|
animes = append(animes, anime)
|
||||||
@@ -102,3 +134,9 @@ func DbDelete(bucket, key string, val []byte) error {
|
|||||||
return tx.Delete(bucket, keyBytes)
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha512"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -45,14 +44,11 @@ func Season(ctx *fasthttp.RequestCtx) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = ctx.Write(data)
|
err = writeResponseBody(ctx, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
addErrorToCtx(ctx, err)
|
addErrorToCtx(ctx, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.SetContentType("application/json; charset=utf-8")
|
|
||||||
ctx.SetStatusCode(fasthttp.StatusOK)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func WatchGet(ctx *fasthttp.RequestCtx) {
|
func WatchGet(ctx *fasthttp.RequestCtx) {
|
||||||
@@ -126,7 +122,7 @@ func Register(ctx *fasthttp.RequestCtx) {
|
|||||||
return
|
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) {
|
if calcSauce != strings.ToLower(register.Sauce) {
|
||||||
ctx.WriteString("Möge die Sauce mit dir sein")
|
ctx.WriteString("Möge die Sauce mit dir sein")
|
||||||
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
ctx.SetStatusCode(fasthttp.StatusBadRequest)
|
||||||
@@ -174,7 +170,8 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
username := fmt.Sprintf("%s", ctx.UserValue("user"))
|
username := fmt.Sprintf("%s", ctx.UserValue("user"))
|
||||||
if !GheddoAuth(username, string(auth)) {
|
legit, userId := GheddoAuth(username, string(auth))
|
||||||
|
if !legit {
|
||||||
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
|
ctx.SetStatusCode(fasthttp.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -188,10 +185,21 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, anime := range animes {
|
|
||||||
// TODO check if anime is in season
|
|
||||||
|
|
||||||
// iterate sent animes
|
// 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)
|
detail, _, err := GetAnimeDetailData(anime.Anime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.WriteString(err.Error())
|
ctx.WriteString(err.Error())
|
||||||
@@ -199,15 +207,36 @@ func WatchPost(ctx *fasthttp.RequestCtx) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("%+v\n", detail)
|
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)
|
user, err := ReadUser(username)
|
||||||
if err != nil {
|
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) {
|
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