4284c6abc8f975be18e7148fdd366575ef66f1dd — Louis Solofrizzo 6 months ago e3f2801
torrent-resolver: Add torrent resolver in tree

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>
A torrent-resolver/CMakeLists.txt => torrent-resolver/CMakeLists.txt +11 -0
@@ 0,0 1,11 @@
+ generate_git_go_version(version.go main)
+ add_go_component(tpb-torrent-resolver
+     main.go
+     version.go
+     config.go
+ )
+ 
+ upload_artifacts(tpb-torrent-resolver tpb-torrent-resolver x86_64)
+ 
+ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tpb-torrent-resolver DESTINATION bin
+     PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

A torrent-resolver/config.go => torrent-resolver/config.go +73 -0
@@ 0,0 1,73 @@
+ package main
+ 
+ import (
+ 	"github.com/jinzhu/gorm"
+ 	_ "github.com/jinzhu/gorm/dialects/postgres"
+ 	_ "github.com/jinzhu/gorm/dialects/sqlite"
+ 	"gopkg.in/yaml.v2"
+ 	"io/ioutil"
+ )
+ 
+ type TpbCrawlerConfig struct {
+ 	TPB struct {
+ 		Categories []int  `yaml:"categories"`
+ 		Url        string `yaml:"url"`
+ 	} `yaml:"tpb"`
+ 	Database struct {
+ 		Driver string `yaml:"driver"`
+ 		Url    string `yaml:"url"`
+ 	} `yaml:"database"`
+ }
+ 
+ var Config TpbCrawlerConfig
+ var DB *gorm.DB
+ 
+ type Torrent struct {
+ 	gorm.Model
+ 	Title       string
+ 	Url         string
+ 	Type        int
+ 	Languages   string
+ 	Tags        string
+ 	Uploaded    string
+ 	User        string
+ 	Magnet      uint
+ 	Description string
+ }
+ 
+ type Magnet struct {
+ 	gorm.Model
+ 	Url      string
+ 	Size     string
+ 	Seeders  int
+ 	Leechers int
+ 	Hash     string
+ }
+ 
+ type File struct {
+ 	gorm.Model
+ 	Magnet uint
+ 	Name   string
+ 	Size   string
+ }
+ 
+ func ReadConfig(filename string) {
+ 	source, err := ioutil.ReadFile(filename)
+ 	if err != nil {
+ 		panic("Cannot open configuration file: " + err.Error())
+ 	}
+ 
+ 	err = yaml.Unmarshal(source, &Config)
+ 	if err != nil {
+ 		panic("Cannot read configuration file: " + err.Error())
+ 	}
+ 
+ 	DB, err = gorm.Open(Config.Database.Driver, Config.Database.Url)
+ 	if err != nil {
+ 		panic("Failed to connect to database:" + err.Error())
+ 	}
+ 
+ 	DB.AutoMigrate(&Torrent{})
+ 	DB.AutoMigrate(&Magnet{})
+ 	DB.AutoMigrate(&File{})
+ }

A torrent-resolver/main.go => torrent-resolver/main.go +83 -0
@@ 0,0 1,83 @@
+ package main
+ 
+ import (
+ 	"flag"
+ 	"fmt"
+ 	"github.com/anacrolix/torrent"
+ 	"os"
+ 	"strconv"
+ 	"strings"
+ 	"sync"
+ 	"time"
+ )
+ 
+ func resolveTorrent(magnet Magnet, c *torrent.Client, wg *sync.WaitGroup) {
+ 	var newfile File
+ 	var size int64
+ 
+ 	t, _ := c.AddMagnet(magnet.Url)
+ 	<-t.GotInfo()
+ 	defer wg.Done()
+ 
+ 	newfile.ID = 0
+ 	DB.First(&newfile, "magnet = ?", magnet.ID)
+ 	if newfile.ID == 0 {
+ 		size = 0
+ 		for _, file := range t.Files() {
+ 			newfile.ID = 0
+ 			newfile.Name = file.DisplayPath()
+ 			newfile.Size = strconv.FormatInt(file.Length(), 10)
+ 			newfile.Magnet = magnet.ID
+ 			size += file.Length()
+ 			DB.Create(&newfile)
+ 		}
+ 	}
+ 
+ 	stats := t.Stats()
+ 	magnet.Seeders = stats.ActivePeers + stats.ConnectedSeeders + stats.HalfOpenPeers
+ 	magnet.Leechers = stats.TotalPeers - stats.PendingPeers
+ 	magnet.Hash = strings.ToUpper(t.InfoHash().HexString())
+ 	magnet.Size = strconv.FormatInt(size, 10)
+ 	DB.Save(&magnet)
+ 	fmt.Printf("%s: Done!\n", t.Info().Name)
+ }
+ 
+ func resolveAllTorrents() {
+ 	var magnets []Magnet
+ 	var wg sync.WaitGroup
+ 
+ 	c, _ := torrent.NewClient(nil)
+ 	defer c.Close()
+ 
+ 	fmt.Printf("Waiting for torrent client to start-up\n")
+ 	time.Sleep(15 * time.Second)
+ 
+ 	DB.Find(&magnets)
+ 
+ 	for _, magnet := range magnets {
+ 		wg.Add(1)
+ 		go resolveTorrent(magnet, c, &wg)
+ 		/* Wait a bit before spawning another goroutine */
+ 		time.Sleep(1 * time.Second)
+ 	}
+ 	fmt.Printf("Waiting for threads\n")
+ 	wg.Wait()
+ }
+ 
+ func main() {
+ 	version := flag.Bool("version", false, "Show version")
+ 	filename := flag.String("config", "/etc/tpb-crawler.conf", "The yaml configuration file")
+ 
+ 	flag.Parse()
+ 
+ 	if *version {
+ 		fmt.Printf("%s-%s (%s)\n", GitTag(), GitRev(), GitBranch())
+ 		os.Exit(0)
+ 	}
+ 
+ 	ReadConfig(*filename)
+ 
+ 	for {
+ 		resolveAllTorrents()
+ 	}
+ }

A torrent-resolver/version.go => torrent-resolver/version.go +23 -0
@@ 0,0 1,23 @@
+ package main
+ 
+     const GITREV     = "N/A"
+     const GITTAG     = "N/A"
+     const GITBRANCH  = "N/A"
+     const GITVERSION = ""
+ 
+     func GitVersion() string {
+         return GITVERSION
+     }
+ 
+     func GitTag() string {
+         return GITTAG
+     }
+ 
+     func GitBranch() string {
+         return GITBRANCH
+     }
+ 
+     func GitRev() string {
+         return GITREV
+     }
+     < 
\ No newline at end of file