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/rssHandler.go
akuma06 d75cb9f863 Separated Routes and handler from Main
Everything is in the router folder in different file
2017-05-05 16:39:15 +02:00

47 lignes
Pas d'EOL
1,1 Kio
Go

package router
import(
"time"
"net/http"
"github.com/gorilla/feeds"
"github.com/ewhal/nyaa/util/search"
)
func RssHandler(w http.ResponseWriter, r *http.Request) {
_, torrents, _ := search.SearchByQuery( r, 1 )
created_as_time := time.Now()
if len(torrents) > 0 {
created_as_time = time.Unix(torrents[0].Date, 0)
}
feed := &feeds.Feed{
Title: "Nyaa Pantsu",
Link: &feeds.Link{Href: "https://nyaa.pantsu.cat/"},
Created: created_as_time,
}
feed.Items = []*feeds.Item{}
feed.Items = make([]*feeds.Item, len(torrents))
for i, _ := range torrents {
timestamp_as_time := time.Unix(torrents[0].Date, 0)
torrent_json := torrents[i].ToJson()
feed.Items[i] = &feeds.Item{
// need a torrent view first
//Id: URL + torrents[i].Hash,
Title: torrents[i].Name,
Link: &feeds.Link{Href: string(torrent_json.Magnet)},
Description: "",
Created: timestamp_as_time,
Updated: timestamp_as_time,
}
}
rss, err := feed.ToRss()
if err == nil {
w.Write([]byte(rss))
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}