Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
djrandom
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ale
djrandom
Commits
7d214247
Commit
7d214247
authored
10 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
add a limit parameter to the search API
parent
24ac7c3e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
client/mpd/djrandom.go
+3
-0
3 additions, 0 deletions
client/mpd/djrandom.go
server/frontend/api_views.go
+45
-26
45 additions, 26 deletions
server/frontend/api_views.go
with
48 additions
and
26 deletions
client/mpd/djrandom.go
+
3
−
0
View file @
7d214247
...
...
@@ -10,6 +10,7 @@ import (
"log"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
...
...
@@ -23,6 +24,7 @@ import (
var
(
configFile
=
flag
.
String
(
"djrandom-config"
,
util
.
ExpandTilde
(
"~/.djrandom.conf"
),
"Config file location"
)
maxResults
=
flag
.
Int
(
"max-search-results"
,
200
,
"maximum number of search results to return"
)
serverUrl
=
config
.
String
(
"djrandom-server"
,
"https://djrandom.incal.net"
,
"Server URL"
)
authKeyId
=
config
.
String
(
"auth_key"
,
""
,
"API authentication key"
)
...
...
@@ -182,6 +184,7 @@ func (d *DJRandomDatabase) doQuery(query string) ([]mpd.Song, error) {
values
:=
url
.
Values
{}
values
.
Add
(
"q"
,
query
)
values
.
Add
(
"limit"
,
strconv
.
Itoa
(
*
maxResults
))
req
,
err
:=
d
.
client
.
NewRequest
(
"POST"
,
"/api/search"
,
strings
.
NewReader
(
values
.
Encode
()))
if
err
!=
nil
{
return
nil
,
err
...
...
This diff is collapsed.
Click to expand it.
server/frontend/api_views.go
+
45
−
26
View file @
7d214247
...
...
@@ -2,6 +2,7 @@ package frontend
import
(
"encoding/json"
"errors"
// "fmt"
"io"
"io/ioutil"
...
...
@@ -182,49 +183,67 @@ func CheckFingerprints(w http.ResponseWriter, r *djRequest) {
sendJsonResponse
(
w
,
&
fpResp
)
}
// SearchIds runs a search query and returns only song IDs.
func
SearchIds
(
w
http
.
ResponseWriter
,
r
*
djRequest
)
{
// Assemble the search query.
func
doSearch
(
r
*
djRequest
)
([]
api
.
SongID
,
error
)
{
// Extract query parameters from the request.
query
:=
r
.
Request
.
FormValue
(
"q"
)
if
query
==
""
{
http
.
Error
(
w
,
"Empty search query"
,
http
.
StatusBadRequest
)
return
return
nil
,
errors
.
New
(
"empty search query"
)
}
var
limit
int
limitstr
:=
r
.
Request
.
FormValue
(
"limit"
)
if
limitstr
!=
""
{
var
err
error
limit
,
err
=
strconv
.
Atoi
(
limitstr
)
if
err
!=
nil
{
return
nil
,
err
}
}
// Run search.
var
resp
api
.
SearchIdsResponse
for
item
,
_
:=
range
r
.
Ctx
.
Index
.
Search
(
query
)
{
resp
.
Results
=
append
(
resp
.
Results
,
item
)
// Run search, only return 'limit' results (if 0, return all
// of them). Try to use the least memory possible.
results
:=
r
.
Ctx
.
Index
.
Search
(
query
)
if
limit
==
0
{
limit
=
len
(
results
)
}
sendJsonResponse
(
w
,
&
resp
)
songIds
:=
make
([]
api
.
SongID
,
0
,
limit
)
i
:=
0
for
id
:=
range
results
{
songIds
=
append
(
songIds
,
id
)
i
++
if
i
>
limit
{
break
}
}
return
songIds
,
nil
}
// Search returns full search results.
func
Search
(
w
http
.
ResponseWriter
,
r
*
djRequest
)
{
// Assemble the search query.
query
:=
r
.
Request
.
FormValue
(
"q"
)
if
query
==
""
{
http
.
Error
(
w
,
"Empty search query"
,
http
.
StatusBadRequest
)
// SearchIds runs a search query and returns only song IDs.
func
SearchIds
(
w
http
.
ResponseWriter
,
r
*
djRequest
)
{
ids
,
err
:=
doSearch
(
r
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
return
}
sendJsonResponse
(
w
,
&
api
.
SearchIdsResponse
{
Results
:
ids
})
}
// Run search (and create a list).
results
:=
r
.
Ctx
.
Index
.
Search
(
query
)
songIds
:=
make
([]
api
.
SongID
,
0
,
len
(
results
))
for
id
,
_
:=
range
results
{
songIds
=
append
(
songIds
,
id
)
// Search returns full search results.
func
Search
(
w
http
.
ResponseWriter
,
r
*
djRequest
)
{
ids
,
err
:=
doSearch
(
r
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
return
}
songs
,
err
:=
db_client
.
ParallelFetchSongs
(
r
.
Ctx
.
Db
,
songI
ds
)
songs
,
err
:=
db_client
.
ParallelFetchSongs
(
r
.
Ctx
.
Db
,
i
ds
)
if
err
!=
nil
{
log
.
Printf
(
"Search(): %s"
,
err
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
resp
:=
api
.
SearchResponse
{
Results
:
songs
,
}
sendJsonResponse
(
w
,
&
resp
)
sendJsonResponse
(
w
,
&
api
.
SearchResponse
{
Results
:
songs
})
}
// ArtistAutocomplete returns artist autocompletion results.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment