mirror of
https://github.com/gabehf/sonarr-anime-importer.git
synced 2026-03-13 09:20:27 -07:00
commit
d1022a172f
5 changed files with 43 additions and 15 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
sonarr-anime-importer
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
## syntax=docker/dockerfile:1
|
||||
FROM golang:1.23
|
||||
FROM golang:1.24
|
||||
WORKDIR /app
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
|
|
|||
20
anilist.go
20
anilist.go
|
|
@ -102,9 +102,13 @@ func handleAniListAnimeSearch(idMap *ConcurrentMap, permaSkipIds []string) http.
|
|||
search, err := getAniListAnimeSearch(idMap, permaSkipIds, r)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
if _, writeErr := w.Write([]byte(err.Error())); writeErr != nil {
|
||||
log.Printf("Error writing error response: %v", writeErr)
|
||||
}
|
||||
} else {
|
||||
w.Write(search)
|
||||
if _, writeErr := w.Write(search); writeErr != nil {
|
||||
log.Printf("Error writing response: %v", writeErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -187,7 +191,7 @@ func makeAniListApiCall(q url.Values) (*AniListApiResponse, error) {
|
|||
// Build the GraphQL request body
|
||||
variables := BuildGraphQLVariables(q)
|
||||
|
||||
body := map[string]interface{}{
|
||||
body := map[string]any{
|
||||
"query": anilistQuery,
|
||||
"variables": variables,
|
||||
}
|
||||
|
|
@ -201,7 +205,11 @@ func makeAniListApiCall(q url.Values) (*AniListApiResponse, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() {
|
||||
if closeErr := resp.Body.Close(); closeErr != nil {
|
||||
log.Printf("Error closing response body: %v", closeErr)
|
||||
}
|
||||
}()
|
||||
|
||||
respData := new(AniListApiResponse)
|
||||
err = json.NewDecoder(resp.Body).Decode(respData)
|
||||
|
|
@ -212,8 +220,8 @@ func makeAniListApiCall(q url.Values) (*AniListApiResponse, error) {
|
|||
}
|
||||
|
||||
// BuildGraphQLVariables converts URL query parameters into a GraphQL variables map.
|
||||
func BuildGraphQLVariables(params url.Values) map[string]interface{} {
|
||||
vars := make(map[string]interface{})
|
||||
func BuildGraphQLVariables(params url.Values) map[string]any {
|
||||
vars := make(map[string]any)
|
||||
|
||||
// Helper to convert comma-separated strings into slices
|
||||
parseList := func(key string) []string {
|
||||
|
|
|
|||
27
main.go
27
main.go
|
|
@ -20,11 +20,13 @@ type ResponseItem struct {
|
|||
AniListId int `json:"anilistId,omitempty"`
|
||||
TvdbId int `json:"tvdbId"`
|
||||
}
|
||||
|
||||
type AnimeEntry struct {
|
||||
TvdbId int `json:"tvdb_id"`
|
||||
MalId interface{} `json:"mal_id"`
|
||||
AniListId int `json:"anilist_id"`
|
||||
TvdbId int `json:"tvdb_id"`
|
||||
MalId any `json:"mal_id"`
|
||||
AniListId int `json:"anilist_id"`
|
||||
}
|
||||
|
||||
type ConcurrentMap struct {
|
||||
mal map[int]int
|
||||
mut sync.RWMutex
|
||||
|
|
@ -38,8 +40,10 @@ func (m *ConcurrentMap) GetByMalId(i int) int {
|
|||
|
||||
var lastBuiltAnimeIdList time.Time
|
||||
|
||||
const Version = "v0.2.1"
|
||||
|
||||
func main() {
|
||||
log.Println("sonarr-anime-importer v0.2.1")
|
||||
log.Printf("sonarr-anime-importer %s", Version)
|
||||
log.Println("Building Anime ID Associations...")
|
||||
var idMap = new(ConcurrentMap)
|
||||
buildIdMap(idMap)
|
||||
|
|
@ -57,7 +61,14 @@ func main() {
|
|||
http.HandleFunc("/v1/mal/anime", loggerMiddleware(buildIdMapMiddleware(handleMalAnimeSearch(idMap, permaSkipMalIds))))
|
||||
http.HandleFunc("/v1/anilist/anime", loggerMiddleware(buildIdMapMiddleware(handleAniListAnimeSearch(idMap, permaSkipAniListIds))))
|
||||
log.Println("Listening on :3333")
|
||||
log.Fatal(http.ListenAndServe(":3333", nil))
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":3333",
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
log.Fatal(srv.ListenAndServe())
|
||||
}
|
||||
|
||||
func buildIdMap(idMap *ConcurrentMap) {
|
||||
|
|
@ -69,7 +80,11 @@ func buildIdMap(idMap *ConcurrentMap) {
|
|||
if err != nil {
|
||||
log.Fatal("Error fetching anime_ids.json: ", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() {
|
||||
if closeErr := resp.Body.Close(); closeErr != nil {
|
||||
log.Printf("Error closing response body: %v", closeErr)
|
||||
}
|
||||
}()
|
||||
idListBytes, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal("Error reading anime_ids.json: ", err)
|
||||
|
|
|
|||
8
mal.go
8
mal.go
|
|
@ -17,9 +17,13 @@ func handleMalAnimeSearch(idMap *ConcurrentMap, permaSkipMalIds []string) http.H
|
|||
search, err := getJikanAnimeSearch(idMap, permaSkipMalIds, r)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
w.Write([]byte(err.Error()))
|
||||
if _, writeErr := w.Write([]byte(err.Error())); writeErr != nil {
|
||||
log.Printf("Error writing error response: %v", writeErr)
|
||||
}
|
||||
} else {
|
||||
w.Write([]byte(search))
|
||||
if _, writeErr := w.Write([]byte(search)); writeErr != nil {
|
||||
log.Printf("Error writing response: %v", writeErr)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue