forked from Sansui233/proxypool
-
Notifications
You must be signed in to change notification settings - Fork 6
/
subscribe.go
78 lines (67 loc) · 1.75 KB
/
subscribe.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
package getter
import (
"io/ioutil"
"strings"
"sync"
"time"
"github.com/One-Piecs/proxypool/log"
"github.com/One-Piecs/proxypool/pkg/proxy"
"github.com/One-Piecs/proxypool/pkg/tool"
)
// Add key value pair to creatorMap(string → creator) in base.go
func init() {
Register("subscribe", NewSubscribe)
}
// Subscribe is A Getter with an additional property
type Subscribe struct {
Url string
}
// Get() of Subscribe is to implement Getter interface
func (s *Subscribe) Get() proxy.ProxyList {
resp, err := tool.GetHttpClient().Get(s.Url)
if err != nil {
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
nodesString, err := tool.Base64DecodeString(string(body))
if err != nil {
return nil
}
nodesString = strings.ReplaceAll(nodesString, "\t", "")
nodes := strings.Split(nodesString, "\n")
return StringArray2ProxyArray(nodes)
}
// Get2Chan() of Subscribe is to implement Getter interface. It gets proxies and send proxy to channel one by one
func (s *Subscribe) Get2ChanWG(pc chan proxy.Proxy, wg *sync.WaitGroup) {
defer wg.Done()
start := time.Now()
nodes := s.Get()
log.Infoln("STATISTIC: Subscribe\tcost=%v\tcount=%d\turl=%s", time.Since(start), len(nodes), s.Url)
for _, node := range nodes {
pc <- node
}
}
func (s *Subscribe) Get2Chan(pc chan proxy.Proxy) {
nodes := s.Get()
log.Infoln("STATISTIC: Subscribe\tcount=%d\turl=%s", len(nodes), s.Url)
for _, node := range nodes {
pc <- node
}
}
func NewSubscribe(options tool.Options) (getter Getter, err error) {
urlInterface, found := options["url"]
if found {
url, err := AssertTypeStringNotNull(urlInterface)
if err != nil {
return nil, err
}
return &Subscribe{
Url: url,
}, nil
}
return nil, ErrorUrlNotFound
}