Note
This is the version 2 of the gobalt library, intended for interecting with cobalt recent version (v10.0.0 and up). If you're upgrading from v1, note that there some breaking changes.
This version IS NOT compatible with cobalt previous version (v7.15 and lower), use the v1 version if you want to use the older api, it will be maintained until 31/December/2024.
Gobalt provides a way to communicate with cobalt.tools using Go. To use it in your projects, simply run this command:
go get -u github.com/lostdusty/gobalt/v2
First, make sure you call gobalt.CreateDefaultSettings()
to create the Settings
struct with default values, then set an url.
//Creates a Settings struct with default values, and save it to downloadMedia variable.
downloadMedia := gobalt.CreateDefaultSettings()
//Sets the URL, you MUST set one before downloading the media.
downloadMedia.Url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
//After changing the url, Do() will make the necessary requests to cobalt to download your media
destination, err := gobalt.Do(downloadMedia)
if err != nil {
//Handle errors here
}
//Prints out the url from cobalt to download the requested media.
fmt.Println(destination.URL)
//Output example: https://us4-co.wuk.sh/api/stream?t=wTn-71aaWAcV2RBejNFNV&e=1711777798864&h=fMtwwtW8AUmtTLB24DGjJJjrq1EJDBFaCDoDuZpX0pA&s=_YVZhz8fnzBBKKo7UZmGWOqfe4wWwH5P1azdgBqwf-I&i=6tGZqAXbW08_6KmAiLevZA
You can query information about any cobalt server by using CobaltServerInfo(apiurl)
. This will return a ServerInfo
struct with this info.
Example code:
server, err := gobalt.CobaltServerInfo(gobalt.CobaltApi)
if err != nil {
//Handle the error here
}
fmt.Printf("Downloading from %v!\n", server.URL)
//Output: Downloading from https://us4-co.wuk.sh/!
Using GetCobaltInstances()
fetches a community maintaned list of third-party cobalt instances ran by the community, except for *-co.wuk.sh
, none of them are "official" cobalt instances. Use them if you can't download from the main instance for whatever reason.
Example:
cobalt, err := gobalt.GetCobaltInstances()
if err != nil {
//Handle errors here
}
fmt.Printf("Found %v online cobalt servers: ", len(cobalt))
for _, value := range cobalt {
fmt.Printf("%v, ", value.URL)
}
/* Output:
* Found 8 online cobalt servers: co.wuk.sh, cobalt-api.hyper.lol, cobalt.api.timelessnesses.me, api-dl.cgm.rs, cobalt.synzr.space, capi.oak.li, co.tskau.team, api.co.rooot.gay
*/