Bettor sort

Use absolute paths
Cleaned up stopping the torrents
Start the torrents that are selected that are below 1.0
Fix problem with duplicate links
This commit is contained in:
lordwelch 2018-01-17 14:27:52 -07:00
parent 8bf4f5bf5c
commit 7be6edcc45
2 changed files with 43 additions and 143 deletions

70
main.go
View File

@ -44,11 +44,11 @@ func main() {
initialize()
go stdinLoop(stdC)
for i := 0; true; {
i++
for i := 0; true; i++ {
fmt.Println(i)
select {
case <-time.After(time.Minute * 15):
case TIME := <-time.After(time.Minute * 15):
fmt.Println(TIME)
download()
case current := <-stdC:
@ -96,22 +96,26 @@ func stdinLoop(C chan *SceneVideoTorrent) {
// Get hashes of torrents that were previously selected then remove the link to them
func getLinks() (hash []string) {
// get files in selected dir
selectedDir := filepath.Join(unselectedDir, "../")
selectedFolder, _ := os.Open(selectedDir)
selectedFolder, _ := os.Open(args.PATH)
defer selectedFolder.Close()
selectedNames, _ := selectedFolder.Readdirnames(0)
// Add hashes of currently selected torrents
for _, lnk := range selectedNames {
target, err := os.Readlink(filepath.Join(selectedDir, lnk))
target, err := os.Readlink(filepath.Join(args.PATH, lnk))
if err == nil {
if filepath.Base(filepath.Dir(target)) == "unselected" {
hash = append(hash, process(target).Meta.Hash)
if filepath.Dir(target) == unselectedDir {
fakeTorrent := process(target)
realTorrent := CurrentTorrents[fakeTorrent.Title][fakeTorrent.Season][fakeTorrent.Episode].Ep[0]
if realTorrent.Meta.Hash != fakeTorrent.Meta.Hash {
fmt.Printf("Better file found for: %s S%sE%s\n", realTorrent.Title, realTorrent.Season, realTorrent.Episode)
err = os.Remove(filepath.Join(args.PATH, filepath.Base(fakeTorrent.Meta.FilePath)))
os.Symlink(realTorrent.Meta.FilePath, filepath.Join(args.PATH, filepath.Base(realTorrent.Meta.FilePath)))
hash = append(hash, fakeTorrent.Meta.Hash)
}
}
}
}
selectedNames, _ = selectedFolder.Readdirnames(0)
fmt.Println(selectedNames)
return
}
@ -121,29 +125,15 @@ func download() {
err error
)
hash := getLinks()
if Transmission != nil {
stopDownloads(hash)
}
for _, s := range CurrentTorrents {
for _, se := range s {
for _, ep := range se {
var CurrentHash bool
for _, HASH := range hash {
if HASH == ep.Ep[0].Meta.Hash {
CurrentHash = HASH == ep.Ep[0].Meta.Hash
break
}
}
if !CurrentHash {
fmt.Printf("Better file found for: %s S%sE%s\n", ep.Ep[0].Title, ep.Ep[0].Season, ep.Ep[0].Episode)
os.Remove(filepath.Join(filepath.Join(unselectedDir, "../"), filepath.Base(ep.Ep[0].Meta.FilePath)))
os.Symlink(ep.Ep[0].Meta.FilePath, filepath.Join(filepath.Join(unselectedDir, "../"), filepath.Base(ep.Ep[0].Meta.FilePath)))
}
CurrentHashes = append(CurrentHashes, ep.Ep[0].Meta.Hash)
}
}
}
if Transmission != nil {
stopDownloads(hash)
time.Sleep(time.Second * 30)
tmap, _ := Transmission.GetTorrentMap()
if err != nil {
@ -173,7 +163,6 @@ func download() {
}
}
}
}
func addtorrent(St SeriesTorrent, torrent *SceneVideoTorrent) {
@ -200,7 +189,7 @@ func addtorrent(St SeriesTorrent, torrent *SceneVideoTorrent) {
if i+1 == 0 {
panic("You do not exist in a world that I know of")
}
St[torrent.Season][torrent.Episode].Release[v] = -1
St[torrent.Season][torrent.Episode].Release[v] = (i + 1) * -1
}
for i, v := range args.TAGS {
if i+1 == 0 {
@ -208,6 +197,7 @@ func addtorrent(St SeriesTorrent, torrent *SceneVideoTorrent) {
}
St[torrent.Season][torrent.Episode].Tags[v] = i + 1
}
St[torrent.Season][torrent.Episode].Tags["nuked"] = -99999
}
St[torrent.Season][torrent.Episode].Add(torrent)
}
@ -232,20 +222,21 @@ func stopDownloads(hash []string) {
}
}
if run {
thash := make([]*transmission.Torrent, 0, len(hash))
// Stops torrents from transmission that are not selected this time
for _, CHash := range hash {
v, ok := tmap[CHash]
if ok {
current := scene.Parse(v.Name)
if CurrentTorrents[current.Title][current.Season][current.Episode].Ep[0].Meta.Hash != CHash {
thash = append(thash, v)
}
v.Stop()
}
}
// Removes torrents from transmission that are not selected this time
for _, torrent := range thash {
torrent.Stop()
for _, CHash := range CurrentHashes {
v, ok := tmap[CHash]
if ok {
if v.UploadRatio < 1 {
v.Start()
}
}
}
}
}
@ -267,7 +258,8 @@ func initialize() {
CurrentTorrents[title] = make(SeriesTorrent, 10)
}
unselectedDir, _ = filepath.Abs(filepath.Join(args.PATH, "unselected/"))
args.PATH, _ = filepath.Abs(args.PATH)
unselectedDir = filepath.Join(args.PATH, "unselected/")
// Load all downloaded torrents
if !args.NEW {
@ -328,6 +320,6 @@ func process(torrentFile string) *SceneVideoTorrent {
vt.Torrent = NewTorrent(*mt)
vt.Parse(strings.TrimSuffix(vt.Name, filepath.Ext(vt.Name)))
//fmt.Println(vt.Original)
//fmt.Println(vt.Title)
//fmt.Println(vt)
return vt
}

116
type.go
View File

@ -54,26 +54,6 @@ type EpisodeTorrent struct {
type SeasonTorrent map[string]*EpisodeTorrent
type SeriesTorrent map[string]SeasonTorrent
func OrderedBy(fns ...func(int, int) bool) func(int, int) bool {
return func(i, j int) bool {
// Try all but the last comparison.
for _, less := range fns {
switch {
case less(i, j):
// i < j, so we have a decision.
return true
case less(j, i):
// i > j, so we have a decision.
return false
}
// i == j; try the next comparison.
}
// All comparisons to here said "equal", so just return whatever
// the final comparison reports.
return fns[len(fns)-1](i, j)
}
}
func NewTorrent(mt MetaTorrent) (T Torrent) {
if mt.Info.Length == 0 {
for _, path := range mt.Info.Files {
@ -110,83 +90,7 @@ func (Mt *MetaTorrent) ReadFile(r *os.File) error {
}
func (Vt SceneVideoTorrent) String() string {
return Vt.Torrent.Meta.FilePath
}
func (Et *EpisodeTorrent) ByExt(i, j int) bool {
var (
ii int
ij int
)
if filepath.Ext(Et.Ep[i].Meta.Info.Name) != ".mkv" {
ii = 1
}
if filepath.Ext(Et.Ep[j].Meta.Info.Name) != ".mkv" {
ij = 1
}
return ii < ij
}
func (Et *EpisodeTorrent) ByRelease(i, j int) bool {
var (
ii int
ij int
ret bool
)
ii = Et.Release[Et.Ep[i].Release]
ij = Et.Release[Et.Ep[j].Release]
if ii == 0 {
ii = 999999
}
if ij == 0 {
ij = 999999
}
if ii == ij {
ret = Et.Ep[i].Release > Et.Ep[j].Release
//fmt.Println(Et.Ep[i].Release, ">", Et.Ep[j].Release, "=", ret, Et)
} else {
ret = ii < ij
}
return ret
}
func (Et *EpisodeTorrent) ByTag(i, j int) bool {
var (
ii int
ij int
ret bool
)
for k := range Et.Ep[i].Tags {
if Et.Tags[k] > 0 {
ii++
}
}
for k := range Et.Ep[j].Tags {
if Et.Tags[k] > 0 {
ij++
}
}
if ii == ij {
ret = len(Et.Ep[i].Tags) < len(Et.Ep[j].Tags)
//fmt.Println(len(Et.Ep[i].Tags), "<", len(Et.Ep[j].Tags), "=", ret)
} else {
ret = ii > ij
}
return ret
}
func (Et *EpisodeTorrent) ByRes(i, j int) bool {
var ret bool
ret = Et.Ep[i].Resolution > Et.Ep[j].Resolution
//fmt.Println(Et.Ep[i].Resolution, ">", Et.Ep[j].Resolution, "=", ret)
if Et.Res == Et.Ep[i].Resolution && Et.Ep[i].Resolution != Et.Ep[j].Resolution {
ret = true
}
return ret
return Vt.Scene.String()
}
func (Et *EpisodeTorrent) Len() int {
@ -199,21 +103,25 @@ func (Et *EpisodeTorrent) Swap(i, j int) {
}
func (Et *EpisodeTorrent) Less(i, j int) bool {
return OrderedBy(Et.ByExt, Et.ByRes, Et.ByRelease, Et.ByTag)(i, j)
return Et.score(i) > Et.score(j)
}
func (Et *EpisodeTorrent) score(i int) int {
var score int
if filepath.Ext(Et.Ep[i].Meta.FilePath) == mkv {
score += 10000
if filepath.Ext(Et.Ep[i].Meta.FilePath) == ".mkv" {
score += 1000
}
if Et.Ep[i].Resolution == Et.Res {
score += 9000
score += 900
} else {
score += Et.Ep[i].Resolution * 1000
score += int(Et.Ep[i].Resolution) * 100
}
score += Et.Release[Et.Ep[i].Release] * 100
score += Et.Tags[Et.Ep[i].Tags]
score += Et.Release[Et.Ep[i].Release] + 1
for k := range Et.Ep[i].Tags {
score += Et.Tags[k]
}
score += len(Et.Ep[i].Tags)
return score
}
func (Et *EpisodeTorrent) Add(Vt *SceneVideoTorrent) {