commit 17b1917f0b65ea52a6390891123a05a34c252f46 Author: Eliot Whalan Date: Tue May 2 20:39:53 2017 +1000 initial version diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..2d24479d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.sqlite +*.db +main +nyaa diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000..a751266c --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,19 @@ +Copyright (c) 2016 Eliot Whalan + +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. + diff --git a/index.html b/index.html new file mode 100644 index 00000000..0f50201b --- /dev/null +++ b/index.html @@ -0,0 +1,121 @@ + + + + + + + + Bootstrap 101 Template + + + + + + + + + + + + +
+ + + + + + + {{ range .Records}} + + + + + + + + {{end}} + +
NameHashLinks
{{.Name}}{{.Hash}} + +
+ +
+ + + + + + + + + diff --git a/main.go b/main.go new file mode 100644 index 00000000..16cf3aea --- /dev/null +++ b/main.go @@ -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) +}