Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Merge pull request #7 from akuma06/master

Rearranged static Files and add number of item per page
Cette révision appartient à :
Eliot Whalan 2017-05-04 07:50:46 +10:00 révisé par GitHub
révision 551ceaf5ef
5 fichiers modifiés avec 101 ajouts et 48 suppressions

1
.gitignore externe
Voir le fichier

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

13
css/style.css Fichier normal
Voir le fichier

@ -0,0 +1,13 @@
.navbar {
background: linear-gradient(to bottom, #76eda0 0%,#1ad87f 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
}
.remake {
background-color: #f0b0b0;
}
.trusted {
background-color: #98d9a8;
}
.aplus {
background-color: #60b0f0;
}

Voir le fichier

@ -17,18 +17,10 @@
<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]-->
<!-- Website CSS -->
<link rel="stylesheet" href="css/style.css">
</head>
<style>
.remake {
background-color: #f0b0b0;
}
.trusted {
background-color: #98d9a8;
}
.aplus {
background-color: #60b0f0;
}
</style>
<body>
<nav class="navbar navbar-default">
<div class="container">
@ -67,6 +59,24 @@
<option value="5_11" {{if eq .Category "5_11"}}selected{{end}}>Live Action - Raw</option>
</select>
<input name="q" class="form-control" placeholder="Search" type="text">
<select name="max" class="form-control" value>
<option value="">Nb Item per Page</option>
<option value="5" {{if eq .QueryRecordCount 5}}selected{{end}}>5</option>
<option value="10" {{if eq .QueryRecordCount 10}}selected{{end}}>10</option>
<option value="15" {{if eq .QueryRecordCount 15}}selected{{end}}>15</option>
<option value="20" {{if eq .QueryRecordCount 20}}selected{{end}}>20</option>
<option value="25" {{if eq .QueryRecordCount 25}}selected{{end}}>25</option>
<option value="30" {{if eq .QueryRecordCount 30}}selected{{end}}>30</option>
<option value="35" {{if eq .QueryRecordCount 35}}selected{{end}}>35</option>
<option value="40" {{if eq .QueryRecordCount 40}}selected{{end}}>40</option>
<option value="45" {{if eq .QueryRecordCount 45}}selected{{end}}>45</option>
<option value="50" {{if eq .QueryRecordCount 50}}selected{{end}}>50</option>
<option value="70" {{if eq .QueryRecordCount 70}}selected{{end}}>70</option>
<option value="100" {{if eq .QueryRecordCount 100}}selected{{end}}>100</option>
<option value="150" {{if eq .QueryRecordCount 150}}selected{{end}}>150</option>
<option value="200" {{if eq .QueryRecordCount 200}}selected{{end}}>200</option>
<option value="300" {{if eq .QueryRecordCount 300}}selected{{end}}>300</option>
</select>
</div>
<button type="submit" class="btn btn-default">Search for Torrents</button>
</form>
@ -122,37 +132,8 @@
<!-- 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 query = window.location.search;
var page = parseInt(pathArray[2]);
var pageString = "/page/";
var next = page + 1;
var prev = page - 1;
if (prev < 1) {
prev = 1;
}
if (isNaN(page)) {
next = 2;
prev = 1;
}
if (query != "") {
pageString = "/search/";
}
var maxId = 5;
for (var i = 0; i < maxId; i++) {
var el = document.getElementById('page-' + i), n = next + i;
el.href = pageString + n + query;
el.innerHTML = n;
}
document.getElementById('page-next').href = pageString + next + query;
document.getElementById('page-prev').href = pageString + prev + query;
</script>
<!-- Main Javascript File -->
<script type="text/javascript" charset="utf-8" src="js/main.js"></script>
</body>
</html>

30
js/main.js Fichier normal
Voir le fichier

@ -0,0 +1,30 @@
var pathArray = window.location.pathname.split( '/' );
var query = window.location.search;
var page = parseInt(pathArray[2]);
var pageString = "/page/";
var next = page + 1;
var prev = page - 1;
if (prev < 1) {
prev = 1;
}
if (isNaN(page)) {
next = 2;
prev = 1;
}
if (query != "") {
pageString = "/search/";
}
var maxId = 5;
for (var i = 0; i < maxId; i++) {
var el = document.getElementById('page-' + i), n = next + i;
el.href = pageString + n + query;
el.innerHTML = n;
}
document.getElementById('page-next').href = pageString + next + query;
document.getElementById('page-prev').href = pageString + prev + query;

40
main.go
Voir le fichier

@ -116,17 +116,27 @@ func singleapiHandler(w http.ResponseWriter, r *http.Request) {
func searchHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
// db params url
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"));
if (errConv != nil) {
maxPerPage = 50 // default Value maxPerPage
}
pagenum, _ := strconv.Atoi(html.EscapeString(page))
param1 := r.URL.Query().Get("q")
cat := r.URL.Query().Get("c")
param2 := strings.Split(cat, "_")[0]
param3 := strings.Split(cat, "_")[1]
nbTorrents := 0
b := Record{Category: cat, Records: []Records{}}
rows, err := dbHandle.Query("select torrent_id, torrent_name, status_id, torrent_hash from torrents "+
"where torrent_name LIKE ? AND category_id LIKE ? AND sub_category_id LIKE ? "+
"ORDER BY torrent_id DESC LIMIT 50 offset ?",
"%"+html.EscapeString(param1)+"%", html.EscapeString(param2)+"%", html.EscapeString(param3)+"%", 50*(pagenum-1))
"ORDER BY torrent_id DESC LIMIT ? offset ?",
"%"+html.EscapeString(param1)+"%", html.EscapeString(param2)+"%", html.EscapeString(param3)+"%", maxPerPage, maxPerPage*(pagenum-1))
for rows.Next() {
nbTorrents++
var id, name, hash, magnet string
var status int
rows.Scan(&id, &name, &status, &hash)
@ -141,6 +151,8 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
b.Records = append(b.Records, res)
}
b.QueryRecordCount = maxPerPage
b.TotalRecordCount = nbTorrents
rows.Close()
err = templates.ExecuteTemplate(w, "index.html", &b)
@ -155,10 +167,19 @@ func safe(s string) template.URL {
func rootHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
// db params url
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"));
if (errConv != nil) {
maxPerPage = 50 // default Value maxPerPage
}
nbTorrents := 0
pagenum, _ := strconv.Atoi(html.EscapeString(page))
b := Record{Category: "_", Records: []Records{}}
rows, err := dbHandle.Query("select torrent_id, torrent_name, status_id, torrent_hash from torrents ORDER BY torrent_id DESC LIMIT 50 offset ?", 50*(pagenum-1))
rows, err := dbHandle.Query("select torrent_id, torrent_name, status_id, torrent_hash from torrents ORDER BY torrent_id DESC LIMIT ? offset ?", maxPerPage, maxPerPage*(pagenum-1))
for rows.Next() {
nbTorrents++
var id, name, hash, magnet string
var status int
rows.Scan(&id, &name, &status, &hash)
@ -173,8 +194,8 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
b.Records = append(b.Records, res)
}
b.QueryRecordCount = 50
b.TotalRecordCount = 1473098
b.QueryRecordCount = maxPerPage
b.TotalRecordCount = nbTorrents
rows.Close()
err = templates.ExecuteTemplate(w, "index.html", &b)
if err != nil {
@ -188,6 +209,11 @@ func main() {
dbHandle = getDBHandle()
router := mux.NewRouter()
cssHandler := http.FileServer(http.Dir("./css/"))
jsHandler := http.FileServer(http.Dir("./js/"))
http.Handle("/css/", http.StripPrefix("/css/", cssHandler))
http.Handle("/js/", http.StripPrefix("/js/", jsHandler))
// Routes,
router.HandleFunc("/", rootHandler)
router.HandleFunc("/page/{page}", rootHandler)
@ -195,9 +221,11 @@ func main() {
router.HandleFunc("/search/{page}", searchHandler)
router.HandleFunc("/api/{page}", apiHandler).Methods("GET")
router.HandleFunc("/api/torrent/{id}", singleapiHandler).Methods("GET")
http.Handle("/", router)
// Set up server,
srv := &http.Server{
Handler: router,
Addr: "localhost:9999",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,