Skip to content

Commit

Permalink
Issue #25
Browse files Browse the repository at this point in the history
ADD:
  - database/database.go:ScrapeTorrentFromInfoHash new function to scrape a
    specific torrent from its info hash.
  • Loading branch information
Ianleeclark committed May 10, 2016
1 parent e779168 commit 0711728
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ MySQLPass: testuser
MySQLDB: testdb
RedisServer: localhost
RedisPort: 6379
Whitelist: true
Whitelist: false
21 changes: 18 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,22 @@ func GetWhitelistedTorrents() (x *sql.Rows, err error) {
}

// ScrapeTorrent supports the Scrape convention
func ScrapeTorrent(db *gorm.DB, infoHash string) interface{} {
var torrent Torrent
return db.Where("infoHash = ?", infoHash).First(&torrent)
func ScrapeTorrentFromInfoHash(db *gorm.DB, infoHash string) (values string, err error) {
torrent := &Torrent{}

x := db.Where("info_hash = ?", infoHash).First(&torrent)
if x.Error != nil {
err = x.Error
}

fmt.Println(x.Value)

return
}

func ScrapeTorrent(db *gorm.DB) string {
torrent := &Torrent{}
db.Find(&torrent)

return ""
}
47 changes: 36 additions & 11 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func worker(data *announceData) []string {
CreateNewTorrentKey(data.requestContext.redisClient, data.info_hash)
return worker(data)
}

func (app *applicationContext) handleStatsTracking(data *announceData) {
if app.trackerLevel > RATIOLESS {
db.UpdatePeerStats(data.uploaded, data.downloaded, data.ip)
Expand Down Expand Up @@ -74,24 +73,50 @@ func (app *applicationContext) requestHandler(w http.ResponseWriter, req *http.R
w.Write([]byte(response))

} else {
failMsg := fmt.Sprintf("No peers for torrent %s\n", data.info_hash)
w.Write([]byte(createFailureMessage(failMsg)))
failMsg := fmt.Sprintf("No peers for torrent %s\n",
data.info_hash)
writeErrorResponse(w, failMsg)
}
}

app.handleStatsTracking(data)
}

func scrapeHandler(w http.ResponseWriter, req *http.Request) interface{} {
//query := req.URL.Query()
//infoHash := ParseInfoHash(query.Get("info_hash"))
func scrapeHandler(w http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
dbConn, err := db.OpenConnection()
if err != nil {
panic(err)
}

infoHash := query.Get("info_hash")
if infoHash != "" {
values, err := db.ScrapeTorrentFromInfoHash(
dbConn,
ParseInfoHash(infoHash))
if err != nil {
failMsg := fmt.Sprintf("Torrent not found.")
writeErrorResponse(w, failMsg)
}

writeResponse(w, req, values)
} else {
writeResponse(w, req, db.ScrapeTorrent(dbConn))
}

return
}

func writeErrorResponse(w http.ResponseWriter, failMsg string) {
w.Write([]byte(createFailureMessage(failMsg)))
}

//data := db.ScrapeTorrent(db.OpenConnection(), infoHash)
//return data
return "TODO"
func writeResponse(w http.ResponseWriter, req *http.Request, values string) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(values))
}

// RunServer spins up the server and muxes the url
// RunServer spins up the server and muxes the routes.
func RunServer() {
app := applicationContext{
config: config.LoadConfig(),
Expand All @@ -101,6 +126,6 @@ func RunServer() {
mux := http.NewServeMux()

mux.HandleFunc("/announce", app.requestHandler)
//mux.HandleFunc("/scrape", scrapeHandler)
mux.HandleFunc("/scrape", scrapeHandler)
http.ListenAndServe(":3000", mux)
}

0 comments on commit 0711728

Please # to comment.