forked from tnychn/torrodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrodle.go
173 lines (157 loc) · 4.97 KB
/
torrodle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package torrodle
import (
"sort"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"github.com/a1phat0ny/torrodle/models"
"github.com/a1phat0ny/torrodle/providers/leetx"
"github.com/a1phat0ny/torrodle/providers/limetorrents"
"github.com/a1phat0ny/torrodle/providers/rarbg"
"github.com/a1phat0ny/torrodle/providers/sukebei"
"github.com/a1phat0ny/torrodle/providers/thepiratebay"
"github.com/a1phat0ny/torrodle/providers/torrentz"
"github.com/a1phat0ny/torrodle/providers/yify"
)
const (
CategoryAll string = "ALL"
CategoryMovie string = "MOVIE"
CategoryTV string = "TV"
CategoryAnime string = "ANIME"
CategoryPorn string = "PORN"
SortByDefault string = "default"
SortBySeeders string = "seeders"
SortByLeechers string = "leechers"
SortBySize string = "size"
)
// Expose all the providers
var SukebeiProvider = sukebei.New()
var ThePirateBayProvider = thepiratebay.New()
var LimeTorrentsProvider = limetorrents.New()
var Torrentz2Provider = torrentz.New()
var RarbgProvider = rarbg.New()
var LeetxProvider = leetx.New()
var YifyProvider = yify.New()
var AllProviders = [...]models.ProviderInterface{
SukebeiProvider,
ThePirateBayProvider,
LimeTorrentsProvider,
Torrentz2Provider,
RarbgProvider,
LeetxProvider,
YifyProvider,
}
// ListProviderResults lists all results queried from this specific provider only.
// It sorts the results and returns at most {count} results.
func ListProviderResults(provider models.ProviderInterface, query string, count int, category string, sortBy string) []models.Source {
var sources []models.Source
categories := provider.GetCategories()
caturl := GetCategoryURL(category, categories)
if caturl == "" {
logrus.Warningf("'%v' provider does not support category '%v', getting default category (ALL)...", provider.GetName(), category)
}
sources, err := provider.Search(query, count, caturl)
if err != nil {
logrus.Fatalln(err)
}
if len(sources) == 0 {
logrus.Warningf("No torrents found via '%v'\n", provider.GetName())
}
results := GetSortedResults(sources, sortBy)
if count > len(results) {
count = len(results)
}
return results[:count]
}
// ListResults lists all results queried from all the specified providers.
// It sorts the results after collected all the sorted results from different providers.
// Returns at most {count} results.
func ListResults(providers []interface{}, query string, count int, category string, sortBy string) []models.Source {
argProviders := []models.ProviderInterface{}
for _, p := range providers {
switch p.(type) {
case string:
for _, p2 := range AllProviders {
if p2.GetName() == p.(string) {
argProviders = append(argProviders, p2)
}
}
case models.ProviderInterface:
argProviders = append(argProviders, p.(models.ProviderInterface))
default:
logrus.Fatalln("Invalid interface type in 'providers': only 'string' and 'models.ProviderInterface' are accepted")
}
}
// Init spinner
var s *spinner.Spinner
showSpinner := logrus.GetLevel() <= logrus.WarnLevel
if count > 500 {
logrus.Warningln("'count' should not be larger than 500, set to 500 automatically")
count = 500
}
// Get results from providers
results := []models.Source{}
for _, provider := range argProviders {
if showSpinner {
c := color.New(color.FgYellow, color.Bold)
s = spinner.New(spinner.CharSets[33], 100*time.Millisecond)
s.Color("fgBlue")
s.Suffix = c.Sprint(" Waiting for ") + color.GreenString(provider.GetName()) + c.Sprint(" ...")
s.Start()
}
sources := ListProviderResults(provider, query, count, category, sortBy)
results = append(results, sources...)
if showSpinner {
s.Stop()
}
}
logrus.Infof("Returning %d results in total...\n", len(results))
results = GetSortedResults(results, sortBy)
if count > len(results) {
count = len(results)
}
return results[:count]
}
// GetCategoryURL returns CategoryURL according to the category name (constant).
func GetCategoryURL(category string, categories models.Categories) models.CategoryURL {
var caturl models.CategoryURL
switch strings.ToUpper(category) {
case CategoryAll:
caturl = categories.All
case CategoryMovie:
caturl = categories.Movie
case CategoryTV:
caturl = categories.TV
case CategoryAnime:
caturl = categories.Anime
case CategoryPorn:
caturl = categories.Porn
default:
logrus.Fatalf("Invalid category: %v\n", category)
}
return caturl
}
func GetSortedResults(results []models.Source, sortBy string) []models.Source {
// Sort results
switch strings.ToLower(sortBy) {
case SortByDefault:
// nothing to do
case SortBySeeders:
sort.Slice(results, func(i, j int) bool {
return results[i].Seeders > results[j].Seeders
})
case SortByLeechers:
sort.Slice(results, func(i, j int) bool {
return results[i].Leechers > results[j].Leechers
})
case SortBySize:
sort.Slice(results, func(i, j int) bool {
return results[i].FileSize > results[j].FileSize
})
default:
logrus.Fatalf("Invalid SortBy: %v", sortBy)
}
return results
}