2017-05-05 16:39:15 +02:00
package router
2017-05-09 13:31:58 +02:00
import (
2017-06-15 15:43:18 +02:00
"errors"
2017-05-25 21:54:58 +02:00
"html"
"net/http"
"strconv"
"time"
2017-06-15 21:54:06 +02:00
"sort"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/config"
2017-06-15 15:43:18 +02:00
"github.com/NyaaPantsu/nyaa/model"
2017-05-20 01:09:09 +02:00
userService "github.com/NyaaPantsu/nyaa/service/user"
2017-06-15 21:54:06 +02:00
"github.com/NyaaPantsu/nyaa/util/categories"
2017-06-15 15:43:18 +02:00
"github.com/NyaaPantsu/nyaa/util/feeds"
2017-06-15 21:54:06 +02:00
"github.com/NyaaPantsu/nyaa/util/publicSettings"
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util/search"
2017-06-14 08:36:38 +02:00
"github.com/gorilla/feeds"
2017-05-17 07:58:40 +02:00
"github.com/gorilla/mux"
2017-05-05 16:39:15 +02:00
)
2017-05-25 21:54:58 +02:00
// RSSHandler : Controller for displaying rss feed, accepting common search arguments
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
func RSSHandler ( w http . ResponseWriter , r * http . Request ) {
2017-05-27 03:50:31 +02:00
defer r . Body . Close ( )
2017-06-15 15:43:18 +02:00
// We only get the basic variable for rss based on search param
torrents , createdAsTime , title , err := getTorrentList ( r )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
feed := & nyaafeeds . RssFeed {
Title : title ,
Link : config . WebAddress ( ) + "/" ,
PubDate : createdAsTime . String ( ) ,
}
feed . Items = make ( [ ] * nyaafeeds . RssItem , len ( torrents ) )
for i , torrent := range torrents {
torrentJSON := torrent . ToJSON ( )
feed . Items [ i ] = & nyaafeeds . RssItem {
Title : torrentJSON . Name ,
Link : config . WebAddress ( ) + "/view/" + strconv . FormatUint ( uint64 ( torrentJSON . ID ) , 10 ) ,
Description : string ( torrentJSON . Description ) ,
Author : config . WebAddress ( ) + "/view/" + strconv . FormatUint ( uint64 ( torrentJSON . ID ) , 10 ) ,
PubDate : torrent . Date . String ( ) ,
GUID : config . WebAddress ( ) + "/download/" + torrentJSON . Hash ,
Enclosure : & nyaafeeds . RssEnclosure {
URL : config . WebAddress ( ) + "/download/" + torrentJSON . Hash ,
Length : strconv . FormatUint ( uint64 ( torrentJSON . Filesize ) , 10 ) ,
Type : "application/x-bittorrent" ,
} ,
}
}
// allow cross domain AJAX requests
w . Header ( ) . Set ( "Access-Control-Allow-Origin" , "*" )
rss , rssErr := feeds . ToXML ( feed )
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 )
}
}
// RSSEztvHandler : Controller for displaying rss feed, accepting common search arguments
func RSSEztvHandler ( w http . ResponseWriter , r * http . Request ) {
defer r . Body . Close ( )
// We only get the basic variable for rss based on search param
torrents , createdAsTime , title , err := getTorrentList ( r )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
feed := & nyaafeeds . RssFeed {
Title : title ,
Link : config . WebAddress ( ) + "/" ,
PubDate : createdAsTime . String ( ) ,
}
feed . Items = make ( [ ] * nyaafeeds . RssItem , len ( torrents ) )
for i , torrent := range torrents {
torrentJSON := torrent . ToJSON ( )
feed . Items [ i ] = & nyaafeeds . RssItem {
Title : torrentJSON . Name ,
Link : config . WebAddress ( ) + "/download/" + torrentJSON . Hash ,
Category : & nyaafeeds . RssCategory {
Domain : config . WebAddress ( ) + "/search?c=" + torrentJSON . Category + "_" + torrentJSON . SubCategory ,
} ,
Description : string ( torrentJSON . Description ) ,
Comments : config . WebAddress ( ) + "/view/" + strconv . FormatUint ( uint64 ( torrentJSON . ID ) , 10 ) ,
PubDate : torrent . Date . String ( ) ,
GUID : config . WebAddress ( ) + "/view/" + strconv . FormatUint ( uint64 ( torrentJSON . ID ) , 10 ) ,
Enclosure : & nyaafeeds . RssEnclosure {
URL : config . WebAddress ( ) + "/download/" + torrentJSON . Hash ,
Length : strconv . FormatUint ( uint64 ( torrentJSON . Filesize ) , 10 ) ,
Type : "application/x-bittorrent" ,
} ,
Torrent : & nyaafeeds . RssTorrent {
Xmlns : "http://xmlns.ezrss.it/0.1/" ,
FileName : torrentJSON . Name + ".torrent" ,
ContentLength : strconv . FormatUint ( uint64 ( torrentJSON . Filesize ) , 10 ) ,
InfoHash : torrentJSON . Hash ,
MagnetURI : string ( torrentJSON . Magnet ) ,
} ,
}
}
// allow cross domain AJAX requests
w . Header ( ) . Set ( "Access-Control-Allow-Origin" , "*" )
rss , rssErr := feeds . ToXML ( feed )
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 )
}
}
2017-06-15 21:54:06 +02:00
// RSSTorznabHandler : Controller for displaying rss feed, accepting common search arguments
2017-06-15 15:43:18 +02:00
func RSSTorznabHandler ( w http . ResponseWriter , r * http . Request ) {
defer r . Body . Close ( )
2017-06-15 21:54:06 +02:00
t := r . URL . Query ( ) . Get ( "t" )
rss := ""
title := "Nyaa Pantsu"
if config . IsSukebei ( ) {
title = "Sukebei Pantsu"
2017-06-15 15:43:18 +02:00
}
2017-06-15 21:54:06 +02:00
if t == "caps" {
T := publicSettings . GetTfuncFromRequest ( r )
2017-06-17 04:24:30 +02:00
cat := categories . GetCategoriesSelect ( true , true )
2017-06-15 21:54:06 +02:00
var categories [ ] * nyaafeeds . RssCategoryTorznab
var keys [ ] string
for name := range cat {
keys = append ( keys , name )
}
sort . Strings ( keys )
last := - 1
for _ , key := range keys {
if len ( cat [ key ] ) <= 2 {
categories = append ( categories , & nyaafeeds . RssCategoryTorznab {
ID : nyaafeeds . ConvertFromCat ( cat [ key ] ) ,
Name : string ( T ( key ) ) ,
} )
last ++
} else {
categories [ last ] . Subcat = append ( categories [ last ] . Subcat , & nyaafeeds . RssSubCat {
ID : nyaafeeds . ConvertFromCat ( cat [ key ] ) ,
Name : string ( T ( key ) ) ,
} )
}
}
feed := & nyaafeeds . RssCaps {
Server : & nyaafeeds . RssServer {
Version : "1.0" ,
Title : title ,
Strapline : "..." ,
Email : config . Conf . Email . From ,
URL : config . WebAddress ( ) ,
Image : config . WebAddress ( ) + "/img/logo.png" ,
2017-06-15 15:43:18 +02:00
} ,
2017-06-15 21:54:06 +02:00
Limits : & nyaafeeds . RssLimits {
Max : "300" ,
Default : "50" ,
} ,
Registration : & nyaafeeds . RssRegistration {
Available : "yes" ,
Open : "yes" ,
2017-06-15 15:43:18 +02:00
} ,
2017-06-15 21:54:06 +02:00
Searching : & nyaafeeds . RssSearching {
Search : & nyaafeeds . RssSearch {
Available : "yes" ,
SupportedParams : "q" ,
} ,
TvSearch : & nyaafeeds . RssSearch {
Available : "no" ,
} ,
MovieSearch : & nyaafeeds . RssSearch {
Available : "no" ,
} ,
2017-06-15 15:43:18 +02:00
} ,
2017-06-15 21:54:06 +02:00
Categories : & nyaafeeds . RssCategories {
Category : categories ,
} ,
}
var rssErr error
rss , rssErr = feeds . ToXML ( feed )
if rssErr != nil {
http . Error ( w , rssErr . Error ( ) , http . StatusInternalServerError )
}
} else {
// We only get the basic variable for rss based on search param
torrents , createdAsTime , title , err := getTorrentList ( r )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
feed := & nyaafeeds . RssFeed {
Title : title ,
Link : config . WebAddress ( ) + "/" ,
PubDate : createdAsTime . String ( ) ,
}
feed . Items = make ( [ ] * nyaafeeds . RssItem , len ( torrents ) )
for i , torrent := range torrents {
torrentJSON := torrent . ToJSON ( )
feed . Items [ i ] = & nyaafeeds . RssItem {
Title : torrentJSON . Name ,
Link : config . WebAddress ( ) + "/download/" + torrentJSON . Hash ,
Category : & nyaafeeds . RssCategory {
Domain : config . WebAddress ( ) + "/search?c=" + torrentJSON . Category + "_" + torrentJSON . SubCategory ,
} ,
Description : string ( torrentJSON . Description ) ,
Comments : config . WebAddress ( ) + "/view/" + strconv . FormatUint ( uint64 ( torrentJSON . ID ) , 10 ) ,
PubDate : torrent . Date . String ( ) ,
GUID : config . WebAddress ( ) + "/view/" + strconv . FormatUint ( uint64 ( torrentJSON . ID ) , 10 ) ,
Enclosure : & nyaafeeds . RssEnclosure {
URL : config . WebAddress ( ) + "/download/" + torrentJSON . Hash ,
Length : strconv . FormatUint ( uint64 ( torrentJSON . Filesize ) , 10 ) ,
Type : "application/x-bittorrent" ,
} ,
Torznab : & nyaafeeds . RssTorznab {
Xmlns : "http://torznab.com/schemas/2015/feed" ,
Size : strconv . FormatUint ( uint64 ( torrentJSON . Filesize ) , 10 ) ,
Files : strconv . Itoa ( len ( torrentJSON . FileList ) ) ,
Grabs : strconv . Itoa ( torrentJSON . Downloads ) ,
Seeders : strconv . Itoa ( int ( torrentJSON . Seeders ) ) ,
Leechers : strconv . Itoa ( int ( torrentJSON . Leechers ) ) ,
Infohash : torrentJSON . Hash ,
MagnetURL : string ( torrentJSON . Magnet ) ,
} ,
}
}
var rssErr error
rss , rssErr = feeds . ToXML ( feed )
if rssErr != nil {
http . Error ( w , rssErr . Error ( ) , http . StatusInternalServerError )
2017-06-15 15:43:18 +02:00
}
}
// allow cross domain AJAX requests
w . Header ( ) . Set ( "Access-Control-Allow-Origin" , "*" )
_ , writeErr := w . Write ( [ ] byte ( rss ) )
if writeErr != nil {
http . Error ( w , writeErr . Error ( ) , http . StatusInternalServerError )
}
}
func getTorrentList ( r * http . Request ) ( torrents [ ] model . Torrent , createdAsTime time . Time , title string , err error ) {
2017-05-16 23:32:41 +02:00
vars := mux . Vars ( r )
page := vars [ "page" ]
2017-05-20 01:09:09 +02:00
userID := vars [ "id" ]
2017-06-15 21:54:06 +02:00
cat := r . URL . Query ( ) . Get ( "cat" )
2017-05-17 07:58:40 +02:00
2017-06-15 12:48:38 +02:00
offset := r . URL . Query ( ) . Get ( "offset" )
2017-05-16 23:32:41 +02:00
pagenum := 1
2017-06-15 12:48:38 +02:00
if page == "" && offset != "" {
page = offset
}
2017-05-16 23:32:41 +02:00
if page != "" {
pagenum , err = strconv . Atoi ( html . EscapeString ( page ) )
if err != nil {
return
}
2017-05-20 01:10:16 +02:00
if pagenum <= 0 {
2017-06-15 15:43:18 +02:00
err = errors . New ( "Page number is invalid" )
2017-05-20 01:10:16 +02:00
return
}
2017-05-16 23:32:41 +02:00
}
2017-05-20 01:09:09 +02:00
if userID != "" {
2017-06-15 15:43:18 +02:00
userIDnum := 0
userIDnum , err = strconv . Atoi ( html . EscapeString ( userID ) )
2017-05-20 01:09:09 +02:00
// Should we have a feed for anonymous uploads?
if err != nil || userIDnum == 0 {
return
}
_ , _ , err = userService . RetrieveUserForAdmin ( userID )
if err != nil {
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 ( )
}
2017-06-15 21:54:06 +02:00
if cat != "" {
query := r . URL . Query ( )
c , sub := nyaafeeds . ConvertToCat ( cat )
query . Set ( "c" , c + "_" + sub )
}
2017-06-15 15:43:18 +02:00
_ , torrents , err = search . SearchByQueryNoCount ( r , pagenum )
createdAsTime = time . Now ( )
2017-05-05 16:39:15 +02:00
if len ( torrents ) > 0 {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
createdAsTime = torrents [ 0 ] . Date
2017-05-05 16:39:15 +02:00
}
2017-06-15 15:43:18 +02:00
title = "Nyaa Pantsu"
2017-06-08 20:08:30 +02:00
if config . IsSukebei ( ) {
title = "Sukebei Pantsu"
}
2017-05-05 16:39:15 +02:00
2017-06-15 15:43:18 +02:00
return
2017-05-07 15:19:36 +02:00
}