mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Move functions into their own, separate files & add docs
This commit is contained in:
parent
d1859b4c25
commit
ec872d8c86
12 changed files with 681 additions and 638 deletions
45
watch_history.go
Normal file
45
watch_history.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package crunchyroll
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// WatchHistory returns the history of watched episodes based on the currently logged in account from the given page with the given size.
|
||||
func (c *Crunchyroll) WatchHistory(page uint, size uint) (e []*HistoryEpisode, err error) {
|
||||
watchHistoryEndpoint := fmt.Sprintf("https://beta-api.crunchyroll.com/content/v1/watch-history/%s?page=%d&page_size=%d&locale=%s",
|
||||
c.Config.AccountID, page, size, c.Locale)
|
||||
resp, err := c.request(watchHistoryEndpoint, http.MethodGet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var jsonBody map[string]interface{}
|
||||
if err = json.NewDecoder(resp.Body).Decode(&jsonBody); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse 'watch-history' response: %w", err)
|
||||
}
|
||||
|
||||
for _, item := range jsonBody["items"].([]interface{}) {
|
||||
panel := item.(map[string]interface{})["panel"]
|
||||
|
||||
episode := &Episode{
|
||||
crunchy: c,
|
||||
}
|
||||
if err := decodeMapToStruct(panel, episode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
historyEpisode := &HistoryEpisode{
|
||||
Episode: episode,
|
||||
}
|
||||
if err := decodeMapToStruct(item, historyEpisode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
e = append(e, historyEpisode)
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue