2017-05-05 16:39:15 +02:00
package router
2017-05-09 13:31:58 +02:00
import (
2017-05-05 16:39:15 +02:00
"net/http"
2017-05-09 13:31:58 +02:00
"strconv"
"time"
2017-05-07 15:19:36 +02:00
"github.com/ewhal/nyaa/config"
2017-05-09 13:31:58 +02:00
"github.com/ewhal/nyaa/util"
2017-05-05 16:39:15 +02:00
"github.com/ewhal/nyaa/util/search"
2017-05-09 13:31:58 +02:00
"github.com/gorilla/feeds"
2017-05-05 16:39:15 +02:00
)
func RssHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-09 17:07:42 +02:00
_ , torrents , err := search . SearchByQueryNoCount ( r , 1 )
2017-05-09 13:31:58 +02:00
if err != nil {
util . SendError ( w , err , 400 )
return
}
2017-05-05 16:39:15 +02:00
created_as_time := time . Now ( )
if len ( torrents ) > 0 {
2017-05-08 19:26:29 +02:00
created_as_time = torrents [ 0 ] . Date
2017-05-05 16:39:15 +02:00
}
feed := & feeds . Feed {
Title : "Nyaa Pantsu" ,
2017-05-07 15:19:36 +02:00
Link : & feeds . Link { Href : "https://" + config . WebAddress + "/" } ,
2017-05-05 16:39:15 +02:00
Created : created_as_time ,
}
feed . Items = [ ] * feeds . Item { }
feed . Items = make ( [ ] * feeds . Item , len ( torrents ) )
for i , _ := range torrents {
torrent_json := torrents [ i ] . ToJson ( )
feed . Items [ i ] = & feeds . Item {
// need a torrent view first
2017-05-08 19:26:29 +02:00
Id : "https://" + config . WebAddress + "/view/" + strconv . FormatUint ( uint64 ( torrents [ i ] . Id ) , 10 ) ,
2017-05-05 16:39:15 +02:00
Title : torrents [ i ] . Name ,
Link : & feeds . Link { Href : string ( torrent_json . Magnet ) } ,
Description : "" ,
2017-05-08 19:26:29 +02:00
Created : torrents [ 0 ] . Date ,
Updated : torrents [ 0 ] . Date ,
2017-05-05 16:39:15 +02:00
}
}
rss , err := feed . ToRss ( )
if err == nil {
w . Write ( [ ] byte ( rss ) )
} else {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
2017-05-07 15:19:36 +02:00
}