RSS feeds for specific search queries.
Cette révision appartient à :
Parent
a50d218504
révision
6304c447fd
2 fichiers modifiés avec 74 ajouts et 90 suppressions
135
main.go
135
main.go
|
@ -18,6 +18,15 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SearchParam struct {
|
||||||
|
Category string
|
||||||
|
Order string
|
||||||
|
Query string
|
||||||
|
Max int
|
||||||
|
Status string
|
||||||
|
Sort string
|
||||||
|
}
|
||||||
|
|
||||||
var db *gorm.DB
|
var db *gorm.DB
|
||||||
var router *mux.Router
|
var router *mux.Router
|
||||||
var debugLogger *log.Logger
|
var debugLogger *log.Logger
|
||||||
|
@ -108,38 +117,64 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
page := vars["page"]
|
page := vars["page"]
|
||||||
|
|
||||||
// db params url
|
// db params url
|
||||||
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
|
|
||||||
if errConv != nil {
|
|
||||||
maxPerPage = 50 // default Value maxPerPage
|
|
||||||
}
|
|
||||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||||
if pagenum == 0 {
|
if pagenum == 0 {
|
||||||
pagenum = 1
|
pagenum = 1
|
||||||
}
|
}
|
||||||
searchQuery := r.URL.Query().Get("q")
|
|
||||||
cat := r.URL.Query().Get("c")
|
b := []TorrentsJson{}
|
||||||
stat := r.URL.Query().Get("s")
|
|
||||||
sort := r.URL.Query().Get("sort")
|
search_param, torrents, nbTorrents := searchByQuery( r, pagenum )
|
||||||
order := r.URL.Query().Get("order")
|
|
||||||
|
|
||||||
catsSplit := strings.Split(cat, "_")
|
for i, _ := range torrents {
|
||||||
|
res := torrents[i].toJson()
|
||||||
|
b = append(b, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
navigationTorrents := Navigation{nbTorrents, search_param.Max, pagenum, "search_page"}
|
||||||
|
searchForm := SearchForm{
|
||||||
|
search_param.Query,
|
||||||
|
search_param.Status,
|
||||||
|
search_param.Category,
|
||||||
|
search_param.Sort,
|
||||||
|
search_param.Order,
|
||||||
|
}
|
||||||
|
htv := HomeTemplateVariables{b, getAllCategories(false), searchForm, navigationTorrents, r.URL, mux.CurrentRoute(r)}
|
||||||
|
|
||||||
|
err := templates.ExecuteTemplate(w, "index.html", htv)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func searchByQuery(r *http.Request, pagenum int) (SearchParam, []Torrents, int) {
|
||||||
|
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
|
||||||
|
if errConv != nil {
|
||||||
|
maxPerPage = 50 // default Value maxPerPage
|
||||||
|
}
|
||||||
|
|
||||||
|
search_param := SearchParam{}
|
||||||
|
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
|
// need this to prevent out of index panics
|
||||||
var searchCatId, searchSubCatId string
|
var searchCatId, searchSubCatId string
|
||||||
if len(catsSplit) == 2 {
|
if len(catsSplit) == 2 {
|
||||||
searchCatId = html.EscapeString(catsSplit[0])
|
searchCatId = html.EscapeString(catsSplit[0])
|
||||||
searchSubCatId = html.EscapeString(catsSplit[1])
|
searchSubCatId = html.EscapeString(catsSplit[1])
|
||||||
}
|
}
|
||||||
if sort == "" {
|
if search_param.Sort == "" {
|
||||||
sort = "torrent_id"
|
search_param.Sort = "torrent_id"
|
||||||
}
|
}
|
||||||
if order == "" {
|
if search_param.Order == "" {
|
||||||
order = "desc"
|
search_param.Order = "desc"
|
||||||
}
|
}
|
||||||
order_by := sort + " " + order
|
order_by := search_param.Sort + " " + search_param.Order
|
||||||
|
|
||||||
nbTorrents := 0
|
|
||||||
|
|
||||||
b := []TorrentsJson{}
|
|
||||||
|
|
||||||
parameters := WhereParams{}
|
parameters := WhereParams{}
|
||||||
conditions := []string{}
|
conditions := []string{}
|
||||||
|
@ -151,11 +186,11 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
conditions = append(conditions, "sub_category_id = ?")
|
conditions = append(conditions, "sub_category_id = ?")
|
||||||
parameters.params = append(parameters.params, searchSubCatId)
|
parameters.params = append(parameters.params, searchSubCatId)
|
||||||
}
|
}
|
||||||
if stat != "" {
|
if search_param.Status != "" {
|
||||||
conditions = append(conditions, "status_id = ?")
|
conditions = append(conditions, "status_id = ?")
|
||||||
parameters.params = append(parameters.params, stat)
|
parameters.params = append(parameters.params, search_param.Status)
|
||||||
}
|
}
|
||||||
searchQuerySplit := strings.Split(searchQuery, " ")
|
searchQuerySplit := strings.Split(search_param.Query, " ")
|
||||||
for i, _ := range searchQuerySplit {
|
for i, _ := range searchQuerySplit {
|
||||||
conditions = append(conditions, "torrent_name LIKE ?")
|
conditions = append(conditions, "torrent_name LIKE ?")
|
||||||
parameters.params = append(parameters.params, "%"+searchQuerySplit[i]+"%")
|
parameters.params = append(parameters.params, "%"+searchQuerySplit[i]+"%")
|
||||||
|
@ -163,22 +198,10 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
parameters.conditions = strings.Join(conditions[:], " AND ")
|
parameters.conditions = strings.Join(conditions[:], " AND ")
|
||||||
log.Printf("SQL query is :: %s\n", parameters.conditions)
|
log.Printf("SQL query is :: %s\n", parameters.conditions)
|
||||||
torrents, nbTorrents := getTorrentsOrderBy(¶meters, order_by, maxPerPage, maxPerPage*(pagenum-1))
|
torrents, n := getTorrentsOrderBy(¶meters, order_by, maxPerPage, maxPerPage*(pagenum-1))
|
||||||
|
return search_param, torrents, n
|
||||||
for i, _ := range torrents {
|
|
||||||
res := torrents[i].toJson()
|
|
||||||
b = append(b, res)
|
|
||||||
}
|
|
||||||
|
|
||||||
navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}
|
|
||||||
searchForm := SearchForm{searchQuery, stat, cat, sort, order}
|
|
||||||
htv := HomeTemplateVariables{b, getAllCategories(false), searchForm, navigationTorrents, r.URL, mux.CurrentRoute(r)}
|
|
||||||
|
|
||||||
err := templates.ExecuteTemplate(w, "index.html", htv)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func safe(s string) template.URL {
|
func safe(s string) template.URL {
|
||||||
return template.URL(s)
|
return template.URL(s)
|
||||||
}
|
}
|
||||||
|
@ -193,20 +216,10 @@ func faqHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func rssHandler(w http.ResponseWriter, r *http.Request) {
|
func rssHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
//vars := mux.Vars(r)
|
_, torrents, _ := searchByQuery( r, 1 )
|
||||||
//category := vars["c"]
|
created_as_time := time.Now()
|
||||||
|
|
||||||
// db params url
|
|
||||||
//maxPerPage := 50 // default Value maxPerPage
|
|
||||||
|
|
||||||
torrents := getFeeds()
|
|
||||||
created := time.Now().String()
|
|
||||||
if len(torrents) > 0 {
|
if len(torrents) > 0 {
|
||||||
created = torrents[0].Timestamp
|
created_as_time = time.Unix(torrents[0].Date, 0)
|
||||||
}
|
|
||||||
created_as_time, err := time.Parse("2006-01-02 15:04:05", created)
|
|
||||||
if err == nil {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
feed := &feeds.Feed{
|
feed := &feeds.Feed{
|
||||||
Title: "Nyaa Pantsu",
|
Title: "Nyaa Pantsu",
|
||||||
|
@ -217,17 +230,16 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
feed.Items = make([]*feeds.Item, len(torrents))
|
feed.Items = make([]*feeds.Item, len(torrents))
|
||||||
|
|
||||||
for i, _ := range torrents {
|
for i, _ := range torrents {
|
||||||
timestamp_as_time, err := time.Parse("2006-01-02 15:04:05", torrents[i].Timestamp)
|
timestamp_as_time := time.Unix(torrents[0].Date, 0)
|
||||||
if err == nil {
|
torrent_json := torrents[i].toJson()
|
||||||
feed.Items[i] = &feeds.Item{
|
feed.Items[i] = &feeds.Item{
|
||||||
// need a torrent view first
|
// need a torrent view first
|
||||||
//Id: URL + torrents[i].Hash,
|
//Id: URL + torrents[i].Hash,
|
||||||
Title: torrents[i].Name,
|
Title: torrents[i].Name,
|
||||||
Link: &feeds.Link{Href: string(torrents[i].Magnet)},
|
Link: &feeds.Link{Href: string(torrent_json.Magnet)},
|
||||||
Description: "",
|
Description: "",
|
||||||
Created: timestamp_as_time,
|
Created: timestamp_as_time,
|
||||||
Updated: timestamp_as_time,
|
Updated: timestamp_as_time,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,6 +250,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func viewHandler(w http.ResponseWriter, r *http.Request) {
|
func viewHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var templates = template.Must(template.ParseFiles("templates/index.html", "templates/view.html"))
|
var templates = template.Must(template.ParseFiles("templates/index.html", "templates/view.html"))
|
||||||
templates.ParseGlob("templates/_*.html") // common
|
templates.ParseGlob("templates/_*.html") // common
|
||||||
|
|
29
models.go
29
models.go
|
@ -10,14 +10,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Feed struct {
|
|
||||||
Id int
|
|
||||||
Name string
|
|
||||||
Hash string
|
|
||||||
Magnet string
|
|
||||||
Timestamp string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Categories struct {
|
type Categories struct {
|
||||||
Id int `gorm:"column:category_id"`
|
Id int `gorm:"column:category_id"`
|
||||||
Name string `gorm:"column:category_name"`
|
Name string `gorm:"column:category_name"`
|
||||||
|
@ -97,27 +89,6 @@ type WhereParams struct {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// don't need raw SQL once we get MySQL
|
|
||||||
func getFeeds() []Feed {
|
|
||||||
var result []Feed
|
|
||||||
rows, err := db.DB().
|
|
||||||
Query(
|
|
||||||
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " +
|
|
||||||
"ORDER BY `timestamp` desc LIMIT 50")
|
|
||||||
if err == nil {
|
|
||||||
for rows.Next() {
|
|
||||||
item := Feed{}
|
|
||||||
rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp)
|
|
||||||
magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(item.Hash) + "&dn=" + item.Name + trackers
|
|
||||||
item.Magnet = magnet
|
|
||||||
// memory hog
|
|
||||||
result = append(result, item)
|
|
||||||
}
|
|
||||||
rows.Close()
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTorrentById(id string) (Torrents, error) {
|
func getTorrentById(id string) (Torrents, error) {
|
||||||
var torrent Torrents
|
var torrent Torrents
|
||||||
|
|
||||||
|
|
Référencer dans un nouveau ticket