-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
259 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package anilist | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/sonalys/animeman/internal/utils" | ||
) | ||
|
||
type AnimeListArg interface { | ||
ApplyList(url.Values) | ||
} | ||
|
||
type ( | ||
ListStatus string | ||
AiringStatus string | ||
Title string | ||
|
||
AnimeListEntry struct { | ||
Status ListStatus `json:"status"` | ||
Media struct { | ||
Type string | ||
AiringStatus AiringStatus `json:"status"` | ||
Title struct { | ||
Romaji string `json:"romaji"` | ||
English string `json:"english"` | ||
Native string `json:"native"` | ||
} `json:"title"` | ||
} `json:"media"` | ||
} | ||
|
||
GraphqlQuery struct { | ||
Query string `json:"query"` | ||
Variables map[string]any `json:"variables"` | ||
} | ||
|
||
AnimeListResp struct { | ||
Data struct { | ||
MediaListCollection struct { | ||
Lists []struct { | ||
Entries []AnimeListEntry `json:"entries"` | ||
} `json:"lists"` | ||
} `json:"MediaListCollection"` | ||
} `json:"data"` | ||
} | ||
) | ||
|
||
const ( | ||
ListStatusWatching ListStatus = "CURRENT" | ||
ListStatusCompleted ListStatus = "COMPLETED" | ||
ListStatusDropped ListStatus = "DROPPED" | ||
ListStatusPlanning ListStatus = "PLANNING" | ||
) | ||
|
||
const ( | ||
AiringStatusAiring AiringStatus = "AIRING" | ||
AiringStatusCompleted AiringStatus = "COMPLETED" | ||
) | ||
|
||
const getCurrentlyWatchingQuery = `query($userName:String,$type:MediaType){ | ||
MediaListCollection(userName:$userName,type:$type){ | ||
lists{ | ||
name | ||
entries{ | ||
status | ||
media{ | ||
title{romaji english native} | ||
type | ||
status(version:2) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
|
||
func filter[T any](in []T, f func(T) bool) []T { | ||
out := make([]T, 0, len(in)) | ||
for i := range in { | ||
if f(in[i]) { | ||
out = append(out, in[i]) | ||
} | ||
} | ||
return out | ||
} | ||
|
||
func (api *API) GetCurrentlyWatching(ctx context.Context) ([]AnimeListEntry, error) { | ||
var path = API_URL + "/animelist/" + api.Username + "/load.json" | ||
body := GraphqlQuery{ | ||
Query: getCurrentlyWatchingQuery, | ||
Variables: map[string]any{ | ||
"userName": api.Username, | ||
"type": "ANIME", | ||
}, | ||
} | ||
req := utils.Must(http.NewRequestWithContext(ctx, http.MethodPost, path, bytes.NewReader(utils.Must(json.Marshal(body))))) | ||
req.Header.Add("Content-Type", "application/json") | ||
req.Header.Add("Accept", "application/json") | ||
resp, err := api.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("fetching response: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("fetching anime list: %s", string(utils.Must(io.ReadAll(resp.Body)))) | ||
} | ||
var entries AnimeListResp | ||
if err := json.NewDecoder(resp.Body).Decode(&entries); err != nil { | ||
return nil, fmt.Errorf("reading response: %w", err) | ||
} | ||
var out []AnimeListEntry | ||
for _, list := range entries.Data.MediaListCollection.Lists { | ||
watchingEntries := filter(list.Entries, func(entry AnimeListEntry) bool { | ||
return entry.Status == ListStatusWatching | ||
}) | ||
out = append(out, watchingEntries...) | ||
} | ||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package anilist | ||
|
||
import "net/http" | ||
|
||
const API_URL = "https://graphql.anilist.co" | ||
|
||
type ( | ||
API struct { | ||
Username string | ||
client *http.Client | ||
} | ||
) | ||
|
||
func New(client *http.Client, username string) *API { | ||
return &API{ | ||
client: client, | ||
Username: username, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package anilist | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/sonalys/animeman/integrations/anilist" | ||
"github.com/sonalys/animeman/internal/roundtripper" | ||
"github.com/sonalys/animeman/pkg/v1/animelist" | ||
"golang.org/x/time/rate" | ||
) | ||
|
||
type ( | ||
Wrapper struct { | ||
client *anilist.API | ||
} | ||
) | ||
|
||
const userAgent = "github.com/sonalys/animeman" | ||
|
||
func New(username string) *Wrapper { | ||
client := &http.Client{ | ||
Transport: roundtripper.NewUserAgentTransport( | ||
roundtripper.NewRateLimitedTransport( | ||
http.DefaultTransport, rate.NewLimiter(rate.Every(time.Second), 1), | ||
), userAgent), | ||
Timeout: 10 * time.Second, | ||
} | ||
return &Wrapper{ | ||
client: anilist.New(client, username), | ||
} | ||
} | ||
|
||
func convertStatus(in anilist.ListStatus) animelist.ListStatus { | ||
switch in { | ||
case anilist.ListStatusWatching: | ||
return animelist.ListStatusWatching | ||
case anilist.ListStatusCompleted: | ||
return animelist.ListStatusCompleted | ||
case anilist.ListStatusDropped: | ||
return animelist.ListStatusDropped | ||
case anilist.ListStatusPlanning: | ||
return animelist.ListStatusPlanToWatch | ||
default: | ||
log.Fatal().Msgf("unexpected status from anilist: %s", in) | ||
} | ||
return animelist.ListStatusAll | ||
} | ||
|
||
func convertAiringStatus(in anilist.AiringStatus) animelist.AiringStatus { | ||
switch in { | ||
case anilist.AiringStatusAiring: | ||
return animelist.AiringStatusAiring | ||
case anilist.AiringStatusCompleted: | ||
return animelist.AiringStatusAired | ||
} | ||
return animelist.AiringStatus(-1) | ||
} | ||
|
||
func convertMALEntry(in []anilist.AnimeListEntry) []animelist.Entry { | ||
out := make([]animelist.Entry, 0, len(in)) | ||
for i := range in { | ||
titles := in[i].Media.Title | ||
out = append(out, animelist.Entry{ | ||
ListStatus: convertStatus(in[i].Status), | ||
Titles: []string{titles.English, titles.Romaji, titles.Native}, | ||
AiringStatus: convertAiringStatus(in[i].Media.AiringStatus), | ||
}) | ||
} | ||
return out | ||
} | ||
|
||
func (w *Wrapper) GetCurrentlyWatching(ctx context.Context) ([]animelist.Entry, error) { | ||
resp, err := w.client.GetCurrentlyWatching(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return convertMALEntry(resp), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.