-
Notifications
You must be signed in to change notification settings - Fork 1
[API-84] Add track /stream /download /inspect #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/url" | ||
| "time" | ||
|
|
||
| "bridgerton.audius.co/api/dbv1" | ||
| ) | ||
|
|
||
| // tryFindWorkingUrl attempts to validate a media link by checking if it can serve content. | ||
| // It tries the primary URL first, then falls back to mirrors if needed. | ||
| // Returns the first valid URL found or the main URL if nothing works. | ||
| func tryFindWorkingUrl(mediaLink *dbv1.MediaLink) *url.URL { | ||
| mainURL, err := url.Parse(mediaLink.Url) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| // Construct all URLs to try | ||
| urls := make([]*url.URL, 0, len(mediaLink.Mirrors)+1) | ||
| urls = append(urls, mainURL) | ||
| for _, mirror := range mediaLink.Mirrors { | ||
| mirrorURL := *mainURL | ||
| mirrorURL.Host = mirror | ||
| urls = append(urls, &mirrorURL) | ||
| } | ||
|
|
||
| client := &http.Client{ | ||
| Timeout: 5 * time.Second, | ||
| } | ||
| for _, u := range urls { | ||
| q := u.Query() | ||
| q.Set("skip_play_count", "true") | ||
| u.RawQuery = q.Encode() | ||
|
|
||
| req, err := http.NewRequest("GET", u.String(), nil) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| req.Header.Set("Range", "bytes=0-1") | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| resp.Body.Close() | ||
|
|
||
| if resp.StatusCode == http.StatusPartialContent || | ||
| resp.StatusCode == http.StatusOK || | ||
| resp.StatusCode == http.StatusNoContent { | ||
| return u | ||
| } | ||
| } | ||
|
|
||
| return mainURL | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| track_id,genre,owner_id,title,is_unlisted,stream_conditions,download_conditions | ||
| 100,Electronic,1,T1,f,, | ||
| 101,Alternative,1,T2,f,, | ||
| 200,Electronic,2,Culca Canyon,f,, | ||
| 201,Alternative,2,Turkey Time DEMO,t,, | ||
| 202,Alternative,2,Turkey Time (live),f,, | ||
| 300,Electronic,3,Follow Gated Download,f,,"{""follow_user_id"": 3}" | ||
| 301,Electronic,3,Pay Gated Download,f,,"{""usdc_purchase"": {""price"": 135, ""splits"": [{""user_id"": 3, ""percentage"": 100.0}]}}" | ||
| 302,Electronic,3,Tip Gated Stream,f,"{""tip_user_id"": 3}","{""tip_user_id"": 3}" | ||
| 303,Electronic,3,Pay Gated Stream,f,"{""usdc_purchase"": {""price"": 135, ""splits"": [{""user_id"": 3, ""percentage"": 100.0}]}}", | ||
| 400,Folk,5,Trending Month Folk,f,, | ||
| 500,Experimental,6,track by permalink,f,, | ||
| track_id,genre,owner_id,title,is_unlisted,stream_conditions,download_conditions,is_downloadable | ||
| 100,Electronic,1,T1,f,,,t | ||
| 101,Alternative,1,T2,f,,,f | ||
| 200,Electronic,2,Culca Canyon,f,,,f | ||
| 201,Alternative,2,Turkey Time DEMO,t,,,f | ||
| 202,Alternative,2,Turkey Time (live),f,,,f | ||
| 300,Electronic,3,Follow Gated Download,f,,"{""follow_user_id"": 3}",t | ||
| 301,Electronic,3,Pay Gated Download,f,,"{""usdc_purchase"": {""price"": 135, ""splits"": [{""user_id"": 3, ""percentage"": 100.0}]}}",t | ||
| 302,Electronic,3,Tip Gated Stream,f,"{""tip_user_id"": 3}","{""tip_user_id"": 3}",f | ||
| 303,Electronic,3,Pay Gated Stream,f,"{""usdc_purchase"": {""price"": 135, ""splits"": [{""user_id"": 3, ""percentage"": 100.0}]}}",,f | ||
| 400,Folk,5,Trending Month Folk,f,,,f | ||
| 500,Experimental,6,track by permalink,f,,,f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "bridgerton.audius.co/api/dbv1" | ||
| "github.com/gofiber/fiber/v2" | ||
| ) | ||
|
|
||
| func (app *ApiServer) v1TrackDownload(c *fiber.Ctx) error { | ||
| myId := app.getMyId(c) | ||
| trackId := c.Locals("trackId").(int) | ||
| filename := c.Query("filename") | ||
|
|
||
| tracks, err := app.queries.FullTracks(c.Context(), dbv1.GetTracksParams{ | ||
| MyID: myId, | ||
| Ids: []int32{int32(trackId)}, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(tracks) == 0 { | ||
| return sendError(c, 404, "track not found") | ||
| } | ||
|
|
||
| track := tracks[0] | ||
| if !track.Access.Download { | ||
| return sendError(c, 403, "track not downloadable") | ||
| } | ||
|
|
||
| downloadUrl := tryFindWorkingUrl(track.Download) | ||
|
|
||
| q := downloadUrl.Query() | ||
| q.Set("skip_play_count", "true") | ||
| if filename != "" { | ||
| q.Set("filename", filename) | ||
| } | ||
| downloadUrl.RawQuery = q.Encode() | ||
|
|
||
| return c.Redirect(downloadUrl.String(), fiber.StatusFound) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGetTrackDownload(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/v1/tracks/eYZmn/download", nil) | ||
| res, err := app.Test(req, -1) | ||
| assert.NoError(t, err) | ||
| assert.Contains(t, res.Header.Get("Location"), "https://dummynode.com/tracks/cidstream/?signature=%7B%22data%22%3A%22%7B%5C%22cid%5C%22%3A%5C%22%5C%22%2C%5C%22timestamp%5C%22%3") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to keep doing this or just send it to the first one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we do. I went back & forth for a while, but for people streaming from the public API until we have proportional rewards, etc., to promise a really high QoS - i think about lots of scenarios where the stream endpoint "just doesn't work"
e.g. node de-registers & content is still shuffling around
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have the mirrors now though, so this code is much nicer than python