add searching via the api
Cette révision appartient à :
Parent
7398ce2f1f
révision
bb7554b79a
2 fichiers modifiés avec 39 ajouts et 1 suppressions
|
@ -18,6 +18,7 @@ import (
|
|||
"github.com/NyaaPantsu/nyaa/service/upload"
|
||||
"github.com/NyaaPantsu/nyaa/util"
|
||||
"github.com/NyaaPantsu/nyaa/util/log"
|
||||
"github.com/NyaaPantsu/nyaa/util/search"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
|
@ -261,3 +262,38 @@ func ApiUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Printf("%+v\n", torrent)
|
||||
}
|
||||
}
|
||||
func ApiSearchHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
page := vars["page"]
|
||||
|
||||
// db params url
|
||||
var err error
|
||||
pagenum := 1
|
||||
if page != "" {
|
||||
pagenum, err = strconv.Atoi(html.EscapeString(page))
|
||||
if !log.CheckError(err) {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if pagenum <= 0 {
|
||||
NotFoundHandler(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
_, torrents, _, err := search.SearchByQuery(r, pagenum)
|
||||
if err != nil {
|
||||
util.SendError(w, err, 400)
|
||||
return
|
||||
}
|
||||
|
||||
b := model.TorrentsToJSON(torrents)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(b)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -48,6 +48,8 @@ func init() {
|
|||
Router.Handle("/api/view/{id}", wrapHandler(gzipAPIViewHandler)).Methods("GET")
|
||||
Router.HandleFunc("/api/view/{id}", ApiViewHeadHandler).Methods("HEAD")
|
||||
Router.HandleFunc("/api/upload", ApiUploadHandler).Methods("POST")
|
||||
Router.HandleFunc("/api/search", ApiSearchHandler)
|
||||
Router.HandleFunc("/api/search/{page}", ApiSearchHandler)
|
||||
Router.HandleFunc("/api/update", ApiUpdateHandler).Methods("PUT")
|
||||
Router.HandleFunc("/faq", FaqHandler).Name("faq")
|
||||
Router.HandleFunc("/feed", RSSHandler).Name("feed")
|
||||
|
@ -95,7 +97,7 @@ func init() {
|
|||
Router.HandleFunc("/mod/comment/delete", WrapModHandler(CommentDeleteModPanel)).Name("mod_cdelete")
|
||||
Router.HandleFunc("/mod/reassign", WrapModHandler(TorrentReassignModPanel)).Name("mod_treassign").Methods("GET")
|
||||
Router.HandleFunc("/mod/reassign", WrapModHandler(TorrentPostReassignModPanel)).Name("mod_treassign").Methods("POST")
|
||||
Router.HandleFunc("/mod/api/torrents", WrapModHandler(ApiMassMod)).Name("mod_tapi").Methods("POST")
|
||||
Router.HandleFunc("/mod/api/torrents", WrapModHandler(ApiMassMod)).Name("mod_tapi").Methods("POST")
|
||||
|
||||
//reporting a torrent
|
||||
Router.HandleFunc("/report/{id}", ReportTorrentHandler).Methods("POST").Name("torrent_report")
|
||||
|
|
Référencer dans un nouveau ticket