Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Cette révision appartient à :
ayame-git 2017-05-05 07:24:58 +03:00
Parent aa52cd7026
révision d8cfa78b4d
3 fichiers modifiés avec 82 ajouts et 49 suppressions

Voir le fichier

@ -67,8 +67,17 @@
<option value="4" {{if eq .Status "4"}}selected{{end}}>A+</option> <option value="4" {{if eq .Status "4"}}selected{{end}}>A+</option>
</select> </select>
<input name="q" class="form-control input-sm" placeholder="Search" type="text" value={{.Query}}> <input name="q" class="form-control input-sm" placeholder="Search" type="text" value={{.Query}}>
<select name="sort" class="form-control input-sm">
<option value="torrent_id" {{if eq .Sort "torrent_id"}}selected{{end}}>Id</option>
<option value="torrent_name" {{if eq .Sort "torrent_name"}}selected{{end}}>Name</option>
<option value="date" {{if eq .Sort "date"}}selected{{end}}>Date</option>
<option value="downloads" {{if eq .Sort "downloads"}}selected{{end}}>Downloads</option>
</select>
<select name="order" class="form-control input-sm">
<option value="desc" {{if eq .Order "desc"}}selected{{end}}>Descend</option>
<option value="asc" {{if eq .Order "asc"}}selected{{end}}>Ascend</option>
</select>
<select name="max" class="form-control input-sm" value> <select name="max" class="form-control input-sm" value>
<option value="">No Items per Page</option>
<option value="5" {{if eq .QueryRecordCount 5}}selected{{end}}>5</option> <option value="5" {{if eq .QueryRecordCount 5}}selected{{end}}>5</option>
<option value="10" {{if eq .QueryRecordCount 10}}selected{{end}}>10</option> <option value="10" {{if eq .QueryRecordCount 10}}selected{{end}}>10</option>
<option value="15" {{if eq .QueryRecordCount 15}}selected{{end}}>15</option> <option value="15" {{if eq .QueryRecordCount 15}}selected{{end}}>15</option>
@ -91,7 +100,7 @@
</div> </div>
</div> </div>
</nav> </nav>
<div class="container"> <div class="container">
<table class="table"> <table class="table">
<tr> <tr>

46
main.go
Voir le fichier

@ -24,8 +24,7 @@ func getDBHandle() *gorm.DB {
dbInit, err := gorm.Open("sqlite3", "./nyaa.db") dbInit, err := gorm.Open("sqlite3", "./nyaa.db")
// Migrate the schema of Torrents // Migrate the schema of Torrents
// dbInit.AutoMigrate(&Torrents{}) dbInit.AutoMigrate(&Torrents{}, &Categories{}, &Sub_Categories{}, &Statuses{})
// dbInit.AutoMigrate(&Sub_Categories{})
checkErr(err) checkErr(err)
return dbInit return dbInit
@ -99,6 +98,9 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
searchQuery := r.URL.Query().Get("q") searchQuery := r.URL.Query().Get("q")
cat := r.URL.Query().Get("c") cat := r.URL.Query().Get("c")
stat := r.URL.Query().Get("s") stat := r.URL.Query().Get("s")
sort := r.URL.Query().Get("sort")
order := r.URL.Query().Get("order")
catsSplit := strings.Split(cat, "_") catsSplit := strings.Split(cat, "_")
// need this to prevent out of index panics // need this to prevent out of index panics
var searchCatId, searchSubCatId string var searchCatId, searchSubCatId string
@ -107,13 +109,21 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
searchCatId = html.EscapeString(catsSplit[0]) searchCatId = html.EscapeString(catsSplit[0])
searchSubCatId = html.EscapeString(catsSplit[1]) searchSubCatId = html.EscapeString(catsSplit[1])
} }
if sort == "" {
sort = "torrent_id"
}
if order == "" {
order = "desc"
}
order_by := sort + " " + order
nbTorrents := 0 nbTorrents := 0
b := []TorrentsJson{} b := []TorrentsJson{}
torrents := getTorrents(createWhereParams("torrent_name LIKE ? AND status_id LIKE ? AND category_id LIKE ? AND sub_category_id LIKE ?", parameters := createWhereParams("torrent_name LIKE ? AND status_id LIKE ? AND category_id LIKE ? AND sub_category_id LIKE ?",
"%"+searchQuery+"%", stat+"%", searchCatId+"%", searchSubCatId+"%"), maxPerPage, maxPerPage*(pagenum-1)) "%"+searchQuery+"%", stat+"%", searchCatId+"%", searchSubCatId+"%")
torrents := getTorrentsOrderBy(&parameters, order_by, maxPerPage, maxPerPage*(pagenum-1))
for i, _ := range torrents { for i, _ := range torrents {
nbTorrents++ nbTorrents++
@ -123,7 +133,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
} }
htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, maxPerPage, nbTorrents} htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, sort, order, maxPerPage, nbTorrents}
err := templates.ExecuteTemplate(w, "index.html", htv) err := templates.ExecuteTemplate(w, "index.html", htv)
if err != nil { if err != nil {
@ -150,39 +160,39 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
torrents := getFeeds() torrents := getFeeds()
created := time.Now().String() created := time.Now().String()
if ( len(torrents) > 0 ) { if len(torrents) > 0 {
created = torrents[0].Timestamp created = torrents[0].Timestamp
} }
created_as_time, err := time.Parse("2006-01-02 15:04:05", created) created_as_time, err := time.Parse("2006-01-02 15:04:05", created)
if err == nil { if err == nil {
;
} }
feed := &feeds.Feed{ feed := &feeds.Feed{
Title: "Nyaa Pantsu", Title: "Nyaa Pantsu",
Link: &feeds.Link{Href: "https://nyaa.pantsu.cat/"}, Link: &feeds.Link{Href: "https://nyaa.pantsu.cat/"},
Created: created_as_time, Created: created_as_time,
} }
feed.Items = []*feeds.Item{} feed.Items = []*feeds.Item{}
feed.Items = make( []*feeds.Item, len(torrents)) feed.Items = make([]*feeds.Item, len(torrents))
for i, _ := range torrents { for i, _ := range torrents {
timestamp_as_time, err := time.Parse("2006-01-02 15:04:05", torrents[i].Timestamp) timestamp_as_time, err := time.Parse("2006-01-02 15:04:05", torrents[i].Timestamp)
if err == nil { if err == nil {
feed.Items[i] = &feeds.Item{ feed.Items[i] = &feeds.Item{
// need a torrent view first // need a torrent view first
//Id: URL + torrents[i].Hash, //Id: URL + torrents[i].Hash,
Title: torrents[i].Name, Title: torrents[i].Name,
Link: &feeds.Link{Href: string(torrents[i].Magnet)}, Link: &feeds.Link{Href: string(torrents[i].Magnet)},
Description: "", Description: "",
Created: timestamp_as_time, Created: timestamp_as_time,
Updated: timestamp_as_time, Updated: timestamp_as_time,
} }
} }
} }
rss, err := feed.ToRss() rss, err := feed.ToRss()
if err == nil { if err == nil {
w.Write( []byte( rss ) ) w.Write([]byte(rss))
} else { } else {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
@ -210,7 +220,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
} }
htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", maxPerPage, nbTorrents} htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", "id", "desc", maxPerPage, nbTorrents}
err := templates.ExecuteTemplate(w, "index.html", htv) err := templates.ExecuteTemplate(w, "index.html", htv)
if err != nil { if err != nil {

Voir le fichier

@ -10,35 +10,45 @@ import (
) )
type Feed struct { type Feed struct {
Id int Id int
Name string Name string
Hash string Hash string
Magnet string Magnet string
Timestamp string Timestamp string
} }
type Categories struct { type Categories struct {
Category_id int Category_id int
Category_name string Category_name string
Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"` Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"`
Sub_Categories []Sub_Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:parent_id"` Sub_Categories []Sub_Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:parent_id"`
} }
type Sub_Categories struct { type Sub_Categories struct {
Sub_category_id int `gorm:"column:sub_category_id"` Sub_category_id int
Sub_category_name string `gorm:"column:Sub_category_name"` Sub_category_name string
Parent_id int `gorm:"column:parent_id"` Parent_id int
Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"` Torrents []Torrents `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"`
} }
type Statuses struct {
Status_id int
Status_name string
Torrents []Torrents `gorm:"ForeignKey:status_id;AssociationForeignKey:status_id"`
}
type Torrents struct { type Torrents struct {
gorm.Model
Id int `gorm:"column:torrent_id"` Id int `gorm:"column:torrent_id"`
Name string `gorm:"column:torrent_name"` Name string `gorm:"column:torrent_name"`
Category_id int `gorm:"column:category_id"` Category_id int `gorm:"column:category_id"`
Sub_category_id int `gorm:"column:sub_category_id"` Sub_category_id int `gorm:"column:sub_category_id"`
Status int `gorm:"column:status_id"` Status_id int `gorm:"column:status_id"`
Hash string `gorm:"column:torrent_hash"` Hash string `gorm:"column:torrent_hash"`
Date int `gorm:"column:date"`
Downloads int `gorm:"column:downloads"`
Filesize string `gorm:"column:filesize"`
Description []byte `gorm:"column:description"`
Statuses Statuses `gorm:"ForeignKey:status_id;AssociationForeignKey:status_id"`
Categories Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"` Categories Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"`
Sub_Categories Sub_Categories `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"` Sub_Categories Sub_Categories `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"`
} }
@ -77,6 +87,8 @@ type HomeTemplateVariables struct {
Query string Query string
Status string Status string
Category string Category string
Sort string
Order string
QueryRecordCount int QueryRecordCount int
TotalRecordCount int TotalRecordCount int
} }
@ -93,15 +105,15 @@ func getFeeds() []Feed {
rows, err := db.DB(). rows, err := db.DB().
Query( Query(
"SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " + "SELECT `torrent_id` AS `id`, `torrent_name` AS `name`, `torrent_hash` AS `hash`, `timestamp` FROM `torrents` " +
"ORDER BY `timestamp` desc LIMIT 50") "ORDER BY `timestamp` desc LIMIT 50")
if ( err == nil ) { if err == nil {
for rows.Next() { for rows.Next() {
item := Feed{} item := Feed{}
rows.Scan( &item.Id, &item.Name, &item.Hash, &item.Timestamp ) rows.Scan(&item.Id, &item.Name, &item.Hash, &item.Timestamp)
magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(item.Hash) + "&dn=" + item.Name + trackers magnet := "magnet:?xt=urn:btih:" + strings.TrimSpace(item.Hash) + "&dn=" + item.Name + trackers
item.Magnet = magnet item.Magnet = magnet
// memory hog // memory hog
result = append( result, item ) result = append(result, item)
} }
rows.Close() rows.Close()
} }
@ -121,23 +133,25 @@ func getTorrentById(id string) (Torrents, error) {
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) []Torrents { func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) []Torrents {
var torrents []Torrents var torrents []Torrents
var dbQuery *gorm.DB var dbQuery *gorm.DB
if (parameters != nil) { // if there is where parameters if parameters != nil { // if there is where parameters
dbQuery = db.Model(&torrents).Where(parameters.conditions, parameters.params...) dbQuery = db.Model(&torrents).Where(parameters.conditions, parameters.params...)
} else { } else {
dbQuery = db.Model(&torrents) dbQuery = db.Model(&torrents)
} }
if (orderBy == "") { orderBy = "torrent_id DESC" } // Default OrderBy if orderBy == "" {
orderBy = "torrent_id DESC"
} // Default OrderBy
if limit != 0 || offset != 0 { // if limits provided if limit != 0 || offset != 0 { // if limits provided
dbQuery = dbQuery.Limit(limit).Offset(offset) dbQuery = dbQuery.Limit(limit).Offset(offset)
} }
dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents) dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents)
return torrents return torrents
} }
/* Functions to simplify the get parameters of the main function /* Functions to simplify the get parameters of the main function
* *
* Get Torrents with where parameters and limits, order by default * Get Torrents with where parameters and limits, order by default
*/ */
func getTorrents(parameters WhereParams, limit int, offset int) []Torrents { func getTorrents(parameters WhereParams, limit int, offset int) []Torrents {
@ -153,7 +167,7 @@ func getTorrentsDB(parameters WhereParams) []Torrents {
/* Function to get all torrents /* Function to get all torrents
*/ */
func getAllTorrentsOrderBy(orderBy string, limit int, offset int) [] Torrents { func getAllTorrentsOrderBy(orderBy string, limit int, offset int) []Torrents {
return getTorrentsOrderBy(nil, orderBy, limit, offset) return getTorrentsOrderBy(nil, orderBy, limit, offset)
} }
@ -182,7 +196,7 @@ func (t *Torrents) toJson() TorrentsJson {
res := TorrentsJson{ res := TorrentsJson{
Id: strconv.Itoa(t.Id), Id: strconv.Itoa(t.Id),
Name: html.UnescapeString(t.Name), Name: html.UnescapeString(t.Name),
Status: t.Status, Status: t.Status_id,
Hash: t.Hash, Hash: t.Hash,
Magnet: safe(magnet)} Magnet: safe(magnet)}
return res return res
@ -198,4 +212,4 @@ func createWhereParams(conditions string, params ...string) WhereParams {
return whereParams return whereParams
} }
/* Complete the functions when necessary... */ /* Complete the functions when necessary... */