Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/controllers/torrent/stats.go
kilo dd94402f04
Fixes & improvements (#1684)
* Update main.css

* Update torrent_item.jet.html

* Update main.js

* Update torrent_item.jet.html

* Update main.js

* Update main.js

* Update main.js

* Update main.js

* Update main.js

* Update template_functions_test.go

* Update template_functions_test.go

* Update user.go

* Update profile.jet.html

* Update view.jet.html

* Update template_functions.go

* Update en-us.all.json

* Add files via upload

* Update CHANGELOG.md

* Update main.css

* Update template_functions.go

* Add files via upload

* Update en-us.all.json

* Update template_functions_test.go

* Update view.jet.html

* Update main.css

* Update ja-jp.all.json

* Update main.css

* Update edit.jet.html

* Update main.css

* Update tomorrow.css

* Update ja-jp.all.json

* Update main.css

* Update view.jet.html

* Update comment.go

* Update create.go

* Update classic.css

* Make GetOldNavFromRequest return true by default

* Update view.jet.html

* Force torrent date in UTC+0 timezone during display

* ditto

* Update ja-jp.all.json

* Update oldNav.jet.html

* Update structs.go

* Update default_config.yml

* Update torrent.go

* Update stats.go

* Update stats.go

* Update user.go

* Update template_functions.go

* Update template_functions_test.go

* Update torrent.go

* Update comment.go

* Update view.jet.html

* Update main.css

* Update user.go

* Update template_functions.go

* Update profile.jet.html

* Update main.js
2017-10-28 18:28:59 +02:00

107 lignes
3,6 Kio
Go

package torrentController
import (
"strconv"
"strings"
"net/url"
"time"
"github.com/NyaaPantsu/nyaa/models/torrents"
"github.com/NyaaPantsu/nyaa/models"
"github.com/NyaaPantsu/nyaa/config"
"github.com/Stephen304/goscrape"
"github.com/gin-gonic/gin"
)
// ViewHeadHandler : Controller for getting torrent stats
func GetStatsHandler(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 32)
if err != nil {
return
}
torrent, err := torrents.FindRawByID(uint(id))
if err != nil {
return
}
var CurrentData models.Scrape
statsExists := !(models.ORM.Where("torrent_id = ?", id).Find(&CurrentData).RecordNotFound())
if statsExists {
//Stats already exist, we check if the torrent stats have been scraped already very recently and if so, we stop there to avoid abuse of the /stats/:id route
if (CurrentData.Seeders == 0 && CurrentData.Leechers == 0 && CurrentData.Completed == 0) && time.Since(CurrentData.LastScrape).Minutes() <= config.Get().Scrape.MaxStatScrapingFrequencyUnknown {
//Unknown stats but has been scraped less than X minutes ago (X being the limit set in the config file)
return
}
if (CurrentData.Seeders != 0 || CurrentData.Leechers != 0 || CurrentData.Completed != 0) && time.Since(CurrentData.LastScrape).Minutes() <= config.Get().Scrape.MaxStatScrapingFrequency {
//Known stats but has been scraped less than X minutes ago (X being the limit set in the config file)
return
}
}
var Trackers []string
if len(Trackers) > 3 {
for _, line := range strings.Split(torrent.Trackers[3:], "&tr=") {
tracker, error := url.QueryUnescape(line)
if error == nil && strings.Contains(tracker, "udp://") {
Trackers = append(Trackers, tracker)
}
//Cannot scrape from http trackers so don't put them in the array
}
}
for _, line := range config.Get().Torrents.Trackers.Default {
if !contains(Trackers, line) {
Trackers = append(Trackers, line)
}
}
stats := goscrape.Single(Trackers, []string{
torrent.Hash,
})[0]
//Single() returns an array which contain results for each torrent Hash it is fed, since we only feed him one we want to directly access the results
//If we put seeders on -1, the script instantly knows the fetching did not give any result, avoiding having to check all three stats below and in view.jet.html's javascript
if stats.Seeders == 0 && stats.Leechers == 0 && stats.Completed == 0 {
stats.Seeders = -1
}
c.JSON(200, gin.H{
"seeders": stats.Seeders,
"leechers": stats.Leechers,
"downloads": stats.Completed,
})
if stats.Seeders == -1 {
stats.Seeders = 0
}
if !statsExists {
torrent.Scrape = torrent.Scrape.Create(uint(id), uint32(stats.Seeders), uint32(stats.Leechers), uint32(stats.Completed), time.Now())
//Create entry in the DB because none exist
} else {
//Entry in the DB already exists, simply update it
if (CurrentData.Seeders == 0 && CurrentData.Leechers == 0 && CurrentData.Completed == 0) || (stats.Seeders != 0 && stats.Leechers != 0 && stats.Completed != 0 ) {
torrent.Scrape = &models.Scrape{uint(id), uint32(stats.Seeders), uint32(stats.Leechers), uint32(stats.Completed), time.Now()}
} else {
torrent.Scrape = &models.Scrape{uint(id), uint32(CurrentData.Seeders), uint32(CurrentData.Leechers), uint32(CurrentData.Completed), time.Now()}
}
//Only overwrite stats if the old one are Unknown OR if the current ones are not unknown, preventing good stats from being turned into unknown own but allowing good stats to be updated to more reliable ones
torrent.Scrape.Update(false)
}
return
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}