Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/router/rss_handler.go

103 lignes
2,5 Kio
Go
Brut Vue normale Historique

package router
import (
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/config"
"github.com/NyaaPantsu/nyaa/feeds"
userService "github.com/NyaaPantsu/nyaa/service/user"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util"
"github.com/NyaaPantsu/nyaa/util/search"
"github.com/gorilla/mux"
"html"
"net/http"
"strconv"
"time"
)
func RSSHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
userID := vars["id"]
2017-05-17 07:58:40 +02:00
var err error
pagenum := 1
if page != "" {
pagenum, err = strconv.Atoi(html.EscapeString(page))
if err != nil {
util.SendError(w, err, 400)
return
}
if pagenum <= 0 {
NotFoundHandler(w, r)
return
}
}
if userID != "" {
userIDnum, err := strconv.Atoi(html.EscapeString(userID))
// Should we have a feed for anonymous uploads?
if err != nil || userIDnum == 0 {
util.SendError(w, err, 400)
return
}
_, _, err = userService.RetrieveUserForAdmin(userID)
if err != nil {
util.SendError(w, err, 404)
return
}
// Set the user ID on the request, so that SearchByQuery finds it.
query := r.URL.Query()
query.Set("userID", userID)
r.URL.RawQuery = query.Encode()
}
_, 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",
2017-05-07 15:19:36 +02:00
Link: &feeds.Link{Href: "https://" + config.WebAddress + "/"},
Created: createdAsTime,
}
feed.Items = make([]*feeds.Item, len(torrents))
2017-05-11 23:35:51 +02:00
for i, torrent := range torrents {
torrentJSON := torrent.ToJSON()
feed.Items[i] = &feeds.Item{
2017-05-12 17:54:08 +02:00
Id: "https://" + config.WebAddress + "/view/" + torrentJSON.ID,
2017-05-11 23:35:51 +02:00
Title: torrent.Name,
Link: &feeds.Link{Href: string(torrentJSON.Magnet)},
2017-05-11 15:50:26 +02:00
Description: string(torrentJSON.Description),
2017-05-11 23:35:51 +02:00
Created: torrent.Date,
Updated: torrent.Date,
Torrent: &feeds.Torrent{
FileName: torrent.Name,
Seeds: torrent.Seeders,
Peers: torrent.Leechers,
InfoHash: torrent.Hash,
ContentLength: torrent.Filesize,
MagnetURI: string(torrentJSON.Magnet),
},
}
}
2017-05-11 23:35:51 +02:00
// allow cross domain AJAX requests
2017-05-11 15:50:26 +02:00
w.Header().Set("Access-Control-Allow-Origin", "*")
rss, rssErr := feed.ToRss()
if rssErr != nil {
2017-05-12 16:29:40 +02:00
http.Error(w, rssErr.Error(), http.StatusInternalServerError)
}
_, writeErr := w.Write([]byte(rss))
if writeErr != nil {
2017-05-12 16:29:40 +02:00
http.Error(w, writeErr.Error(), http.StatusInternalServerError)
}
2017-05-07 15:19:36 +02:00
}