From 605652b0e312715172235ea9dcc894a73a5a37fe Mon Sep 17 00:00:00 2001 From: The-Foxon Date: Tue, 12 Sep 2023 22:25:44 +1200 Subject: [PATCH] Initial commit --- .vscode/settings.json | 3 ++ anilibria/GetTeam.go | 23 +++++++++++ anilibria/GetTitleByCode.go | 24 +++++++++++ anilibria/GetTitleByID.go | 28 +++++++++++++ anilibria/GetTitleByTorrentID.go | 28 +++++++++++++ anilibria/GetTitleChanges.go | 23 +++++++++++ anilibria/GetTitleList.go | 23 +++++++++++ anilibria/GetTitleRandom.go | 23 +++++++++++ anilibria/GetTitleUpdates.go | 23 +++++++++++ anilibria/errors.go | 9 +++++ anilibria/request.go | 35 ++++++++++++++++ anilibria/vars.go | 3 ++ go.mod | 3 ++ model/Episodes.go | 7 ++++ model/Pagination.go | 8 ++++ model/Player.go | 37 +++++++++++++++++ model/Team.go | 9 +++++ model/Title.go | 69 ++++++++++++++++++++++++++++++++ model/TitleList.go | 6 +++ model/Torrent.go | 26 ++++++++++++ 20 files changed, 410 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 anilibria/GetTeam.go create mode 100644 anilibria/GetTitleByCode.go create mode 100644 anilibria/GetTitleByID.go create mode 100644 anilibria/GetTitleByTorrentID.go create mode 100644 anilibria/GetTitleChanges.go create mode 100644 anilibria/GetTitleList.go create mode 100644 anilibria/GetTitleRandom.go create mode 100644 anilibria/GetTitleUpdates.go create mode 100644 anilibria/errors.go create mode 100644 anilibria/request.go create mode 100644 anilibria/vars.go create mode 100644 go.mod create mode 100644 model/Episodes.go create mode 100644 model/Pagination.go create mode 100644 model/Player.go create mode 100644 model/Team.go create mode 100644 model/Title.go create mode 100644 model/TitleList.go create mode 100644 model/Torrent.go diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1f2b05c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "nuxt.isNuxtApp": false +} \ No newline at end of file diff --git a/anilibria/GetTeam.go b/anilibria/GetTeam.go new file mode 100644 index 0000000..2af0a06 --- /dev/null +++ b/anilibria/GetTeam.go @@ -0,0 +1,23 @@ +package anilibria + +import ( + "encoding/json" + "errors" + + "git.thefoxon.ru/anilibria/anilibria-go/model" +) + +func GetTeam() (team *model.Team, err error) { + + reqBody, status, err := request("GET", "/v3/team", nil) + + if status != 200 { + return nil, errors.New(errorFetchTeam) + } + + if err = json.Unmarshal(reqBody, &team); err != nil { + return nil, err + } + + return +} diff --git a/anilibria/GetTitleByCode.go b/anilibria/GetTitleByCode.go new file mode 100644 index 0000000..a7b661c --- /dev/null +++ b/anilibria/GetTitleByCode.go @@ -0,0 +1,24 @@ +package anilibria + +import ( + "encoding/json" + "errors" + "fmt" + + "git.thefoxon.ru/anilibria/anilibria-go/model" +) + +func GetTitleByCode(code string) (title *model.Title, err error) { + + reqBody, status, err := request("GET", fmt.Sprintf("/v3/title?code=%s", code), nil) + + if status != 200 { + return nil, errors.New(errorFetchTitle) + } + + if err = json.Unmarshal(reqBody, &title); err != nil { + return nil, err + } + + return +} diff --git a/anilibria/GetTitleByID.go b/anilibria/GetTitleByID.go new file mode 100644 index 0000000..c2415b3 --- /dev/null +++ b/anilibria/GetTitleByID.go @@ -0,0 +1,28 @@ +package anilibria + +import ( + "encoding/json" + "errors" + "fmt" + + "git.thefoxon.ru/anilibria/anilibria-go/model" +) + +func GetTitleByID(id uint64) (title *model.Title, err error) { + + if id == 0 { + return nil, errors.New(errorTitleIdIsZero) + } + + reqBody, status, err := request("GET", fmt.Sprintf("/v3/title?id=%d", id), nil) + + if status != 200 { + return nil, errors.New(errorFetchTitle) + } + + if err = json.Unmarshal(reqBody, &title); err != nil { + return nil, err + } + + return +} diff --git a/anilibria/GetTitleByTorrentID.go b/anilibria/GetTitleByTorrentID.go new file mode 100644 index 0000000..dc3714f --- /dev/null +++ b/anilibria/GetTitleByTorrentID.go @@ -0,0 +1,28 @@ +package anilibria + +import ( + "encoding/json" + "errors" + "fmt" + + "git.thefoxon.ru/anilibria/anilibria-go/model" +) + +func GetTitleByTorrentID(id uint64) (title *model.Title, err error) { + + if id == 0 { + return nil, errors.New(errorTitleIdIsZero) + } + + reqBody, status, err := request("GET", fmt.Sprintf("/v3/title?torrent_id=%d", id), nil) + + if status != 200 { + return nil, errors.New(errorFetchTitle) + } + + if err = json.Unmarshal(reqBody, &title); err != nil { + return nil, err + } + + return +} diff --git a/anilibria/GetTitleChanges.go b/anilibria/GetTitleChanges.go new file mode 100644 index 0000000..ebf054a --- /dev/null +++ b/anilibria/GetTitleChanges.go @@ -0,0 +1,23 @@ +package anilibria + +import ( + "encoding/json" + "errors" + + "git.thefoxon.ru/anilibria/anilibria-go/model" +) + +func GetTitleChanges() (titles *[]model.TitleList, err error) { + + reqBody, status, err := request("GET", "/v3/title/changes", nil) + + if status != 200 { + return nil, errors.New(errorFetchTitle) + } + + if err = json.Unmarshal(reqBody, &titles); err != nil { + return nil, err + } + + return +} diff --git a/anilibria/GetTitleList.go b/anilibria/GetTitleList.go new file mode 100644 index 0000000..031469e --- /dev/null +++ b/anilibria/GetTitleList.go @@ -0,0 +1,23 @@ +package anilibria + +import ( + "encoding/json" + "errors" + + "git.thefoxon.ru/anilibria/anilibria-go/model" +) + +func GetTitleList() (title *[]model.Title, err error) { + + reqBody, status, err := request("GET", "/v3/title/list", nil) + + if status != 200 { + return nil, errors.New(errorFetchTitle) + } + + if err = json.Unmarshal(reqBody, &title); err != nil { + return nil, err + } + + return +} diff --git a/anilibria/GetTitleRandom.go b/anilibria/GetTitleRandom.go new file mode 100644 index 0000000..f292d11 --- /dev/null +++ b/anilibria/GetTitleRandom.go @@ -0,0 +1,23 @@ +package anilibria + +import ( + "encoding/json" + "errors" + + "git.thefoxon.ru/anilibria/anilibria-go/model" +) + +func GetTitleRandom() (titles *model.TitleList, err error) { + + reqBody, status, err := request("GET", "/v3/title/random", nil) + + if status != 200 { + return nil, errors.New(errorFetchTitle) + } + + if err = json.Unmarshal(reqBody, &titles); err != nil { + return nil, err + } + + return +} diff --git a/anilibria/GetTitleUpdates.go b/anilibria/GetTitleUpdates.go new file mode 100644 index 0000000..851e07a --- /dev/null +++ b/anilibria/GetTitleUpdates.go @@ -0,0 +1,23 @@ +package anilibria + +import ( + "encoding/json" + "errors" + + "git.thefoxon.ru/anilibria/anilibria-go/model" +) + +func GetTitleUpdates() (titles *[]model.TitleList, err error) { + + reqBody, status, err := request("GET", "/v3/title/updates", nil) + + if status != 200 { + return nil, errors.New(errorFetchTitle) + } + + if err = json.Unmarshal(reqBody, &titles); err != nil { + return nil, err + } + + return +} diff --git a/anilibria/errors.go b/anilibria/errors.go new file mode 100644 index 0000000..dbe038e --- /dev/null +++ b/anilibria/errors.go @@ -0,0 +1,9 @@ +package anilibria + +const ( + // GetTitleErrors + errorFetchTitle = "Error fetch title" + errorTitleIdIsZero = "Error title ID is zero" + // GetTeamErrors + errorFetchTeam = "Error fetch team" +) diff --git a/anilibria/request.go b/anilibria/request.go new file mode 100644 index 0000000..e999896 --- /dev/null +++ b/anilibria/request.go @@ -0,0 +1,35 @@ +package anilibria + +import ( + "fmt" + "io" + "net/http" +) + +func request(method string, url string, body io.Reader) ([]byte, int, error) { + var err error + var req *http.Request + var res *http.Response + var client *http.Client = http.DefaultClient + + url = getURL(url) + + if req, err = http.NewRequest(method, url, body); err != nil { + return nil, -1, err + } + + if res, err = client.Do(req); err != nil { + return nil, -1, err + } + + defer res.Body.Close() + data, err := io.ReadAll(res.Body) + if err != nil { + return nil, res.StatusCode, err + } + return data, res.StatusCode, nil +} + +func getURL(path string) string { + return fmt.Sprintf("%s%s", BASE_URL, path) +} diff --git a/anilibria/vars.go b/anilibria/vars.go new file mode 100644 index 0000000..f8f9de0 --- /dev/null +++ b/anilibria/vars.go @@ -0,0 +1,3 @@ +package anilibria + +var BASE_URL string = "http://api.anilibria.tv" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..623080f --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.thefoxon.ru/anilibria/anilibria-go + +go 1.21.1 diff --git a/model/Episodes.go b/model/Episodes.go new file mode 100644 index 0000000..2354ab6 --- /dev/null +++ b/model/Episodes.go @@ -0,0 +1,7 @@ +package model + +type Episodes struct { + First int `json:"first"` + Last int `json:"last"` + String string `json:"string"` +} diff --git a/model/Pagination.go b/model/Pagination.go new file mode 100644 index 0000000..5e81629 --- /dev/null +++ b/model/Pagination.go @@ -0,0 +1,8 @@ +package model + +type Pagination struct { + Pages int64 `json:"pages"` + CurrentPage int64 `json:"current_page"` + ItemsPerPage int64 `json:"items_per_page"` + TotalItems int64 `json:"total_items"` +} diff --git a/model/Player.go b/model/Player.go new file mode 100644 index 0000000..2b8747c --- /dev/null +++ b/model/Player.go @@ -0,0 +1,37 @@ +package model + +type Player struct { + AlternativePlayer string `json:"alternative_player"` + Host string `json:"host"` + IsRutube bool `json:"is_rutube"` + Episodes Episodes `json:"episodes"` + List map[string]playerEpisode `json:"list"` + Rutube map[string]rutubeEpisod `json:"rutube"` +} + +type playerEpisode struct { + Episode int `json:"episode"` + Name string `json:"name"` + UUID string `json:"uuid"` + CreatedTimestamp int `json:"created_timestamp"` + Preview string `json:"preview"` + Skips playerSkips `json:"skips"` + Hls playerHls `json:"hls"` +} + +type playerSkips struct { + Opening []any `json:"opening"` + Ending []any `json:"ending"` +} + +type playerHls struct { + Fhd string `json:"fhd"` + Hd string `json:"hd"` + Sd string `json:"sd"` +} + +type rutubeEpisod struct { + Episode int `json:"episode"` + CreatedTimestamp int `json:"created_timestamp"` + RutubeID string `json:"rutube_id"` +} diff --git a/model/Team.go b/model/Team.go new file mode 100644 index 0000000..f1c9326 --- /dev/null +++ b/model/Team.go @@ -0,0 +1,9 @@ +package model + +type Team struct { + Voice []string `json:"voice"` + Translator []string `json:"translator"` + Editing []string `json:"editing"` + Decor []string `json:"decor"` + Timing []string `json:"timing"` +} diff --git a/model/Title.go b/model/Title.go new file mode 100644 index 0000000..9ac0747 --- /dev/null +++ b/model/Title.go @@ -0,0 +1,69 @@ +package model + +type Title struct { + ID int `json:"id"` + Code string `json:"code"` + Names titleNames `json:"names"` + Franchises []any `json:"franchises"` + Announce any `json:"announce"` + Status titleStatus `json:"status"` + Posters titlePosters `json:"posters"` + Updated int `json:"updated"` + LastChange int `json:"last_change"` + Type titleType `json:"type"` + Genres []string `json:"genres"` + Team Team `json:"team"` + Season titleSeason `json:"season"` + Description string `json:"description"` + InFavorites int `json:"in_favorites"` + Blocked titleBlocked `json:"blocked"` + Player Player `json:"player"` + Torrents titleTorrents `json:"torrents"` +} + +type titleNames struct { + Ru string `json:"ru"` + En string `json:"en"` + Alternative any `json:"alternative"` +} + +type titleStatus struct { + String string `json:"string"` + Code int `json:"code"` +} + +type titlePoster struct { + URL string `json:"url"` + RawBase64File any `json:"raw_base64_file"` +} + +type titlePosters struct { + Small titlePoster `json:"small"` + Medium titlePoster `json:"medium"` + Original titlePoster `json:"original"` +} + +type titleType struct { + FullString string `json:"full_string"` + Code int `json:"code"` + String string `json:"string"` + Episodes int `json:"episodes"` + Length int `json:"length"` +} + +type titleSeason struct { + String string `json:"string"` + Code int `json:"code"` + Year int `json:"year"` + WeekDay int `json:"week_day"` +} + +type titleBlocked struct { + Blocked bool `json:"blocked"` + Bakanim bool `json:"bakanim"` +} + +type titleTorrents struct { + Episodes Episodes `json:"episodes"` + List []Torrent `json:"list"` +} diff --git a/model/TitleList.go b/model/TitleList.go new file mode 100644 index 0000000..29dca4f --- /dev/null +++ b/model/TitleList.go @@ -0,0 +1,6 @@ +package model + +type TitleList struct { + List []Title `json:"list"` + Pagination Pagination `json:"pagination"` +} diff --git a/model/Torrent.go b/model/Torrent.go new file mode 100644 index 0000000..3c9296d --- /dev/null +++ b/model/Torrent.go @@ -0,0 +1,26 @@ +package model + +type Torrent struct { + TorrentID int `json:"torrent_id"` + Episodes Episodes `json:"episodes"` + Quality torrentQuality `json:"quality"` + Leechers int `json:"leechers"` + Seeders int `json:"seeders"` + Downloads int `json:"downloads"` + TotalSize int64 `json:"total_size"` + SizeString string `json:"size_string"` + URL string `json:"url"` + Magnet string `json:"magnet"` + UploadedTimestamp int `json:"uploaded_timestamp"` + Hash string `json:"hash"` + Metadata any `json:"metadata"` + RawBase64File *string `json:"raw_base64_file"` +} + +type torrentQuality struct { + String string `json:"string"` + Type string `json:"type"` + Resolution string `json:"resolution"` + Encoder string `json:"encoder"` + LqAudio any `json:"lq_audio"` +}