Basic fetching

This commit is contained in:
Uthman Fatih 2026-11-02 23:05:53 +00:00
parent 8a3e62366b
commit 443ababbd3
3 changed files with 96 additions and 8 deletions

View File

@ -41,7 +41,7 @@ collection:
meta:
id: req_1196ec001e1a482e9c3a2e7b876e04a7
created: 1793585402931
modified: 1793639349570
modified: 1793660143793
isPrivate: false
description: Fetches media title in romaji and english, alongside its airing
schedule for unreleased episodes
@ -71,7 +71,7 @@ collection:
meta:
id: req_893381a57f37423d9724e1a3e3069b82
created: 1793586482060
modified: 1793639263037
modified: 1793660041768
isPrivate: false
description: Get airing schedules of several anime via a list of Ids in one
batch (pageinated with maximum of 50 items per page)
@ -101,7 +101,7 @@ cookieJar:
meta:
id: jar_24b6e6da5159f879e56d5ae67427295cc9b7e66f
created: 1793583628974
modified: 1793639354386
modified: 1793660044533
cookies:
- id: 6fadc9b7-9fb4-4e01-9629-ad91f697912a
key: laravel_session

View File

@ -1,10 +1,17 @@
package fetch
package main
import (
"fmt"
"git.uthmn.com/boiabba/anical/internal/fetch/format"
"encoding/json"
)
func Sorry() {
fmt.Println(format.Sorry())
func main() {
resp, err := format.AiringSchedule(208025)
if err != nil {
fmt.Println(err)
}
var v map[string]any
json.Unmarshal(resp, &v)
fmt.Println(v)
}

View File

@ -1,5 +1,86 @@
package format
func Sorry() string {
return "Still working on it :P, sorry"
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
const anilistURL = "https://graphql.anilist.co"
var httpClient = &http.Client{Timeout: 10 * time.Second}
const batchAiringQuery = `
query ($mediaIds: [Int]) {
Page(page: 1, perPage: 50) {
media(id_in: $mediaIds) {
id
title { romaji english }
format
status
airingSchedule(notYetAired: true) {
nodes { episode airingAt timeUntilAiring }
}
}
}
}
`
const airingQuery = `
query ($mediaId: Int!) {
Media(id: $mediaId) {
id
title { romaji english }
format
status
airingSchedule(notYetAired: true) {
nodes { episode airingAt timeUntilAiring }
}
}
}
`
func postGraphQL(query string, variables map[string]any) ([]byte, error) {
payload, err := json.Marshal(map[string]any{
"query": query,
"variables": variables,
})
if err != nil {
return nil, fmt.Errorf("marshal request: %w", err)
}
req, err := http.NewRequest(http.MethodPost, anilistURL, bytes.NewReader(payload))
if err != nil {
return nil, fmt.Errorf("build request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("send request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("anilist: %s: %s", resp.Status, body)
}
return body, nil
}
// BatchAiringSchedule fetches schedules for up to 50 media IDs per call.
func BatchAiringSchedule(mediaIds []int32) ([]byte, error) {
return postGraphQL(batchAiringQuery, map[string]any{"mediaIds": mediaIds})
}
// AiringSchedule fetches the upcoming airing schedule for one media ID.
func AiringSchedule(mediaId int32) ([]byte, error) {
return postGraphQL(airingQuery, map[string]any{"mediaId": mediaId})
}