mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 04:02:00 -06:00
Added library
parent
89e38a3c27
commit
68c35eec0a
1 changed files with 120 additions and 0 deletions
120
Library.md
Normal file
120
Library.md
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
**This page contains some more information about the basic use of the library and some of its functions.**
|
||||
|
||||
## Documentation
|
||||
|
||||
The documentation is available on [pkg.go.dev](https://pkg.go.dev/github.com/ByteDream/crunchyroll-go).
|
||||
|
||||
## Login
|
||||
|
||||
### With username / email and Password
|
||||
|
||||
The most convenient way to login is for sure with the actual account information.
|
||||
|
||||
1. `user` - The username or password of the account over which the api requests should be made.
|
||||
2. `password` - The accounts' password.
|
||||
3. `language` - A (library defined) `LOCALE` constant which defines in which language all resulting information like episode titles should be.
|
||||
4. `client` - A `http.Client` where all api requests and download should be transferred over.
|
||||
|
||||
```go
|
||||
crunchy, err := crunchyroll.LoginWithCredentials("crunchy@example.com", "password", crunchyroll.US, http.DefaultClient)
|
||||
```
|
||||
|
||||
### With Session ID
|
||||
|
||||
Beside the normal account credentials, session ids, which are generated whenever a client logs in, can be used as well.
|
||||
They can be extract when you're logged in with the api and call `Crunchyroll.SessionID`.
|
||||
On the actual crunchyroll website the id is stored in the `session_id` cookie on a logged-in account.
|
||||
|
||||
The syntax is very similar to the of authentication via username and password, except that instead of username and password only the session id is required.
|
||||
```go
|
||||
crunchy, err := crunchyroll.LoginWithSessionID("sessionid", crunchyroll.US, http.DefaultClient)
|
||||
```
|
||||
|
||||
## Parse urls
|
||||
|
||||
You will mostly get in touch with urls when you want to get information about a specific episode or series.
|
||||
It is a huge pain to parse urls manually because two types of urls existing: classic and beta urls.
|
||||
They have a very different layout, and it takes up a huge space of code to cover all url types.
|
||||
For this case, two methods are provided:
|
||||
- `Crunchyroll.ParseUrl`
|
||||
|
||||
This method can parse any crunchyroll url and returns either a `*crunchyroll.Season` struct or an array of `*crunchyroll.Episode`.
|
||||
If `*crunchyroll.Season` has a value, the `*crunchyroll.Episode` array has none and vice versa.
|
||||
```go
|
||||
season, episode, err := crunchy.ParseUrl("https://beta.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx")
|
||||
```
|
||||
- `Crunchyroll.ExtractEpisodesFromUrl`
|
||||
|
||||
It is also possible that you only want to get the episodes from and url and not a possible season struct.
|
||||
This method allows this, with the nice extra that you can filter in which languages the returning episodes should be.
|
||||
```go
|
||||
episodes, err := crunchy.ExtractEpisodesFromUrl("https://beta.crunchyroll.com/series/GY8VEQ95Y/darling-in-the-franxx", crunchyroll.JP)
|
||||
```
|
||||
|
||||
## Search
|
||||
|
||||
If [url parsing](#parse-urls) doesn't fit your need, you can also search episodes and series by a pattern / name.
|
||||
```go
|
||||
series, movies, err := crunchy.Search("Darling in the FranXX", 20)
|
||||
```
|
||||
This will search "Darling in the FranXX" (just like if you would search it on the website) and return the 20 first results as arrays of `*crunchyroll.Series` and / or `*crunchroll.Movie`.
|
||||
|
||||
## Structs
|
||||
|
||||
The following list contains the fundamental structs on which you perform all actions.
|
||||
Every struct contains a struct function which gets the next "lower" / children struct(s).
|
||||
So `crunchyroll.Season` has a `Episodes()` function which returns all of its episodes as array of `*crunchyroll.Episode`, `crunchyroll.Episode` has a `Streams()` function and so on.
|
||||
|
||||
- `crunchyroll.Season`: Detailed information about a season.
|
||||
- `crunchyroll.Episode`: Detailed information about the episode.
|
||||
- `crunchyroll.Stream`: Information about the video, like audio locale and subtitles.
|
||||
- `crunchyroll.Format`: The actual format to [download](#download), contains all video information like resolution and fps.
|
||||
|
||||
## Download
|
||||
|
||||
Downloading can be archived via a struct function of `crunchyroll.Format`: `Download(...)`.
|
||||
This downloads, obviously, the format. It takes a `crunchyroll.Downloader` struct as argument where detailed information how to download can be specified.
|
||||
A simple `Downloader` struct which will fit the most needs can be created with the `crunchyroll.NewDownloader(...)` function.
|
||||
You can specify an `io.Writer` where the video should be downloaded to, how many goroutines should be used for parallel segment downloading or if you want to merge the video via ffmpeg and may specify some ffmpeg options.
|
||||
To see all functions of the `Downloader` struct check the [documentation](https://pkg.go.dev/github.com/ByteDream/crunchyroll-go#Downloader).
|
||||
|
||||
Just a quick dive in: Crunchyroll uses [mpeg transport streams](https://en.wikipedia.org/wiki/MPEG_transport_stream) to deliver videos.
|
||||
This means an episode is split into multiple tiny video files (generally around 718 - 721) which are referred as _segments_ here.
|
||||
|
||||
## Caching
|
||||
|
||||
The api has a simple built-in caching system.
|
||||
It may sometimes happen that methods which requesting api data are called multiple times and normally the data would be transferred multiple times without any change on it.
|
||||
And to bypass this, caching is available.
|
||||
It simply caches the result of an api request and when the method where the data was cached is called again, the cached items gets returned without making a new (unnecessary) api request.
|
||||
|
||||
It is activated by default but can be disabled by calling
|
||||
```go
|
||||
crunchy.SetCaching(false)
|
||||
```
|
||||
|
||||
## Utils
|
||||
|
||||
An utils package is also provided for functions which does not fit into the root library.
|
||||
|
||||
### Sorting
|
||||
|
||||
Some sorting algorithms for the native go sort function `sort.Sort(...)` is available.
|
||||
With `utils.FormatsByResolution` you can sort an array of `*crunchyroll.Format` by their resolution or with `utils.EpisodesByNumber` an array of `*crunchyroll.Episode` by their episode number.
|
||||
For a full list see the [documentation](#documentation).
|
||||
|
||||
The `utils.SortEpisodesBySeason(...)` function sorts the as argument passed array of `*crunchyroll.Episode` by their season number and episode number.
|
||||
When using this, only episodes with the same audio locale should be used.
|
||||
But if the array of episodes contains multiple languages it is recommended to sort it before by their language before.
|
||||
See the following paragraph for a function which does exactly that.
|
||||
|
||||
`utils.SortEpisodesByAudio` sorts an array of `*crunchyroll.Episode` by the audio locales of the individual episodes.
|
||||
It returns a map where the keys are the audio locales and the keys the corresponding episodes.
|
||||
|
||||
|
||||
### Locale
|
||||
|
||||
Some locale helper are also included.
|
||||
`utils.AllLocales` provides an array of all locales, which may be helpful if you want to print out all available locales or so.
|
||||
`utils.ValidateLocale(...)` validates if the given input locale is really a supported locale by the api.
|
||||
`utils.LocaleLanguage(...)` returns the human-readable name of the locale.
|
||||
Loading…
Add table
Add a link
Reference in a new issue