Skip to content

Commit

Permalink
v2.0.8: rework errors and playlist download
Browse files Browse the repository at this point in the history
  • Loading branch information
lostdusty committed Nov 25, 2024
1 parent 7197d4d commit d603254
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions gobalt.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ var (
"error.api.youtube.login": "youtube marked the processing server as a bot, tell the owner to check cookies",
"error.api.youtube.token_expired": "the youtube token expired, try again in a few seconds, but if it still doesn't work, tell the instance owner about this error",
"error.api.youtube.no_hls_streams": "the video you're trying to download doesn't have any HLS streams, try other settings",
"error.net.failed": "unable to connect to the cobalt server, check your internet connection, the server status, and try again",
"error.net.generic": "an unknown error occurred while connecting to the cobalt server.",
"error.net.invalid_response": "the cobalt server returned an invalid response, try again later",
}
)

Expand All @@ -232,21 +235,22 @@ type Context struct {
}

// Run(gobalt.Settings) sends the request to the provided cobalt api and returns the server response (gobalt.CobaltResponse) and error, use this to download something AFTER setting your desired configuration.
// Use ErrDescriptions to get a human-readable error message from the error code.
func Run(options Settings) (*CobaltResponse, error) {
//Check if an url is set.
if options.Url == "" {
return nil, errors.New("no url was provided in Settings.Url")
return nil, errors.New("no url was provided to download")
}

//Do a basic check to see if the server is online and handling requests
_, err := CobaltServerInfo(CobaltApi)
if err != nil {
return nil, fmt.Errorf("hello to cobalt instance %v failed, reason: %v", CobaltApi, err)
return nil, fmt.Errorf("error.net.generic: %v", err)
}

jsonBody, err := json.Marshal(options)
if err != nil {
return nil, fmt.Errorf("failed to marshal json body due of the following error: %v", err)
return nil, fmt.Errorf("error.net.invalid_response")
}

req, err := http.NewRequest(http.MethodPost, CobaltApi, strings.NewReader(string(jsonBody)))
Expand All @@ -260,23 +264,23 @@ func Run(options Settings) (*CobaltResponse, error) {

res, err := Client.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to send your request, %v", err)
return nil, fmt.Errorf("error.net.failed")
}
defer res.Body.Close()

jsonbody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
return nil, fmt.Errorf("error.net.invalid_response")
}

var media CobaltResponse
err = json.Unmarshal(jsonbody, &media)
if err != nil {
return nil, err
return nil, fmt.Errorf("error.net.invalid_response")
}

if media.Status == "error" {
return nil, fmt.Errorf("cobalt rejected our request with the following reason: %v (%v)", ErrDescriptions[media.Error.Code], media.Error.Code)
return nil, fmt.Errorf("%v", media.Error.Code)
}

return &media, nil
Expand Down Expand Up @@ -372,6 +376,7 @@ type MediaInfo struct {
}

// ProcessMedia(url) attempts to fetch the file size, mime type and name.
// Deprecated: Cobalt response returns the file name and size.
func ProcessMedia(url string) (*MediaInfo, error) {
req, err := genericHttpRequest(url, http.MethodHead, nil)
if err != nil {
Expand Down Expand Up @@ -408,9 +413,6 @@ func GetYoutubePlaylist(playlist string) (Playlist, error) {
if err != nil {
return nil, err
}
if !strings.HasSuffix(newYoutubePlaylistUrl.Host, "youtube.com") || newYoutubePlaylistUrl.Path != "/playlist" {
return nil, errors.New("non youtube playlist url provided")
}

getUrls, err := genericHttpRequest(fmt.Sprintf("https://playlist.kwiatekmiki.pl/api/getvideos?url=%v", newYoutubePlaylistUrl.String()), http.MethodGet, nil)
if err != nil {
Expand Down

0 comments on commit d603254

Please sign in to comment.