Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Conflicts:
	main.go
	models.go
	templates/index.html
Saw that some previous modifications were lost due to the previous merge :)
Cette révision appartient à :
akuma06 2017-05-05 05:04:04 +02:00
révision 6f00348cbc
10 fichiers modifiés avec 308 ajouts et 33 suppressions

2
.gitignore externe
Voir le fichier

@ -2,4 +2,6 @@
*.db
main
nyaa
nyaa.exe
nyaa-master.exe
*.zip

Voir le fichier

@ -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

Voir le fichier

@ -7,16 +7,23 @@ that anyone will be able to deploy locally or remotely.
# Requirements
* Golang
# Installation
* Install golang
* go get github.com/ewhal/nyaa
* go build
* Download db
* place db in folder as "nyaa.d"
* ./nyaa
* go to localhost:9999
## TODO
* RSS feeds
* status searching
* torrent sorting
* RSS feeds(work in progress)
* torrent sorting (work in progress)
* Merge 5th of april dump with animetosho dump
* API improvement
* Site theme
* improve search by making it less strict and support non-english characters.
* Torrent view and description page
* improve search by making it less strict
* Torrent view and description page(work in progress)
* accounts?
* Adding new torrents
* scraping

117
main.go
Voir le fichier

@ -1,12 +1,16 @@
package main
import (
"bytes"
"compress/zlib"
"encoding/json"
"github.com/gorilla/feeds"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"html"
"html/template"
"io/ioutil"
"log"
"net/http"
"strconv"
@ -16,7 +20,6 @@ import (
var db *gorm.DB
var router *mux.Router
var debugLogger *log.Logger
var trackers = "&tr=udp://zer0day.to:1337/announce&tr=udp://tracker.leechers-paradise.org:6969&tr=udp://explodie.org:6969&tr=udp://tracker.opentrackr.org:1337&tr=udp://tracker.coppersurfer.tk:6969"
@ -24,8 +27,7 @@ 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{}, &Categories{}, &Sub_Categories{}, &Statuses{})
checkErr(err)
return dbInit
@ -37,6 +39,20 @@ func checkErr(err error) {
}
}
func unZlib(description []byte) string {
if (len(description) > 0) {
b := bytes.NewReader(description)
log.Println(b)
z, err := zlib.NewReader(b)
checkErr(err)
defer z.Close()
p, err := ioutil.ReadAll(z)
checkErr(err)
return string(p)
}
return ""
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
@ -64,7 +80,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
}
}
func singleapiHandler(w http.ResponseWriter, r *http.Request) {
func apiViewHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
@ -100,6 +116,9 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
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")
catsSplit := strings.Split(cat, "_")
// need this to prevent out of index panics
var searchCatId, searchSubCatId string
@ -108,13 +127,21 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
searchCatId = html.EscapeString(catsSplit[0])
searchSubCatId = html.EscapeString(catsSplit[1])
}
if sort == "" {
sort = "torrent_id"
}
if order == "" {
order = "desc"
}
order_by := sort + " " + order
nbTorrents := 0
b := []TorrentsJson{}
torrents, nbTorrents := getTorrents(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))
parameters := createWhereParams("torrent_name LIKE ? AND status_id LIKE ? AND category_id LIKE ? AND sub_category_id LIKE ?",
"%"+searchQuery+"%", stat+"%", searchCatId+"%", searchSubCatId+"%")
torrents, nbTorrents := getTorrentsOrderBy(&parameters, order_by, maxPerPage, maxPerPage*(pagenum-1))
for i, _ := range torrents {
res := torrents[i].toJson()
@ -122,7 +149,8 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
}
navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}
htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, navigationTorrents, r.URL, mux.CurrentRoute(r)}
htv := HomeTemplateVariables{b, getAllCategories(false), searchQuery, stat, cat, sort, order, navigationTorrents, r.URL, mux.CurrentRoute(r)}
err := templates.ExecuteTemplate(w, "index.html", htv)
if err != nil {
@ -135,7 +163,71 @@ func safe(s string) template.URL {
func faqHandler(w http.ResponseWriter, r *http.Request) {
var templates = template.Must(template.New("FAQ").Funcs(funcMap).ParseFiles("templates/index.html", "templates/FAQ.html"))
err := templates.ExecuteTemplate(w, "index.html", FaqTemplateVariables{r.URL, mux.CurrentRoute(r), "", "", "", Navigation{}})
err := templates.ExecuteTemplate(w, "index.html", FaqTemplateVariables{r.URL, mux.CurrentRoute(r), "", "", "_", "torrent_id", "desc", Navigation{}})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
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 viewHandler(w http.ResponseWriter, r *http.Request) {
var templates = template.Must(template.ParseFiles("templates/index.html", "templates/view.html"))
vars := mux.Vars(r)
id := vars["id"]
b := []TorrentsJson{}
torrent, err := getTorrentById(id)
res := torrent.toJson()
b = append(b, res)
htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", "", "", Navigation{}, r.URL, mux.CurrentRoute(r)}
err = templates.ExecuteTemplate(w, "index.html", htv)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -165,7 +257,7 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
}
navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"}
htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", navigationTorrents, r.URL, mux.CurrentRoute(r)}
htv := HomeTemplateVariables{b, getAllCategories(false), "", "", "_", "torrent_id", "desc", navigationTorrents, r.URL, mux.CurrentRoute(r)}
err := templates.ExecuteTemplate(w, "index.html", htv)
if err != nil {
@ -191,9 +283,12 @@ func main() {
router.HandleFunc("/page/{page:[0-9]+}", rootHandler).Name("home_page")
router.HandleFunc("/search", searchHandler).Name("search")
router.HandleFunc("/search/{page}", searchHandler).Name("search_page")
router.HandleFunc("/api/{page:[0-9]+}", apiHandler).Methods("GET")
router.HandleFunc("/api/torrent/{id:[0-9]+}", singleapiHandler).Methods("GET")
router.HandleFunc("/api/{page}", apiHandler).Methods("GET")
router.HandleFunc("/api/view/{id}", apiViewHandler).Methods("GET")
router.HandleFunc("/faq", faqHandler).Name("faq")
router.HandleFunc("/feed.xml", rssHandler)
router.HandleFunc("/view/{id}", viewHandler).Name("view_torrent")
http.Handle("/", router)

Voir le fichier

@ -9,6 +9,14 @@ import (
"strings"
)
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"`
@ -23,14 +31,24 @@ type Sub_Categories struct {
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 {
gorm.Model
Id int `gorm:"column:torrent_id"`
Name string `gorm:"column:torrent_name"`
Category_id int `gorm:"column:category_id"`
Sub_category_id int `gorm:"column:sub_category_id"`
Status int `gorm:"column:status_id"`
Status int `gorm:"column:status_id"`
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"`
Sub_Categories Sub_Categories `gorm:"ForeignKey:sub_category_id;AssociationForeignKey:sub_category_id"`
}
@ -59,6 +77,9 @@ type TorrentsJson struct {
Name string `json: "name"`
Status int `json: "status"`
Hash string `json: "hash"`
Date int `json: "date"`
Filesize string `json: "filesize"`
Description string `json: "description"`
Sub_Category SubCategoryJson `json: "sub_category"`
Category CategoryJson `json: "category"`
Magnet template.URL `json: "magnet"`
@ -76,6 +97,27 @@ 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
@ -98,12 +140,15 @@ func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
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
dbQuery = dbQuery.Limit(limit).Offset(offset)
}
dbQuery.Order(orderBy).Preload("Categories").Preload("Sub_Categories").Find(&torrents)
return torrents, count
}
/* Functions to simplify the get parameters of the main function
@ -124,6 +169,7 @@ func getTorrentsDB(parameters WhereParams) ([]Torrents, int) {
*/
func getAllTorrentsOrderBy(orderBy string, limit int, offset int) ([]Torrents, int) {
return getTorrentsOrderBy(nil, orderBy, limit, offset)
}
@ -166,9 +212,13 @@ func (t *Torrents) toJson() TorrentsJson {
Name: html.UnescapeString(t.Name),
Status: t.Status,
Hash: t.Hash,
Date: t.Date,
Filesize: t.Filesize,
Description: unZlib(t.Description),
Sub_Category: t.Sub_Categories.toJson(),
Category: t.Categories.toJson(),
Magnet: safe(magnet)}
return res
}

20
package.sh Fichier exécutable
Voir le fichier

@ -0,0 +1,20 @@
#!/usr/bin/env bash
# Helper script to ease building binary packages for multiple targets.
# Requires the linux64 and mingw64 gcc compilers and zip.
# On Debian-based distros install mingw-w64.
version=$(git describe --tags)
declare -a OSes
OSes[0]='linux;x86_64-linux-gnu-gcc'
OSes[1]='windows;x86_64-w64-mingw32-gcc'
for i in "${OSes[@]}"; do
arr=(${i//;/ })
os=${arr[0]}
cc=${arr[1]}
rm -f nyaa nyaa.exe
echo -e "\nBuilding $os..."
echo GOOS=$os GOARCH=amd64 CC=$cc CGO_ENABLED=1 go build -v
GOOS=$os GOARCH=amd64 CC=$cc CGO_ENABLED=1 go build -v
zip -9 -q nyaa-${version}_${os}_amd64.zip css js *.md *.html nyaa nyaa.exe
done

Voir le fichier

@ -15,6 +15,8 @@ type FaqTemplateVariables struct {
Query string
Status string
Category string
Sort string
Order string
Navigation Navigation
}
@ -24,6 +26,8 @@ type HomeTemplateVariables struct {
Query string
Status string
Category string
Sort string
Order string
Navigation Navigation
URL *url.URL // For parsing Url in templates
Route *mux.Route // For getting current route in templates

Voir le fichier

@ -6,6 +6,8 @@
<th>Id</th>
<th>Name</th>
<th>Hash</th>
<th>Date</th>
<th>Size</th>
<th>Links</th>
</tr>
{{ range .ListTorrents}}
@ -13,10 +15,13 @@
{{if eq .Status 3}}class="trusted"{{end}}
{{if eq .Status 4}}class="aplus"{{end}}>
<td><a href="{{$.URL.Parse (printf "/search?c=%s_%s" .Category.Id .Sub_Category.Id) }}"><img src="{{$.URL.Parse (printf "/img/torrents/%s.png" .Sub_Category.Id) }}"></td>
<td class="torrentName"><a href="{{$.URL.Parse (printf "/torrent/%s/%s" .Id .Name) }}">{{.Name}}</a></td>
<td class="torrentName"><a href="{{genRoute "view_torrent" "id" .Id }}">{{.Name}}</a></td>
<td>{{.Hash}}</td>
<td>{{.Date}}</td>
<td>{{.Filesize}}</td>
<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>

Voir le fichier

@ -35,10 +35,9 @@
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="https://sukebei.pantsu.cat">Sukebei Pantsu</a></li>
<li><a href="https://sukebei.pantsu.cat">Fap</a></li>
<li><a href="{{.URL.Parse "/faq"}}">Faq</a></li>
<li><a href="irc://irc.rizon.net/nyaapantsu">IRC</a></li>
</ul>
<form class="navbar-form navbar-right" role="search" action="/search" method="get">
<div class="form-group">
@ -68,6 +67,59 @@
<option value="3" {{if eq .Status "3"}}selected{{end}}>Trusted</option>
<option value="4" {{if eq .Status "4"}}selected{{end}}>A+</option>
</select>
<div class="input-group">
<input name="q" class="form-control input-sm" placeholder="Search" type="text" value="{{.Query}}">
<span class="input-group-btn">
<button type="submit" class="btn btn-sm btn-success"><span class="glyphicon glyphicon-search" aria-hidden="true"></span> Search</button>
</span>
</div>
</div>
</form>
</div>
</div>
</nav>
<div class="container" id="container">
<div class="blockBody">
<h3>Advanced Search</h3>
<form class="navbar-form navbar-right" role="search" action="/search" method="get">
<div class="form-group">
<select name="c" class="form-control input-sm" value>
<option value="_">All categories</option>
<option value="1_" {{if eq .Category "1_"}}selected{{end}}>Software</option>
<option value="1_1" {{if eq .Category "1_1"}}selected{{end}}>Software - Applications</option>
<option value="1_2" {{if eq .Category "1_2"}}selected{{end}}>Software - Games</option>
<option value="2_" {{if eq .Category "2_"}}selected{{end}}>Audio</option>
<option value="2_3" {{if eq .Category "2_3"}}selected{{end}}>Audio - Lossless</option>
<option value="2_4" {{if eq .Category "2_4"}}selected{{end}}>Audio - Lossy</option>
<option value="3_" {{if eq .Category "3_"}}selected{{end}}>Anime</option>
<option value="3_5" {{if eq .Category "3_5"}}selected{{end}}>Anime - English-translated</option>
<option value="3_6" {{if eq .Category "3_6"}}selected{{end}}>Anime - Raw</option>
<option value="4_" {{if eq .Category "4_"}}selected{{end}}>Literature</option>
<option value="4_7" {{if eq .Category "4_7"}}selected{{end}}>Literature - English-translated</option>
<option value="4_8" {{if eq .Category "4_8"}}selected{{end}}>Literature - Raw</option>
<option value="5_" {{if eq .Category "5_"}}selected{{end}}>Live Action</option>
<option value="5_9" {{if eq .Category "5_9"}}selected{{end}}>Live Action - English-translated</option>
<option value="5_10" {{if eq .Category "5_10"}}selected{{end}}>Live Action - Idol/Promotional Video</option>
<option value="5_11" {{if eq .Category "5_11"}}selected{{end}}>Live Action - Raw</option>
</select>
<select name="s" class="form-control input-sm">
<option value="">Search all</option>
<option value="2" {{if eq .Status "2"}}selected{{end}}>Remakes</option>
<option value="3" {{if eq .Status "3"}}selected{{end}}>Trusted</option>
<option value="4" {{if eq .Status "4"}}selected{{end}}>A+</option>
</select>
<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>
<option value="">No Items per Page</option>
<option value="5" {{if eq .Navigation.MaxItemPerPage 5}}selected{{end}}>5</option>
@ -94,11 +146,8 @@
</div>
</div>
</form>
</div>
</div>
</nav>
<div class="container" id="container">
<div style="clear:both"></div>
</div>
{{block "content" .}}Nothing Here.{{end}}
</div>

42
templates/view.html Fichier normal
Voir le fichier

@ -0,0 +1,42 @@
{{define "content"}}
<div class="blockBody">
<table class="table">
{{ range .ListTorrents}}
<tr {{if eq .Status 2}}class="remake"{{end}}
{{if eq .Status 3}}class="trusted"{{end}}
{{if eq .Status 4}}class="aplus"{{end}}>
<td>Id</td>
<td>{{.Id}}</td>
</tr>
<tr>
<td>Name</td>
<td>{{.Name}}</td>
</tr>
<tr>
<td>Hash</td>
<td>{{.Hash}}</td>
</tr>
<tr>
<td>Date</td>
<td>{{.Date}}</td>
</tr>
<tr>
<td>FileSize</td>
<td>{{.Filesize}}</td>
</tr>
tr
<tr>
<td>Links</td>
<td><a href="{{.Magnet}}" 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>
</tr>
<tr>
<td>Description</td>
<td>{{.Description}}</td>
</tr>
{{end}}
</table>
</div>
{{end}}