-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
84 lines (74 loc) · 1.72 KB
/
search.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
package main
import (
"net/http"
"net/url"
"sync"
"astuart.co/goq"
)
//TorrentInfo torrent information
type TorrentInfo struct {
Name string `json:"name"`
TorrentURL string `json:"url"`
MagnetLink string `json:"magnet"`
Seeds string `json:"seed"`
Leeches string `json:"leech"`
Size string `json:"size"`
}
type rutorResults struct {
Files []struct {
Data []string `goquery:"td"`
Links []string `goquery:"a,[href]"`
Seed string `goquery:"span.green,text"`
Leech string `goquery:"span.red,text"`
} `goquery:"#index table tbody tr,html"`
}
func searchRutor(term string) (torrents []TorrentInfo) {
var wg sync.WaitGroup
var m sync.Mutex
cats := []string{"1", "5", "7"} // "5" - foreign, "1" - russian, "7" - cartoons
for _, cat := range cats {
wg.Add(1)
go func(cat string) {
defer wg.Done()
if results, err := searchRutorCategory(cat, term); err == nil {
m.Lock()
torrents = append(torrents, results...)
m.Unlock()
}
}(cat)
}
wg.Wait()
return
}
func searchRutorCategory(cat string, term string) (torrents []TorrentInfo, err error) {
addr, err := url.Parse("http://rutor.is/search/0/" + cat + "/100/2/" + term)
if err != nil {
return
}
res, err := http.Get(addr.String())
if err != nil {
return
}
defer res.Body.Close()
var r rutorResults
err = goq.NewDecoder(res.Body).Decode(&r)
if err != nil {
return
}
for _, t := range r.Files {
torrent := TorrentInfo{
Seeds: t.Seed,
Leeches: t.Leech,
}
if len(t.Data) > 3 {
torrent.Name = t.Data[1]
torrent.Size = t.Data[3]
}
if len(t.Links) > 1 {
torrent.TorrentURL = "http://rutor.is" + t.Links[0]
torrent.MagnetLink = t.Links[1]
torrents = append(torrents, torrent)
}
}
return
}