Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

RSS feeds for specific search queries.

Cette révision appartient à :
Owner 2017-05-05 13:37:22 +02:00
Parent a50d218504
révision 6304c447fd
2 fichiers modifiés avec 74 ajouts et 90 suppressions

135
main.go
Voir le fichier

@ -18,6 +18,15 @@ import (
"time"
)
type SearchParam struct {
Category string
Order string
Query string
Max int
Status string
Sort string
}
var db *gorm.DB
var router *mux.Router
var debugLogger *log.Logger
@ -108,38 +117,64 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
page := vars["page"]
// db params url
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
if errConv != nil {
maxPerPage = 50 // default Value maxPerPage
}
pagenum, _ := strconv.Atoi(html.EscapeString(page))
if pagenum == 0 {
pagenum = 1
}
searchQuery := r.URL.Query().Get("q")
cat := r.URL.Query().Get("c")
stat := r.URL.Query().Get("s")
sort := r.URL.Query().Get("sort")
order := r.URL.Query().Get("order")
b := []TorrentsJson{}
search_param, torrents, nbTorrents := searchByQuery( r, pagenum )
catsSplit := strings.Split(cat, "_")
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,
}
htv := HomeTemplateVariables{b, 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, []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 sort == "" {
sort = "torrent_id"
if search_param.Sort == "" {
search_param.Sort = "torrent_id"
}
if order == "" {
order = "desc"
if search_param.Order == "" {
search_param.Order = "desc"
}
order_by := sort + " " + order
nbTorrents := 0
b := []TorrentsJson{}
order_by := search_param.Sort + " " + search_param.Order
parameters := WhereParams{}
conditions := []string{}
@ -151,11 +186,11 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
conditions = append(conditions, "sub_category_id = ?")
parameters.params = append(parameters.params, searchSubCatId)
}
if stat != "" {
if search_param.Status != "" {
conditions = append(conditions, "status_id = ?")
parameters.params = append(parameters.params, stat)
parameters.params = append(parameters.params, search_param.Status)
}
searchQuerySplit := strings.Split(searchQuery, " ")
searchQuerySplit := strings.Split(search_param.Query, " ")
for i, _ := range searchQuerySplit {
conditions = append(conditions, "torrent_name LIKE ?")
parameters.params = append(parameters.params, "%"+searchQuerySplit[i]+"%")
@ -163,22 +198,10 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
parameters.conditions = strings.Join(conditions[:], " AND ")
log.Printf("SQL query is :: %s\n", parameters.conditions)
torrents, nbTorrents := getTorrentsOrderBy(&parameters, order_by, maxPerPage, maxPerPage*(pagenum-1))
for i, _ := range torrents {
res := torrents[i].toJson()
b = append(b, res)
}
navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}
searchForm := SearchForm{searchQuery, stat, cat, sort, order}
htv := HomeTemplateVariables{b, 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)
}
torrents, n := getTorrentsOrderBy(&parameters, order_by, maxPerPage, maxPerPage*(pagenum-1))
return search_param, torrents, n
}
func safe(s string) template.URL {
return template.URL(s)
}
@ -193,20 +216,10 @@ func faqHandler(w http.ResponseWriter, r *http.Request) {
}
func rssHandler(w http.ResponseWriter, r *http.Request) {
//vars := mux.Vars(r)
//category := vars["c"]
// db params url
//maxPerPage := 50 // default Value maxPerPage
torrents := getFeeds()
created := time.Now().String()
_, torrents, _ := searchByQuery( r, 1 )
created_as_time := time.Now()
if len(torrents) > 0 {
created = torrents[0].Timestamp
}
created_as_time, err := time.Parse("2006-01-02 15:04:05", created)
if err == nil {
created_as_time = time.Unix(torrents[0].Date, 0)
}
feed := &feeds.Feed{
Title: "Nyaa Pantsu",
@ -217,17 +230,16 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
feed.Items = make([]*feeds.Item, len(torrents))
for i, _ := range torrents {
timestamp_as_time, err := time.Parse("2006-01-02 15:04:05", torrents[i].Timestamp)
if err == nil {
feed.Items[i] = &feeds.Item{
// need a torrent view first
//Id: URL + torrents[i].Hash,
Title: torrents[i].Name,
Link: &feeds.Link{Href: string(torrents[i].Magnet)},
Description: "",
Created: timestamp_as_time,
Updated: timestamp_as_time,
}
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,
}
}
@ -238,6 +250,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
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

Voir le fichier

@ -10,14 +10,6 @@ import (
"time"
)
type Feed struct {
Id int
Name string
Hash string
Magnet string
Timestamp string
}
type Categories struct {
Id int `gorm:"column:category_id"`
Name string `gorm:"column:category_name"`
@ -97,27 +89,6 @@ type WhereParams struct {
*
*/
// don't need raw SQL once we get MySQL
func getFeeds() []Feed {
var result []Feed
rows, err := db.DB().
Query(
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " +
"ORDER BY `timestamp` desc LIMIT 50")
if err == nil {
for rows.Next() {
item := Feed{}
rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp)
magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(item.Hash) + "&dn=" + item.Name + trackers
item.Magnet = magnet
// memory hog
result = append(result, item)
}
rows.Close()
}
return result
}
func getTorrentById(id string) (Torrents, error) {
var torrent Torrents