-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotword.go
More file actions
123 lines (109 loc) · 3.25 KB
/
Copy pathhotword.go
File metadata and controls
123 lines (109 loc) · 3.25 KB
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
package finder
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/ymzuiku/hit"
)
const (
// HotWord kind
HotWordBaidu = "baidu"
HotWordTouTiao = "toutiao"
HotWord360So = "so"
HotWordSogou = "sogou"
HotWordSm = "sm"
// Url
BaiduHotWordsUrl = "https://top.baidu.com/api/board?platform=wise&tab=realtime&tag=%7B%7D"
SoHotWordsUrl = "https://m.so.com/home/data?types=Hotwords&fmt=json"
SoGouHotWordsUrl = "https://wap.sogou.com/data/hotwords/sogou_app.js"
TouTiaoHotWordsUrl = "https://i.snssdk.com/hot-event/hot-board/?count=50&tab_name=stream&origin=hot_board"
SmHotWordsUrl = "http://api.m.sm.cn/rest?method=tools.hot&size=0"
// Suggest Url
DefaultBaiduSuggestUrlFmt = "http://m.baidu.com/from=${from}/s?word=${keyword}&bd_page_type=1"
DefaultSoGouSuggestUrlFmt = "https://wap.sogou.com/web/searchList.jsp?keyword=${keyword}&e=${from}"
Default360SoSuggestUrlFmt = "https://m.so.com/s?q=${keyword}&src=${from}"
DefaultTouTiaoSuggestUrlFmt = "https://so.toutiao.com/search?keyword=${keyword}&traffic_source=${from}&original_source=1&source=client"
DefaultSmSuggestUrlFmt = "http://m.yz2.sm.cn/s?q=${keyword}&by=hot&from=${from}"
// Config
HotWordHttpTimeout = 60
)
// ///////////////////////////
// HotWord Result
// ///////////////////////////
type HotWordResult struct {
Keyword string
Weight int
SuggestUrl string
}
func (wr *HotWordResult) Json() string {
body, _ := json.Marshal(wr)
return string(body)
}
// ///////////////////////////
// HotWord Finder
// ///////////////////////////
type HotWordFinder struct {
Provider string
From string
TTL int
SuggestUrl string
}
func (hwf *HotWordFinder) Find(from string, suggestUrl string) ([]*HotWordResult, error) {
switch hwf.Provider {
case HotWordBaidu:
return BaiduHotWordFind(hwf, from, suggestUrl)
case HotWord360So:
return SoHotWordFind(hwf, from, suggestUrl)
case HotWordSogou:
return SoGouHotWordFind(hwf, from, suggestUrl)
case HotWordTouTiao:
return TouTiaoHotWordFind(hwf, from, suggestUrl)
case HotWordSm:
return SmHotWordFind(hwf, from, suggestUrl)
default:
break
}
return nil, errors.New(fmt.Sprintf("not found %s provider", hwf.Provider))
}
func (hwf *HotWordFinder) GetSuggestUrl(keyword string, from string, suggestUrl string) string {
from = hit.If(from, from, hwf.From).(string)
suggestUrl = hit.If(suggestUrl, suggestUrl, hwf.SuggestUrl).(string)
switch hwf.Provider {
case HotWordBaidu:
suggestUrl = DefaultBaiduSuggestUrlFmt
break
case HotWord360So:
suggestUrl = Default360SoSuggestUrlFmt
break
case HotWordSogou:
suggestUrl = DefaultSoGouSuggestUrlFmt
break
case HotWordTouTiao:
suggestUrl = DefaultTouTiaoSuggestUrlFmt
break
case HotWordSm:
suggestUrl = DefaultSmSuggestUrlFmt
break
default:
break
}
// replace
suggestUrl = strings.ReplaceAll(suggestUrl, "${from}", from)
suggestUrl = strings.ReplaceAll(suggestUrl, "${keyword}", keyword)
return suggestUrl
}
func (hwf *HotWordFinder) Close() {
// nothing
}
func (hwf *HotWordFinder) Version() string {
return VERSION
}
func NewHotWordFinder(provider string, from string, suggestUrl string, ttl int) *HotWordFinder {
return &HotWordFinder{
Provider: provider,
From: from,
TTL: ttl,
SuggestUrl: suggestUrl,
}
}