Basic fetching
This commit is contained in:
parent
8a3e62366b
commit
443ababbd3
@ -41,7 +41,7 @@ collection:
|
|||||||
meta:
|
meta:
|
||||||
id: req_1196ec001e1a482e9c3a2e7b876e04a7
|
id: req_1196ec001e1a482e9c3a2e7b876e04a7
|
||||||
created: 1793585402931
|
created: 1793585402931
|
||||||
modified: 1793639349570
|
modified: 1793660143793
|
||||||
isPrivate: false
|
isPrivate: false
|
||||||
description: Fetches media title in romaji and english, alongside its airing
|
description: Fetches media title in romaji and english, alongside its airing
|
||||||
schedule for unreleased episodes
|
schedule for unreleased episodes
|
||||||
@ -71,7 +71,7 @@ collection:
|
|||||||
meta:
|
meta:
|
||||||
id: req_893381a57f37423d9724e1a3e3069b82
|
id: req_893381a57f37423d9724e1a3e3069b82
|
||||||
created: 1793586482060
|
created: 1793586482060
|
||||||
modified: 1793639263037
|
modified: 1793660041768
|
||||||
isPrivate: false
|
isPrivate: false
|
||||||
description: Get airing schedules of several anime via a list of Ids in one
|
description: Get airing schedules of several anime via a list of Ids in one
|
||||||
batch (pageinated with maximum of 50 items per page)
|
batch (pageinated with maximum of 50 items per page)
|
||||||
@ -101,7 +101,7 @@ cookieJar:
|
|||||||
meta:
|
meta:
|
||||||
id: jar_24b6e6da5159f879e56d5ae67427295cc9b7e66f
|
id: jar_24b6e6da5159f879e56d5ae67427295cc9b7e66f
|
||||||
created: 1793583628974
|
created: 1793583628974
|
||||||
modified: 1793639354386
|
modified: 1793660044533
|
||||||
cookies:
|
cookies:
|
||||||
- id: 6fadc9b7-9fb4-4e01-9629-ad91f697912a
|
- id: 6fadc9b7-9fb4-4e01-9629-ad91f697912a
|
||||||
key: laravel_session
|
key: laravel_session
|
||||||
|
|||||||
@ -1,10 +1,17 @@
|
|||||||
package fetch
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.uthmn.com/boiabba/anical/internal/fetch/format"
|
"git.uthmn.com/boiabba/anical/internal/fetch/format"
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Sorry() {
|
func main() {
|
||||||
fmt.Println(format.Sorry())
|
resp, err := format.AiringSchedule(208025)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
var v map[string]any
|
||||||
|
json.Unmarshal(resp, &v)
|
||||||
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
@ -1,5 +1,86 @@
|
|||||||
package format
|
package format
|
||||||
|
|
||||||
func Sorry() string {
|
import (
|
||||||
return "Still working on it :P, sorry"
|
"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})
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user