2017-05-05 16:39:15 +02:00
package search
2017-05-06 12:41:48 +02:00
import (
2017-05-05 16:39:15 +02:00
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util/log"
"html"
2017-05-06 12:41:48 +02:00
"net/http"
2017-05-05 16:39:15 +02:00
"strconv"
"strings"
2017-05-08 15:50:18 +02:00
"unicode"
"unicode/utf8"
2017-05-05 16:39:15 +02:00
)
type SearchParam struct {
2017-05-06 12:41:48 +02:00
Category string
Order string
Query string
Max int
Status string
Sort string
2017-05-05 16:39:15 +02:00
}
func SearchByQuery ( r * http . Request , pagenum int ) ( SearchParam , [ ] model . Torrents , int ) {
maxPerPage , errConv := strconv . Atoi ( r . URL . Query ( ) . Get ( "max" ) )
if errConv != nil {
maxPerPage = 50 // default Value maxPerPage
}
2017-05-06 12:41:48 +02:00
if maxPerPage > 300 {
maxPerPage = 300
}
2017-05-07 23:41:44 +02:00
search_param := SearchParam { }
2017-05-05 16:39:15 +02:00
search_param . Max = maxPerPage
search_param . Query = r . URL . Query ( ) . Get ( "q" )
search_param . Category = r . URL . Query ( ) . Get ( "c" )
search_param . Status = r . URL . Query ( ) . Get ( "s" )
search_param . Sort = r . URL . Query ( ) . Get ( "sort" )
search_param . Order = r . URL . Query ( ) . Get ( "order" )
catsSplit := strings . Split ( search_param . Category , "_" )
// need this to prevent out of index panics
var searchCatId , searchSubCatId string
if len ( catsSplit ) == 2 {
searchCatId = html . EscapeString ( catsSplit [ 0 ] )
searchSubCatId = html . EscapeString ( catsSplit [ 1 ] )
}
if search_param . Sort == "" {
search_param . Sort = "torrent_id"
}
if search_param . Order == "" {
search_param . Order = "desc"
}
order_by := search_param . Sort + " " + search_param . Order
parameters := torrentService . WhereParams { }
conditions := [ ] string { }
if searchCatId != "" {
conditions = append ( conditions , "category_id = ?" )
parameters . Params = append ( parameters . Params , searchCatId )
}
if searchSubCatId != "" {
conditions = append ( conditions , "sub_category_id = ?" )
parameters . Params = append ( parameters . Params , searchSubCatId )
}
if search_param . Status != "" {
2017-05-05 22:30:56 +02:00
if search_param . Status == "2" {
conditions = append ( conditions , "status_id != ?" )
} else {
conditions = append ( conditions , "status_id = ?" )
}
2017-05-05 16:39:15 +02:00
parameters . Params = append ( parameters . Params , search_param . Status )
}
2017-05-08 15:50:18 +02:00
searchQuerySplit := strings . Fields ( search_param . Query )
for i , word := range searchQuerySplit {
firstRune , _ := utf8 . DecodeRuneInString ( word )
if len ( word ) == 1 && unicode . IsPunct ( firstRune ) {
// some queries have a single punctuation character
// which causes a full scan instead of using the index
// and yields no meaningful results.
// due to len() == 1 we're just looking at 1-byte/ascii
// punctuation characters.
continue
}
2017-05-08 16:13:53 +02:00
conditions = append ( conditions , "torrent_name LIKE % ? %" )
2017-05-08 15:50:18 +02:00
parameters . Params = append ( parameters . Params , searchQuerySplit [ i ] )
2017-05-05 16:39:15 +02:00
}
parameters . Conditions = strings . Join ( conditions [ : ] , " AND " )
log . Infof ( "SQL query is :: %s\n" , parameters . Conditions )
2017-05-07 23:41:44 +02:00
torrents , n := torrentService . GetTorrentsOrderBy ( & parameters , order_by , maxPerPage , maxPerPage * ( pagenum - 1 ) )
return search_param , torrents , n
2017-05-05 16:39:15 +02:00
}