From 59f820a264af2ec0f1dbd8a4970f77e83cd62b36 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Thu, 4 May 2017 17:16:20 +0200 Subject: [PATCH 1/4] Updated Models.go (simplified + orderBy param) Added a new main function to get Torrents with orderBy parameter Added a new function to get all torrents with orderBy parameter Simplifying the other function that get torrents so they pass through one main function that manage db requests. Commented AutoMigrate functions, it emits warning when you execute it after having migrated once --- main.go | 5 +++-- models.go | 53 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index 9528a674..2b211e7f 100644 --- a/main.go +++ b/main.go @@ -23,8 +23,8 @@ func getDBHandle() *gorm.DB { dbInit, err := gorm.Open("sqlite3", "./nyaa.db") // Migrate the schema of Torrents - dbInit.AutoMigrate(&Torrents{}) - dbInit.AutoMigrate(&Sub_Categories{}) + // dbInit.AutoMigrate(&Torrents{}) + // dbInit.AutoMigrate(&Sub_Categories{}) checkErr(err) return dbInit @@ -63,6 +63,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) { return } } + func singleapiHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/models.go b/models.go index 4735d830..66a36ef0 100644 --- a/models.go +++ b/models.go @@ -13,13 +13,13 @@ type Categories struct { Category_id int Category_name string Torrents []Torrents `gorm:"ForeignKey:category_id;AssociationForeignKey:category_id"` - Sub_category []Sub_Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:parent_id"` + Sub_Categories []Sub_Categories `gorm:"ForeignKey:category_id;AssociationForeignKey:parent_id"` } type Sub_Categories struct { - Sub_category_id int - Sub_category_name string - Parent_id int + Sub_category_id int `gorm:"column:sub_category_id"` + Sub_category_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"` } @@ -89,32 +89,51 @@ func getTorrentById(id string) (Torrents, error) { return torrent, nil } -func getTorrents(parameters WhereParams, limit int, offset int) []Torrents { +func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) []Torrents { var torrents []Torrents - db.Limit(limit).Offset(offset).Order("torrent_id DESC").Where(parameters.conditions, parameters.params...).Preload("Categories").Preload("Sub_Categories").Find(&torrents) + var dbQuery *gorm.DB + + if (parameters != nil) { // if there is where parameters + dbQuery = db.Model(&torrents).Where(parameters.conditions, parameters.params...) + } else { + dbQuery = db.Model(&torrents) + } + + if (orderBy == "") { orderBy = "torrent_id DESC" } // Default OrderBy + if limit != 0 || offset != 0 { // if limits provided + dbQuery = dbQuery.Limit(limit).Offset(offset) + } + dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents) return torrents } +/* Functions to simplify the get parameters of the main function + * + * Get Torrents with where parameters and limits, order by default + */ +func getTorrents(parameters WhereParams, limit int, offset int) []Torrents { + return getTorrentsOrderBy(¶meters, "", limit, offset) +} + +/* Get Torrents with where parameters but no limit and order by default (get all the torrents corresponding in the db) + */ func getTorrentsDB(parameters WhereParams) []Torrents { - var torrents []Torrents - db.Where(parameters.conditions, parameters.params...).Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents) - return torrents + return getTorrentsOrderBy(¶meters, "", 0, 0) } /* Function to get all torrents */ -func getAllTorrents(limit int, offset int) []Torrents { - var torrents []Torrents - db.Model(&torrents).Limit(limit).Offset(offset).Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents) +func getAllTorrentsOrderBy(orderBy string, limit int, offset int) [] Torrents { + return getTorrentsOrderBy(nil, orderBy, limit, offset) +} - return torrents +func getAllTorrents(limit int, offset int) []Torrents { + return getTorrentsOrderBy(nil, "", limit, offset) } func getAllTorrentsDB() []Torrents { - var torrents []Torrents - db.Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents) - return torrents + return getTorrentsOrderBy(nil, "", 0, 0) } /* Function to get all categories with/without torrents (bool) @@ -150,4 +169,4 @@ func createWhereParams(conditions string, params ...string) WhereParams { return whereParams } -/* Complete the functions when necessary... */ +/* Complete the functions when necessary... */ \ No newline at end of file From 6939a7517dfde01962616dbb765a53f58a22ca90 Mon Sep 17 00:00:00 2001 From: Owner Date: Thu, 4 May 2017 21:48:40 +0200 Subject: [PATCH 2/4] RSS feed for home page. --- main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ models.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/main.go b/main.go index 9528a674..4681626f 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "github.com/gorilla/feeds" "github.com/gorilla/mux" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" @@ -139,6 +140,53 @@ 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() + if ( len(torrents) > 0 ) { + created = torrents[0].Timestamp + } + created_as_time, err := time.Parse("2006-01-02 15:04:05", created) + if err == nil { + ; + } + 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, 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, + } + } + } + + rss, err := feed.ToRss() + if err == nil { + w.Write( []byte( rss ) ) + } else { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} + func rootHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) page := vars["page"] @@ -188,6 +236,7 @@ func main() { router.HandleFunc("/api/{page}", apiHandler).Methods("GET") router.HandleFunc("/api/torrent/{id}", singleapiHandler).Methods("GET") router.HandleFunc("/faq", faqHandler) + router.HandleFunc("/feed.xml", rssHandler) http.Handle("/", router) diff --git a/models.go b/models.go index 4735d830..d090afd0 100644 --- a/models.go +++ b/models.go @@ -9,6 +9,14 @@ import ( "strings" ) +type Feed struct { + Id int + Name string + Hash string + Magnet string + Timestamp string +} + type Categories struct { Category_id int Category_name string @@ -79,6 +87,27 @@ type HomeTemplateVariables 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 From 72fc788d71b172ab956079780fac2fd94b060489 Mon Sep 17 00:00:00 2001 From: Owner Date: Thu, 4 May 2017 21:58:05 +0200 Subject: [PATCH 3/4] Travis rss feed dependency. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index aae111c2..6815c806 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: go go: - 1.x install: +- go get github.com/gorilla/feeds - go get github.com/gorilla/mux - go get github.com/mattn/go-sqlite3 - go get github.com/jinzhu/gorm From aba761972c445573a54f4bc0bd1e990566ec1334 Mon Sep 17 00:00:00 2001 From: bittebitte Date: Thu, 4 May 2017 16:10:16 -0500 Subject: [PATCH 4/4] iTorrents to replace defunct Torcache --- index.html | 1 + 1 file changed, 1 insertion(+) diff --git a/index.html b/index.html index 82401407..6232c856 100644 --- a/index.html +++ b/index.html @@ -103,6 +103,7 @@ {{.Hash}} +