-
Notifications
You must be signed in to change notification settings - Fork 77
/
b.go
111 lines (97 loc) · 2.32 KB
/
b.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
package browser
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/EDDYCJY/fake-useragent/setting"
"github.com/EDDYCJY/fake-useragent/spiders"
"github.com/EDDYCJY/fake-useragent/useragent"
"github.com/EDDYCJY/fake-useragent/useragent/cache"
)
type browser struct {
Client
Cache
}
type Client struct {
MaxPage int
Delay time.Duration
Timeout time.Duration
}
type Cache struct {
UpdateFile bool
}
var defaultBrowser = NewBrowser(Client{
MaxPage: setting.BROWSER_MAX_PAGE,
Delay: setting.HTTP_DELAY,
Timeout: setting.HTTP_TIMEOUT,
}, Cache{})
func NewBrowser(client Client, cache Cache) *browser {
maxPage := setting.GetMaxPage(client.MaxPage)
delay := setting.GetDelay(client.Delay)
timeout := setting.GetTimeout(client.Timeout)
b := browser{
Client: Client{
MaxPage: maxPage,
Delay: delay,
Timeout: timeout,
},
Cache: Cache{
UpdateFile: cache.UpdateFile,
},
}
return b.load()
}
func (b *browser) load() *browser {
fileCache := cache.NewFileCache(cache.GetTempDir(), fmt.Sprintf(setting.TEMP_FILE_NAME, setting.VERSION))
fileExist, err := fileCache.IsExist()
if err != nil {
log.Fatalf("fileCache.IsExist err: %v", err)
}
// handle cache.
if b.UpdateFile == false {
var (
isCache bool
cacheContent []byte
m map[string][]string
)
if fileExist == true {
cacheContent, err = fileCache.Read()
if err != nil {
log.Fatalf("fileCache.Read err: %v", err)
}
isCache = true
} else {
rawCache := cache.NewRawCache(setting.CACHE_URL, fmt.Sprintf(setting.TEMP_FILE_NAME, setting.CACHE_VERSION))
rawResp, rawExist, err := rawCache.Get()
if err == nil && rawExist == true {
defer rawResp.Body.Close()
rawRead, err := rawCache.Read(rawResp.Body)
if err == nil && len(rawRead) > 0 {
cacheContent = rawRead
isCache = true
}
}
}
if isCache == true {
json.Unmarshal(cacheContent, &m)
useragent.UA.SetData(m)
if fileExist == false {
fileCache.WriteJson(useragent.UA.GetAll())
}
return b
}
}
// handle origin.
s := spiders.NewBrowserSpider()
s.AppendBrowser(b.MaxPage)
s.StartBrowser(b.Delay, b.Timeout)
if fileExist == true && b.UpdateFile == true {
err := fileCache.Remove()
if err != nil {
log.Fatalf("fileCache.Remove err: %v", err)
}
}
fileCache.WriteJson(useragent.UA.GetAll())
return b
}