68 lignes
1,6 Kio
Go
68 lignes
1,6 Kio
Go
package router
|
|
|
|
import (
|
|
"github.com/NyaaPantsu/nyaa/config"
|
|
"github.com/NyaaPantsu/nyaa/util"
|
|
"github.com/NyaaPantsu/nyaa/util/search"
|
|
"github.com/gorilla/feeds"
|
|
"github.com/gorilla/mux"
|
|
"html"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func RSSHandler(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
page := vars["page"]
|
|
|
|
var err error
|
|
pagenum := 1
|
|
if page != "" {
|
|
pagenum, err = strconv.Atoi(html.EscapeString(page))
|
|
if err != nil {
|
|
util.SendError(w, err, 400)
|
|
return
|
|
}
|
|
}
|
|
|
|
_, torrents, err := search.SearchByQueryNoCount(r, pagenum)
|
|
if err != nil {
|
|
util.SendError(w, err, 400)
|
|
return
|
|
}
|
|
createdAsTime := time.Now()
|
|
|
|
if len(torrents) > 0 {
|
|
createdAsTime = torrents[0].Date
|
|
}
|
|
feed := &feeds.Feed{
|
|
Title: "Nyaa Pantsu",
|
|
Link: &feeds.Link{Href: "https://" + config.WebAddress + "/"},
|
|
Created: createdAsTime,
|
|
}
|
|
feed.Items = make([]*feeds.Item, len(torrents))
|
|
|
|
for i, torrent := range torrents {
|
|
torrentJSON := torrent.ToJSON()
|
|
feed.Items[i] = &feeds.Item{
|
|
Id: "https://" + config.WebAddress + "/view/" + torrentJSON.ID,
|
|
Title: torrent.Name,
|
|
Link: &feeds.Link{Href: string(torrentJSON.Magnet)},
|
|
Description: string(torrentJSON.Description),
|
|
Created: torrent.Date,
|
|
Updated: torrent.Date,
|
|
}
|
|
}
|
|
// allow cross domain AJAX requests
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
rss, rssErr := feed.ToRss()
|
|
if rssErr != nil {
|
|
http.Error(w, rssErr.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
_, writeErr := w.Write([]byte(rss))
|
|
if writeErr != nil {
|
|
http.Error(w, writeErr.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|