forked from tnychn/torrodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyify.go
148 lines (134 loc) · 3.93 KB
/
yify.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
package yify
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"github.com/sirupsen/logrus"
"github.com/stl3/torgo/models"
"github.com/stl3/torgo/request"
"github.com/stl3/torgo/utils"
)
const (
Name = "YIFY"
// Site = "https://yts.am"
Site = "https://yts.mx"
// apiURL = "https://yts.am/api"
apiURL = "https://yts.mx/api"
)
var trackers = [...]string{
"udp://open.demonii.com:1337/announce",
"udp://tracker.openbittorrent.com:80",
"udp://tracker.coppersurfer.tk:6969",
"udp://glotorrents.pw:6969/announce",
"udp://tracker.opentrackr.org:1337/announce",
"udp://torrent.gresille.org:80/announce",
"udp://p4p.arenabg.com:1337",
"udp://tracker.leechers-paradise.org:6969",
"http://track.one:1234/announce",
"udp://track.two:80",
}
type provider struct {
models.Provider
}
func New() models.ProviderInterface {
provider := &provider{}
provider.Name = Name
provider.Site = Site
provider.Categories = models.Categories{
All: "/v2/list_movies.json?query_term=%v&limit=50&page=%d",
Movie: "/v2/list_movies.json?query_term=%v&limit=50&page=%d",
// Movie: "/v2/list_movies.json?query_term=%v&limit=50",
} // this provider can only search for movies
return provider
}
type apiResponse struct {
Status string `json:"status"`
StatusMessage string `json:"status_message"`
Data struct {
Movies []struct {
URL string `json:"url"`
Title string `json:"title"`
TitleLong string `json:"title_long"`
Torrents []struct {
URL string `json:"url"`
Hash string `json:"hash"`
Quality string `json:"quality"`
Type string `json:"type"`
Seeds int `json:"seeds"`
Peers int `json:"peers"`
SizeBytes int64 `json:"size_bytes"`
} `json:"torrents"`
} `json:"movies"`
} `json:"data"`
}
func (provider *provider) Search(query string, count int, categoryURL models.CategoryURL) ([]models.Source, error) {
// categoryURL will be ignored since this provider only searches for movies
var results []models.Source
if count <= 0 {
return results, nil
}
if count > 20 { // query was successful but no movie is returned -> YIFY API bug ?
count = 20
}
// Build URL
if categoryURL == "" {
categoryURL = provider.Categories.Movie
}
query = url.QueryEscape(query)
pages := utils.ComputePageCount(count, 50)
surl := fmt.Sprintf(string(categoryURL), query, pages)
logrus.Debugf("YIFY: pages=%d\n", pages)
// Extract sources
logrus.Infoln("YIFY: Getting search results...")
_, resp, err := request.Get(nil, apiURL+surl, nil)
if err != nil {
return results, err
}
response := apiResponse{}
if err = json.Unmarshal([]byte(resp), &response); err != nil {
return results, err
}
status := response.Status
msg := response.StatusMessage
logrus.Debugln("YIFY: Message ->", msg)
if status != "ok" {
return results, errors.New("YIFY: returned a non-ok")
}
logrus.Infoln("YIFY: Extracting sources...")
data := response.Data
movies := data.Movies
for _, movie := range movies {
source := models.Source{
From: provider.Name,
Title: movie.TitleLong,
URL: movie.URL,
}
torrents := movie.Torrents
for _, torrent := range torrents {
s := source
s.Title += " " + torrent.Quality + " " + torrent.Type + " YIFY"
s.Seeders = torrent.Seeds
s.Leechers = torrent.Peers
s.FileSize = torrent.SizeBytes
// filter out invalid sources
if s.Seeders == 0 {
continue
}
// build magnet uri
hash := torrent.Hash
encodedName := url.PathEscape(movie.Title)
magnet := fmt.Sprintf("magnet:?xt=urn:btih:%v&dn=%v", hash, encodedName)
for _, tracker := range trackers {
magnet += "&tr=" + tracker
}
s.Magnet = magnet
results = append(results, s)
}
}
logrus.Infof("YIFY: Found %d results\n", len(results))
if count > len(results) {
count = len(results)
}
return results[:count], nil
}