révision
c99f1e6331
3 fichiers modifiés avec 79 ajouts et 0 suppressions
|
@ -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
|
||||
|
|
49
main.go
49
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"
|
||||
|
@ -140,6 +141,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"]
|
||||
|
@ -189,6 +237,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)
|
||||
|
||||
|
|
29
models.go
29
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
|
||||
|
||||
|
|
Référencer dans un nouveau ticket