Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

initial version

Cette révision appartient à :
Eliot Whalan 2017-05-02 20:39:53 +10:00
révision 17b1917f0b
4 fichiers modifiés avec 332 ajouts et 0 suppressions

4
.gitignore externe Fichier normal
Voir le fichier

@ -0,0 +1,4 @@
*.sqlite
*.db
main
nyaa

19
LICENSE.md Fichier normal
Voir le fichier

@ -0,0 +1,19 @@
Copyright (c) 2016 Eliot Whalan <ewhal@pantsu.cat>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

121
index.html Fichier normal
Voir le fichier

@ -0,0 +1,121 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Bootstrap -->
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="https://nyaa.pantsu.cat">Pantsu Nyaa Indexer</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<form class="navbar-form navbar-left" role="search" action="/search" method="get">
<div class="form-group">
<input name="q" class="form-control" placeholder="Search" type="text">
</div>
<button type="submit" class="btn btn-default">Search for Torrents</button>
</form>
</div>
</div>
</nav>
<div class="container">
<table class="table">
<tr>
<th>Name</th>
<th>Hash</th>
<th>Links</th>
</tr>
{{ range .Records}}
<tr>
<td>{{.Name}}</td>
<td>{{.Hash}}</td>
<td><a href="magnet:{{.Magnet}}"><span class="glyphicon glyphicon-magnet" aria-hidden="true"></span></a>
<a href="http://torcache.net/torrent/{{.Hash}}.torrent"><span class="glyphicon glyphicon-floppy-save" aria-hidden="true"></span></a>
</td>
</tr>
{{end}}
</table>
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a id="Prev" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li><a id="One"></a></li>
<li><a id="Two"></a></li>
<li><a id="Three"></a></li>
<li><a id="Four"></a></li>
<li><a id="Five"></a></li>
<li>
<a id="Next" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
var pathArray = window.location.pathname.split( '/' );
var page = parseInt(pathArray[2]);
var next = page+1;
var prev = page-1
if (prev < 1) {
prev = 1;
}
var one = next+1;
var two = next+2;
var three = next+3;
var four = next+4;
var five = next+5;
document.getElementById("One").href= one;
document.getElementById("One").innerHTML = one.toString();
document.getElementById("Two").href= two;
document.getElementById("Two").innerHTML = two.toString();
document.getElementById("Three").href= three;
document.getElementById("Three").innerHTML = three.toString();
document.getElementById("Four").href= four;
document.getElementById("Four").innerHTML = four.toString();
document.getElementById("Five").href= five;
document.getElementById("Five").innerHTML = five.toString();
document.getElementById("Next").href = next;
document.getElementById("Prev").href = prev;
</script>
</body>
</html>

188
main.go Fichier normal
Voir le fichier

@ -0,0 +1,188 @@
package main
import (
"database/sql"
"encoding/json"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3"
"html"
"html/template"
"log"
"net/http"
"os"
"strconv"
"time"
)
var dbHandle *sql.DB
var templates = template.Must(template.ParseFiles("index.html"))
var debugLogger *log.Logger
type Record struct {
Records []Records `json: "records"`
QueryRecordCount int `json: "queryRecordCount"`
TotalRecordCount int `json: "totalRecordCount"`
}
type Records struct {
Id string `json: "id"`
Name string `json: "name"`
Hash string `json: "hash"`
Magnet string `json: "magnet"`
}
func getDBHandle() *sql.DB {
db, err := sql.Open("sqlite3", "./nyaa.db")
checkErr(err)
return db
}
func checkErr(err error) {
if err != nil {
debugLogger.Println(" " + err.Error())
os.Exit(1)
}
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
pagenum, _ := strconv.Atoi(html.EscapeString(page))
b := Record{Records: []Records{}}
rows, err := dbHandle.Query("select torrent_id, torrent_name, torrent_hash from torrents LIMIT 50 offset ?", 50*pagenum-1)
for rows.Next() {
var id, name, hash, magnet string
rows.Scan(&id, &name, &hash)
magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + html.EscapeString(name) + "&tr=udp://tracker.openbittorrent.com"
res := Records{
Id: id,
Name: name,
Hash: hash,
Magnet: magnet}
b.Records = append(b.Records, res)
}
b.QueryRecordCount = 50
b.TotalRecordCount = 1473098
rows.Close()
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func singleapiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
b := Record{Records: []Records{}}
rows, err := dbHandle.Query("select torrent_id, torrent_name, torrent_hash from torrents where torrent_id = ?", html.EscapeString(id))
for rows.Next() {
var id, name, hash, magnet string
rows.Scan(&id, &name, &hash)
magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + html.EscapeString(name) + "&tr=udp://tracker.openbittorrent.com"
res := Records{
Id: id,
Name: name,
Hash: hash,
Magnet: magnet}
b.Records = append(b.Records, res)
}
b.QueryRecordCount = 1
b.TotalRecordCount = 1473098
rows.Close()
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
pagenum, _ := strconv.Atoi(html.EscapeString(page))
param1 := r.URL.Query().Get("q")
b := Record{Records: []Records{}}
rows, err := dbHandle.Query("select torrent_id, torrent_name, torrent_hash from torrents where torrent_name LIKE ? LIMIT 50 offset ?", "%"+html.EscapeString(param1)+"%", 50*pagenum-1)
for rows.Next() {
var id, name, hash, magnet string
rows.Scan(&id, &name, &hash)
magnet = "magnet:?xt=urn:btih:" + hash + "&dn=" + html.EscapeString(name) + "&tr=udp://tracker.openbittorrent.com"
res := Records{
Id: id,
Name: name,
Hash: hash,
Magnet: magnet}
b.Records = append(b.Records, res)
}
rows.Close()
err = templates.ExecuteTemplate(w, "index.html", &b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
pagenum, _ := strconv.Atoi(html.EscapeString(page))
b := Record{Records: []Records{}}
rows, err := dbHandle.Query("select torrent_id, torrent_name, torrent_hash from torrents LIMIT 50 offset ?", 50*pagenum-1)
for rows.Next() {
var id, name, hash, magnet string
rows.Scan(&id, &name, &hash)
magnet = "?xt=urn:btih:" + hash + "&dn=" + html.EscapeString(name) + "&tr=udp://tracker.openbittorrent.com"
res := Records{
Id: id,
Name: name,
Hash: hash,
Magnet: magnet}
b.Records = append(b.Records, res)
}
b.QueryRecordCount = 50
b.TotalRecordCount = 1473098
rows.Close()
err = templates.ExecuteTemplate(w, "index.html", &b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
dbHandle = getDBHandle()
router := mux.NewRouter()
// Routes,
router.HandleFunc("/", rootHandler)
router.HandleFunc("/page/{page}", rootHandler)
router.HandleFunc("/search", searchHandler)
// router.HandleFunc("/search/{page}", searchHandler)
router.HandleFunc("/api/{page}", apiHandler).Methods("GET")
router.HandleFunc("/api/torrent/{id}", singleapiHandler).Methods("GET")
// Set up server,
srv := &http.Server{
Handler: router,
Addr: "localhost:9999",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
err := srv.ListenAndServe()
checkErr(err)
}