Configured models to match ilikecats' merged db, and expanded automigration
Cette révision appartient à :
révision
95dfbb50e8
|
@ -2,6 +2,7 @@ language: go
|
||||||
go:
|
go:
|
||||||
- 1.x
|
- 1.x
|
||||||
install:
|
install:
|
||||||
|
- go get github.com/gorilla/feeds
|
||||||
- go get github.com/gorilla/mux
|
- go get github.com/gorilla/mux
|
||||||
- go get github.com/mattn/go-sqlite3
|
- go get github.com/mattn/go-sqlite3
|
||||||
- go get github.com/jinzhu/gorm
|
- go get github.com/jinzhu/gorm
|
||||||
|
|
|
@ -109,6 +109,7 @@
|
||||||
<td>{{.Hash}}</td>
|
<td>{{.Hash}}</td>
|
||||||
|
|
||||||
<td><a href="{{.Magnet}}" target="_blank" title="Magnet link" ><span class="glyphicon glyphicon-magnet" aria-hidden="true"></span></a>
|
<td><a href="{{.Magnet}}" target="_blank" title="Magnet link" ><span class="glyphicon glyphicon-magnet" aria-hidden="true"></span></a>
|
||||||
|
<a href="https://itorrents.org/torrent/{{.Hash}}.torrent" title="Torrent file"><span class="glyphicon glyphicon-floppy-save" aria-hidden="true"></span></a>
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
|
|
||||||
|
|
51
main.go
51
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/gorilla/feeds"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
|
@ -24,7 +25,6 @@ func getDBHandle() *gorm.DB {
|
||||||
|
|
||||||
// Migrate the schema of Torrents
|
// Migrate the schema of Torrents
|
||||||
dbInit.AutoMigrate(&Torrents{}, &Categories{}, &Sub_Categories{}, &Statuses{})
|
dbInit.AutoMigrate(&Torrents{}, &Categories{}, &Sub_Categories{}, &Statuses{})
|
||||||
|
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
return dbInit
|
return dbInit
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func singleapiHandler(w http.ResponseWriter, r *http.Request) {
|
func singleapiHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
@ -138,6 +139,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) {
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
page := vars["page"]
|
page := vars["page"]
|
||||||
|
@ -187,6 +235,7 @@ func main() {
|
||||||
router.HandleFunc("/api/{page}", apiHandler).Methods("GET")
|
router.HandleFunc("/api/{page}", apiHandler).Methods("GET")
|
||||||
router.HandleFunc("/api/torrent/{id}", singleapiHandler).Methods("GET")
|
router.HandleFunc("/api/torrent/{id}", singleapiHandler).Methods("GET")
|
||||||
router.HandleFunc("/faq", faqHandler)
|
router.HandleFunc("/faq", faqHandler)
|
||||||
|
router.HandleFunc("/feed.xml", rssHandler)
|
||||||
|
|
||||||
http.Handle("/", router)
|
http.Handle("/", router)
|
||||||
|
|
||||||
|
|
86
models.go
86
models.go
|
@ -8,18 +8,26 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Feed struct {
|
||||||
|
Id int
|
||||||
|
Name string
|
||||||
|
Hash string
|
||||||
|
Magnet 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_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 {
|
type Sub_Categories struct {
|
||||||
Sub_category_id int
|
Sub_category_id int
|
||||||
Sub_category_name string
|
Sub_category_name string
|
||||||
Parent_id int
|
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 {
|
type Statuses struct {
|
||||||
|
@ -88,6 +96,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) {
|
func getTorrentById(id string) (Torrents, error) {
|
||||||
var torrent Torrents
|
var torrent Torrents
|
||||||
|
|
||||||
|
@ -98,32 +127,51 @@ func getTorrentById(id string) (Torrents, error) {
|
||||||
return torrent, nil
|
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
|
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
|
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 {
|
func getTorrentsDB(parameters WhereParams) []Torrents {
|
||||||
var torrents []Torrents
|
return getTorrentsOrderBy(¶meters, "", 0, 0)
|
||||||
db.Where(parameters.conditions, parameters.params...).Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents)
|
|
||||||
return torrents
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function to get all torrents
|
/* Function to get all torrents
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func getAllTorrents(limit int, offset int) []Torrents {
|
func getAllTorrentsOrderBy(orderBy string, limit int, offset int) [] Torrents {
|
||||||
var torrents []Torrents
|
return getTorrentsOrderBy(nil, orderBy, limit, offset)
|
||||||
db.Model(&torrents).Limit(limit).Offset(offset).Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents)
|
}
|
||||||
|
|
||||||
return torrents
|
func getAllTorrents(limit int, offset int) []Torrents {
|
||||||
|
return getTorrentsOrderBy(nil, "", limit, offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAllTorrentsDB() []Torrents {
|
func getAllTorrentsDB() []Torrents {
|
||||||
var torrents []Torrents
|
return getTorrentsOrderBy(nil, "", 0, 0)
|
||||||
db.Order("torrent_id DESC").Preload("Categories").Preload("Sub_Categories").Find(&torrents)
|
|
||||||
return torrents
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function to get all categories with/without torrents (bool)
|
/* Function to get all categories with/without torrents (bool)
|
||||||
|
|
Référencer dans un nouveau ticket