Merge branch 'master' into responsive-table
Cette révision appartient à :
révision
7288ca8e93
16 fichiers modifiés avec 397 ajouts et 311 suppressions
|
@ -35,10 +35,6 @@ body {
|
|||
padding: 0;
|
||||
}
|
||||
|
||||
/*.blockBody {
|
||||
padding: 1rem;
|
||||
}*/
|
||||
|
||||
.torrentNav {
|
||||
text-align: center;
|
||||
}
|
||||
|
@ -76,7 +72,7 @@ a {
|
|||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.torrent-info .hash {
|
||||
.torrent-hash {
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
|
@ -90,6 +86,14 @@ a {
|
|||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.table > tbody > tr > th, .table > tbody > tr > td {
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
tr.torrent-info td.date {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.custom-table-hover > tbody > tr:hover {
|
||||
opacity: 0.82;
|
||||
}
|
||||
|
@ -99,7 +103,6 @@ div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:first-of-ty
|
|||
div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:nth-of-type(3){width:15%;}
|
||||
div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:nth-of-type(4){width:19%;}
|
||||
div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:last-of-type{width:6%;}
|
||||
tr.torrent-info td.hash,tr.torrent-info td.date{word-wrap:break-word;}
|
||||
|
||||
/* Mobile-friendly main table */
|
||||
@media only screen and (max-width: 700px) {
|
||||
|
|
296
main.go
Fichier normal → Fichier exécutable
296
main.go
Fichier normal → Fichier exécutable
|
@ -1,307 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"bufio"
|
||||
"flag"
|
||||
"github.com/gorilla/feeds"
|
||||
"github.com/gorilla/mux"
|
||||
"fmt"
|
||||
|
||||
"github.com/ewhal/nyaa/model"
|
||||
"github.com/ewhal/nyaa/service/torrent"
|
||||
"github.com/ewhal/nyaa/util/log"
|
||||
"github.com/ewhal/nyaa/config"
|
||||
"github.com/ewhal/nyaa/router"
|
||||
"github.com/ewhal/nyaa/util/log"
|
||||
|
||||
"html"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var router *mux.Router
|
||||
type SearchParam struct {
|
||||
Category string
|
||||
Order string
|
||||
Query string
|
||||
Max int
|
||||
Status string
|
||||
Sort string
|
||||
}
|
||||
|
||||
func apiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
vars := mux.Vars(r)
|
||||
page := vars["page"]
|
||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||
|
||||
b := model.CategoryJson{Torrents: []model.TorrentsJson{}}
|
||||
maxPerPage := 50
|
||||
nbTorrents := 0
|
||||
|
||||
torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
|
||||
for i, _ := range torrents {
|
||||
res := torrents[i].ToJson()
|
||||
b.Torrents = append(b.Torrents, res)
|
||||
}
|
||||
|
||||
b.QueryRecordCount = maxPerPage
|
||||
b.TotalRecordCount = nbTorrents
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(b)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func apiViewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
b := model.CategoryJson{Torrents: []model.TorrentsJson{}}
|
||||
|
||||
torrent, err := torrentService.GetTorrentById(id)
|
||||
res := torrent.ToJson()
|
||||
b.Torrents = append(b.Torrents, res)
|
||||
|
||||
b.QueryRecordCount = 1
|
||||
b.TotalRecordCount = 1
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(b)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func searchHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var templates = template.Must(template.New("home").Funcs(funcMap).ParseFiles("templates/index.html", "templates/home.html"))
|
||||
templates.ParseGlob("templates/_*.html") // common
|
||||
vars := mux.Vars(r)
|
||||
page := vars["page"]
|
||||
|
||||
// db params url
|
||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||
if pagenum == 0 {
|
||||
pagenum = 1
|
||||
}
|
||||
|
||||
b := []model.TorrentsJson{}
|
||||
|
||||
search_param, torrents, nbTorrents := searchByQuery( r, pagenum )
|
||||
|
||||
for i, _ := range torrents {
|
||||
res := torrents[i].ToJson()
|
||||
b = append(b, res)
|
||||
}
|
||||
|
||||
navigationTorrents := Navigation{nbTorrents, search_param.Max, pagenum, "search_page"}
|
||||
searchForm := SearchForm{
|
||||
search_param.Query,
|
||||
search_param.Status,
|
||||
search_param.Category,
|
||||
search_param.Sort,
|
||||
search_param.Order,
|
||||
false,
|
||||
}
|
||||
htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), searchForm, navigationTorrents, r.URL, mux.CurrentRoute(r)}
|
||||
|
||||
err := templates.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func searchByQuery(r *http.Request, pagenum int) (SearchParam, []model.Torrents, int) {
|
||||
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
|
||||
if errConv != nil {
|
||||
maxPerPage = 50 // default Value maxPerPage
|
||||
}
|
||||
|
||||
search_param := SearchParam{}
|
||||
search_param.Max = maxPerPage
|
||||
search_param.Query = r.URL.Query().Get("q")
|
||||
search_param.Category = r.URL.Query().Get("c")
|
||||
search_param.Status = r.URL.Query().Get("s")
|
||||
search_param.Sort = r.URL.Query().Get("sort")
|
||||
search_param.Order = r.URL.Query().Get("order")
|
||||
|
||||
catsSplit := strings.Split(search_param.Category, "_")
|
||||
// need this to prevent out of index panics
|
||||
var searchCatId, searchSubCatId string
|
||||
if len(catsSplit) == 2 {
|
||||
|
||||
searchCatId = html.EscapeString(catsSplit[0])
|
||||
searchSubCatId = html.EscapeString(catsSplit[1])
|
||||
}
|
||||
if search_param.Sort == "" {
|
||||
search_param.Sort = "torrent_id"
|
||||
}
|
||||
if search_param.Order == "" {
|
||||
search_param.Order = "desc"
|
||||
}
|
||||
order_by := search_param.Sort + " " + search_param.Order
|
||||
|
||||
parameters := torrentService.WhereParams{}
|
||||
conditions := []string{}
|
||||
if searchCatId != "" {
|
||||
conditions = append(conditions, "category_id = ?")
|
||||
parameters.Params = append(parameters.Params, searchCatId)
|
||||
}
|
||||
if searchSubCatId != "" {
|
||||
conditions = append(conditions, "sub_category_id = ?")
|
||||
parameters.Params = append(parameters.Params, searchSubCatId)
|
||||
}
|
||||
if search_param.Status != "" {
|
||||
if search_param.Status == "2" {
|
||||
conditions = append(conditions, "status_id != ?")
|
||||
} else {
|
||||
conditions = append(conditions, "status_id = ?")
|
||||
}
|
||||
parameters.Params = append(parameters.Params, search_param.Status)
|
||||
}
|
||||
searchQuerySplit := strings.Split(search_param.Query, " ")
|
||||
for i, _ := range searchQuerySplit {
|
||||
conditions = append(conditions, "torrent_name LIKE ?")
|
||||
parameters.Params = append(parameters.Params, "%"+searchQuerySplit[i]+"%")
|
||||
}
|
||||
|
||||
parameters.Conditions = strings.Join(conditions[:], " AND ")
|
||||
log.Infof("SQL query is :: %s\n", parameters.Conditions)
|
||||
torrents, n := torrentService.GetTorrentsOrderBy(¶meters, order_by, maxPerPage, maxPerPage*(pagenum-1))
|
||||
return search_param, torrents, n
|
||||
}
|
||||
|
||||
func faqHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var templates = template.Must(template.New("FAQ").Funcs(funcMap).ParseFiles("templates/index.html", "templates/FAQ.html"))
|
||||
templates.ParseGlob("templates/_*.html") // common
|
||||
|
||||
searchForm := NewSearchForm()
|
||||
searchForm.HideAdvancedSearch = true
|
||||
err := templates.ExecuteTemplate(w, "index.html", FaqTemplateVariables{Navigation{}, searchForm, r.URL, mux.CurrentRoute(r)})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func rssHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
_, torrents, _ := 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{
|
||||
Id: "https://nyaa.pantsu.cat/view/" + strconv.Itoa(torrents[i].Id),
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func viewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var templates = template.Must(template.ParseFiles("templates/index.html", "templates/view.html"))
|
||||
templates.ParseGlob("templates/_*.html") // common
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
torrent, err := torrentService.GetTorrentById(id)
|
||||
b := torrent.ToJson()
|
||||
|
||||
htv := ViewTemplateVariables{b, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
|
||||
|
||||
err = templates.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var templates = template.Must(template.New("home").Funcs(funcMap).ParseFiles("templates/index.html", "templates/home.html"))
|
||||
templates.ParseGlob("templates/_*.html") // common
|
||||
vars := mux.Vars(r)
|
||||
page := vars["page"]
|
||||
|
||||
// db params url
|
||||
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
|
||||
if errConv != nil {
|
||||
maxPerPage = 50 // default Value maxPerPage
|
||||
}
|
||||
|
||||
nbTorrents := 0
|
||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||
if pagenum == 0 {
|
||||
pagenum = 1
|
||||
}
|
||||
|
||||
b := []model.TorrentsJson{}
|
||||
torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
|
||||
|
||||
for i, _ := range torrents {
|
||||
res := torrents[i].ToJson()
|
||||
b = append(b, res)
|
||||
}
|
||||
|
||||
navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}
|
||||
htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), NewSearchForm(), navigationTorrents, r.URL, mux.CurrentRoute(r)}
|
||||
|
||||
err := templates.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func RunServer(conf *config.Config) {
|
||||
router = mux.NewRouter()
|
||||
|
||||
cssHandler := http.FileServer(http.Dir("./css/"))
|
||||
jsHandler := http.FileServer(http.Dir("./js/"))
|
||||
imgHandler := http.FileServer(http.Dir("./img/"))
|
||||
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
|
||||
http.Handle("/js/", http.StripPrefix("/js/", jsHandler))
|
||||
http.Handle("/img/", http.StripPrefix("/img/", imgHandler))
|
||||
|
||||
// Routes,
|
||||
router.HandleFunc("/", rootHandler).Name("home")
|
||||
router.HandleFunc("/page/{page:[0-9]+}", rootHandler).Name("home_page")
|
||||
router.HandleFunc("/search", searchHandler).Name("search")
|
||||
router.HandleFunc("/search/{page}", searchHandler).Name("search_page")
|
||||
router.HandleFunc("/api/{page}", apiHandler).Methods("GET")
|
||||
router.HandleFunc("/api/view/{id}", apiViewHandler).Methods("GET")
|
||||
router.HandleFunc("/faq", faqHandler).Name("faq")
|
||||
router.HandleFunc("/feed.xml", rssHandler)
|
||||
router.HandleFunc("/view/{id}", viewHandler).Name("view_torrent")
|
||||
|
||||
http.Handle("/", router)
|
||||
http.Handle("/", router.Router)
|
||||
|
||||
// Set up server,
|
||||
srv := &http.Server{
|
||||
|
|
|
@ -28,7 +28,7 @@ type Categories struct {
|
|||
|
||||
type Sub_Categories struct {
|
||||
Id int `gorm:"column:sub_category_id"`
|
||||
Name string `gorm:"column:Sub_category_name"`
|
||||
Name string `gorm:"column:sub_category_name"`
|
||||
Parent_id int `gorm:"column:parent_id"`
|
||||
Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"`
|
||||
}
|
||||
|
|
59
router/apiHandler.go
Fichier normal
59
router/apiHandler.go
Fichier normal
|
@ -0,0 +1,59 @@
|
|||
package router
|
||||
|
||||
import(
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"html"
|
||||
"strconv"
|
||||
"github.com/ewhal/nyaa/model"
|
||||
"github.com/ewhal/nyaa/service/torrent"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func ApiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
vars := mux.Vars(r)
|
||||
page := vars["page"]
|
||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||
|
||||
b := model.CategoryJson{Torrents: []model.TorrentsJson{}}
|
||||
maxPerPage := 50
|
||||
nbTorrents := 0
|
||||
|
||||
torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
|
||||
for i, _ := range torrents {
|
||||
res := torrents[i].ToJson()
|
||||
b.Torrents = append(b.Torrents, res)
|
||||
}
|
||||
|
||||
b.QueryRecordCount = maxPerPage
|
||||
b.TotalRecordCount = nbTorrents
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(b)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func ApiViewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
b := model.CategoryJson{Torrents: []model.TorrentsJson{}}
|
||||
|
||||
torrent, err := torrentService.GetTorrentById(id)
|
||||
res := torrent.ToJson()
|
||||
b.Torrents = append(b.Torrents, res)
|
||||
|
||||
b.QueryRecordCount = 1
|
||||
b.TotalRecordCount = 1
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(b)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
19
router/faqHandler.go
Fichier normal
19
router/faqHandler.go
Fichier normal
|
@ -0,0 +1,19 @@
|
|||
package router
|
||||
|
||||
|
||||
import(
|
||||
"net/http"
|
||||
"html/template"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func FaqHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var templates = template.Must(template.New("FAQ").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/FAQ.html"))
|
||||
templates.ParseGlob("templates/_*.html") // common
|
||||
searchForm := NewSearchForm()
|
||||
searchForm.HideAdvancedSearch = true
|
||||
err := templates.ExecuteTemplate(w, "index.html", FaqTemplateVariables{Navigation{}, searchForm, r.URL, mux.CurrentRoute(r)})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
47
router/homeHandler.go
Fichier normal
47
router/homeHandler.go
Fichier normal
|
@ -0,0 +1,47 @@
|
|||
package router
|
||||
|
||||
import(
|
||||
"net/http"
|
||||
"html/template"
|
||||
"github.com/gorilla/mux"
|
||||
"html"
|
||||
"strconv"
|
||||
"github.com/ewhal/nyaa/model"
|
||||
"github.com/ewhal/nyaa/service/torrent"
|
||||
)
|
||||
|
||||
func HomeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var templates = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/home.html"))
|
||||
templates.ParseGlob("templates/_*.html") // common
|
||||
vars := mux.Vars(r)
|
||||
page := vars["page"]
|
||||
|
||||
// db params url
|
||||
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
|
||||
if errConv != nil {
|
||||
maxPerPage = 50 // default Value maxPerPage
|
||||
}
|
||||
|
||||
nbTorrents := 0
|
||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||
if pagenum == 0 {
|
||||
pagenum = 1
|
||||
}
|
||||
|
||||
b := []model.TorrentsJson{}
|
||||
torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
|
||||
|
||||
for i, _ := range torrents {
|
||||
res := torrents[i].ToJson()
|
||||
b = append(b, res)
|
||||
}
|
||||
|
||||
navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}
|
||||
htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), NewSearchForm(), navigationTorrents, r.URL, mux.CurrentRoute(r)}
|
||||
|
||||
err := templates.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
}
|
31
router/router.go
Fichier normal
31
router/router.go
Fichier normal
|
@ -0,0 +1,31 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var Router *mux.Router
|
||||
|
||||
func init() {
|
||||
Router = mux.NewRouter()
|
||||
|
||||
cssHandler := http.FileServer(http.Dir("./css/"))
|
||||
jsHandler := http.FileServer(http.Dir("./js/"))
|
||||
imgHandler := http.FileServer(http.Dir("./img/"))
|
||||
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
|
||||
http.Handle("/js/", http.StripPrefix("/js/", jsHandler))
|
||||
http.Handle("/img/", http.StripPrefix("/img/", imgHandler))
|
||||
|
||||
// Routes,
|
||||
Router.HandleFunc("/", HomeHandler).Name("home")
|
||||
Router.HandleFunc("/page/{page:[0-9]+}", HomeHandler).Name("home_page")
|
||||
Router.HandleFunc("/search", SearchHandler).Name("search")
|
||||
Router.HandleFunc("/search/{page}", SearchHandler).Name("search_page")
|
||||
Router.HandleFunc("/api/{page}", ApiHandler).Methods("GET")
|
||||
Router.HandleFunc("/api/view/{id}", ApiViewHandler).Methods("GET")
|
||||
Router.HandleFunc("/faq", FaqHandler).Name("faq")
|
||||
Router.HandleFunc("/feed", RssHandler).Name("feed")
|
||||
Router.HandleFunc("/view/{id}", ViewHandler).Name("view_torrent")
|
||||
}
|
47
router/rssHandler.go
Fichier normal
47
router/rssHandler.go
Fichier normal
|
@ -0,0 +1,47 @@
|
|||
package router
|
||||
|
||||
import(
|
||||
"time"
|
||||
"net/http"
|
||||
"github.com/gorilla/feeds"
|
||||
"github.com/ewhal/nyaa/util/search"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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: "https://nyaa.pantsu.cat/view/" + strconv.Itoa(torrents[i].Id),
|
||||
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)
|
||||
}
|
||||
}
|
50
router/searchHandler.go
Fichier normal
50
router/searchHandler.go
Fichier normal
|
@ -0,0 +1,50 @@
|
|||
package router
|
||||
|
||||
import(
|
||||
"github.com/ewhal/nyaa/util/search"
|
||||
"net/http"
|
||||
"html/template"
|
||||
"github.com/gorilla/mux"
|
||||
"html"
|
||||
"strconv"
|
||||
"github.com/ewhal/nyaa/model"
|
||||
"github.com/ewhal/nyaa/service/torrent"
|
||||
)
|
||||
|
||||
func SearchHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var templates = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/home.html"))
|
||||
templates.ParseGlob("templates/_*.html") // common
|
||||
vars := mux.Vars(r)
|
||||
page := vars["page"]
|
||||
|
||||
// db params url
|
||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||
if pagenum == 0 {
|
||||
pagenum = 1
|
||||
}
|
||||
|
||||
b := []model.TorrentsJson{}
|
||||
|
||||
search_param, torrents, nbTorrents := search.SearchByQuery( r, pagenum )
|
||||
|
||||
for i, _ := range torrents {
|
||||
res := torrents[i].ToJson()
|
||||
b = append(b, res)
|
||||
}
|
||||
|
||||
navigationTorrents := Navigation{nbTorrents, search_param.Max, pagenum, "search_page"}
|
||||
searchForm := SearchForm{
|
||||
search_param.Query,
|
||||
search_param.Status,
|
||||
search_param.Category,
|
||||
search_param.Sort,
|
||||
search_param.Order,
|
||||
false,
|
||||
}
|
||||
htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), searchForm, navigationTorrents, r.URL, mux.CurrentRoute(r)}
|
||||
|
||||
err := templates.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package router
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
|
@ -8,22 +8,28 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
var funcMap = template.FuncMap{
|
||||
var FuncMap = template.FuncMap{
|
||||
"min": math.Min,
|
||||
"genRoute": func(name string, params ...string) string {
|
||||
url, err := router.Get(name).URL(params...)
|
||||
url, err := Router.Get(name).URL(params...)
|
||||
if err == nil {
|
||||
return url.String()
|
||||
}
|
||||
return "error"
|
||||
},
|
||||
"genRouteWithQuery": func(name string, currentUrl *url.URL, params ...string) template.HTML {
|
||||
url, err := Router.Get(name).URL(params...)
|
||||
if err == nil {
|
||||
return template.HTML(url.String()+ "?" + currentUrl.RawQuery)
|
||||
}
|
||||
return "error"
|
||||
},
|
||||
"genNav": func(nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML {
|
||||
maxPages := math.Ceil(float64(nav.TotalItem) / float64(nav.MaxItemPerPage))
|
||||
route := router.Get(nav.Route)
|
||||
|
||||
var ret = ""
|
||||
if nav.CurrentPage-1 > 0 {
|
||||
url, _ := route.URL("page", "1")
|
||||
url, _ := Router.Get(nav.Route).URL("page", "1")
|
||||
ret = ret + "<li><a id=\"page-prev\" href=\"" + url.String() + "?" + currentUrl.RawQuery + "\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"
|
||||
}
|
||||
startValue := 1
|
||||
|
@ -37,7 +43,7 @@ var funcMap = template.FuncMap{
|
|||
log.Println(nav.TotalItem)
|
||||
for i := startValue; i <= endValue; i++ {
|
||||
pageNum := strconv.Itoa(i)
|
||||
url, _ := route.URL("page", pageNum)
|
||||
url, _ := Router.Get(nav.Route).URL("page", pageNum)
|
||||
ret = ret + "<li"
|
||||
if i == nav.CurrentPage {
|
||||
ret = ret + " class=\"active\""
|
||||
|
@ -46,7 +52,7 @@ var funcMap = template.FuncMap{
|
|||
ret = ret + "><a href=\"" + url.String() + "?" + currentUrl.RawQuery + "\">" + strconv.Itoa(i) + "</a></li>"
|
||||
}
|
||||
if nav.CurrentPage < int(maxPages) {
|
||||
url, _ := route.URL("page", strconv.Itoa(nav.CurrentPage+1))
|
||||
url, _ := Router.Get(nav.Route).URL("page", strconv.Itoa(nav.CurrentPage+1))
|
||||
ret = ret + "<li><a id=\"page-next\" href=\"" + url.String() + "?" + currentUrl.RawQuery + "\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"
|
||||
}
|
||||
return template.HTML(ret)
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
25
router/viewTorrentHandler.go
Fichier normal
25
router/viewTorrentHandler.go
Fichier normal
|
@ -0,0 +1,25 @@
|
|||
package router
|
||||
|
||||
import(
|
||||
"net/http"
|
||||
"html/template"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/ewhal/nyaa/service/torrent"
|
||||
)
|
||||
|
||||
func ViewHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var templates = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html"))
|
||||
templates.ParseGlob("templates/_*.html") // common
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
torrent, err := torrentService.GetTorrentById(id)
|
||||
b := torrent.ToJson()
|
||||
|
||||
htv := ViewTemplateVariables{b, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
|
||||
|
||||
err = templates.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
|
@ -21,7 +21,8 @@
|
|||
{{if eq .Status 2}}remake{{end}}
|
||||
{{if eq .Status 3}}trusted{{end}}
|
||||
{{if eq .Status 4}}aplus{{end}}">
|
||||
<td>
|
||||
<!-- forced width because the <td> gets bigger randomly otherwise -->
|
||||
<td style="width:80px">
|
||||
<a href="{{$.URL.Parse (printf "/search?c=%s_%s" .Category.Id .Sub_Category.Id) }}">
|
||||
<img src="{{$.URL.Parse (printf "/img/torrents/%s.png" .Sub_Category.Id) }}">
|
||||
</a>
|
||||
|
|
8
templates/index.html
Fichier normal → Fichier exécutable
8
templates/index.html
Fichier normal → Fichier exécutable
|
@ -7,6 +7,10 @@
|
|||
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
|
||||
<title>Nyaa Pantsu - {{block "title" .}}Error 404{{end}}</title>
|
||||
<link rel="icon" type="image/png" href="/img/favicon.png" />
|
||||
|
||||
<!-- RSS Feed with Context -->
|
||||
<link href="{{ genRouteWithQuery "feed" .URL }}" rel="alternate" type="application/rss+xml" title="Nyaa Pantsu - {{block "rsstitle" .}}Last torrents{{end}} RSS Feed" />
|
||||
|
||||
<!-- Latest compiled and minified CSS -->
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
|
||||
|
||||
|
@ -37,8 +41,10 @@
|
|||
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
|
||||
<ul class="nav navbar-nav">
|
||||
<li><a href="https://sukebei.pantsu.cat">Fap</a></li>
|
||||
<li><a href="{{.URL.Parse "/faq"}}">FAQ</a></li>
|
||||
<li><a href="{{.URL.Parse "/feed"}}?c={{.Search.Category}}&q={{.Search.Query}}&s={{.Search.Status}}">RSS</a></li>
|
||||
<li><a href="{{.URL.Parse "/faq"}}">FAQ</a></li>
|
||||
<li><a href="irc://irc.rizon.net/nyaapantsu">IRC</a></li>
|
||||
<li><a href="{{ genRouteWithQuery "feed" .URL }}">Feed RSS</a></li>
|
||||
</ul>
|
||||
<form class="navbar-form navbar-right" role="search" action="/search" method="get">
|
||||
<div class="form-group">
|
||||
|
|
|
@ -7,14 +7,12 @@
|
|||
<tr {{if eq .Status 2}}class="remake"{{end}}
|
||||
{{if eq .Status 3}}class="trusted"{{end}}
|
||||
{{if eq .Status 4}}class="aplus"{{end}}>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Name</td>
|
||||
<td>{{.Name}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Hash</td>
|
||||
<td>{{.Hash}}</td>
|
||||
<td class="torrent-hash">{{.Hash}}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Date</td>
|
||||
|
|
80
util/search/search.go
Fichier normal
80
util/search/search.go
Fichier normal
|
@ -0,0 +1,80 @@
|
|||
package search
|
||||
|
||||
import(
|
||||
"github.com/ewhal/nyaa/model"
|
||||
"github.com/ewhal/nyaa/service/torrent"
|
||||
"github.com/ewhal/nyaa/util/log"
|
||||
"net/http"
|
||||
"html"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SearchParam struct {
|
||||
Category string
|
||||
Order string
|
||||
Query string
|
||||
Max int
|
||||
Status string
|
||||
Sort string
|
||||
}
|
||||
|
||||
func SearchByQuery(r *http.Request, pagenum int) (SearchParam, []model.Torrents, int) {
|
||||
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
|
||||
if errConv != nil {
|
||||
maxPerPage = 50 // default Value maxPerPage
|
||||
}
|
||||
|
||||
search_param := SearchParam{}
|
||||
search_param.Max = maxPerPage
|
||||
search_param.Query = r.URL.Query().Get("q")
|
||||
search_param.Category = r.URL.Query().Get("c")
|
||||
search_param.Status = r.URL.Query().Get("s")
|
||||
search_param.Sort = r.URL.Query().Get("sort")
|
||||
search_param.Order = r.URL.Query().Get("order")
|
||||
|
||||
catsSplit := strings.Split(search_param.Category, "_")
|
||||
// need this to prevent out of index panics
|
||||
var searchCatId, searchSubCatId string
|
||||
if len(catsSplit) == 2 {
|
||||
|
||||
searchCatId = html.EscapeString(catsSplit[0])
|
||||
searchSubCatId = html.EscapeString(catsSplit[1])
|
||||
}
|
||||
if search_param.Sort == "" {
|
||||
search_param.Sort = "torrent_id"
|
||||
}
|
||||
if search_param.Order == "" {
|
||||
search_param.Order = "desc"
|
||||
}
|
||||
order_by := search_param.Sort + " " + search_param.Order
|
||||
|
||||
parameters := torrentService.WhereParams{}
|
||||
conditions := []string{}
|
||||
if searchCatId != "" {
|
||||
conditions = append(conditions, "category_id = ?")
|
||||
parameters.Params = append(parameters.Params, searchCatId)
|
||||
}
|
||||
if searchSubCatId != "" {
|
||||
conditions = append(conditions, "sub_category_id = ?")
|
||||
parameters.Params = append(parameters.Params, searchSubCatId)
|
||||
}
|
||||
if search_param.Status != "" {
|
||||
if search_param.Status == "2" {
|
||||
conditions = append(conditions, "status_id != ?")
|
||||
} else {
|
||||
conditions = append(conditions, "status_id = ?")
|
||||
}
|
||||
parameters.Params = append(parameters.Params, search_param.Status)
|
||||
}
|
||||
searchQuerySplit := strings.Split(search_param.Query, " ")
|
||||
for i, _ := range searchQuerySplit {
|
||||
conditions = append(conditions, "torrent_name LIKE ?")
|
||||
parameters.Params = append(parameters.Params, "%"+searchQuerySplit[i]+"%")
|
||||
}
|
||||
|
||||
parameters.Conditions = strings.Join(conditions[:], " AND ")
|
||||
log.Infof("SQL query is :: %s\n", parameters.Conditions)
|
||||
torrents, n := torrentService.GetTorrentsOrderBy(¶meters, order_by, maxPerPage, maxPerPage*(pagenum-1))
|
||||
return search_param, torrents, n
|
||||
}
|
Référencer dans un nouveau ticket