-
Notifications
You must be signed in to change notification settings - Fork 9
/
article.go
257 lines (230 loc) · 6.18 KB
/
article.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package wxspider
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/yizenghui/sda/wechat"
)
func init() {
DB().AutoMigrate(&Article{})
}
//SpiderArticle 采集文章并保存到本地
func SpiderArticle(urlStr string) error {
var a Article
article, err := wechat.Find(urlStr)
if err == nil {
if article.URL == "" {
return errors.New("不支持该链接!")
}
a.GetArticleByURL(article.URL)
a.AppID = article.AppID
a.AppName = article.AppName
a.RoundHead = article.RoundHead
a.OriHead = article.OriHead
a.URL = article.URL
a.Title = article.Title
a.Intro = article.Intro
a.Cover = article.Cover
a.Author = article.Author
a.PubAt = article.PubAt
a.Cont = article.Content
a.Body = article.MdContent
a.Copyright = article.Copyright
a.WxID = article.WxID
a.WxIntro = article.WxIntro
a.Video = article.Video
a.Audio = article.Audio
a.Category = `其它`
// data["tags"] = []string{`php`, `golang`}
tags, err := a.AiGetTags()
if err == nil {
var tarr []string
for _, t := range tags.Items {
tarr = append(tarr, t.Tag)
}
if len(tarr) > 0 {
tagStr := strings.Join(tarr, ",")
a.Tags = tagStr
}
}
// 增加夜读标签
if strings.Contains(a.Title, `夜读`) && a.Audio != `` {
if a.Tags != `` {
a.Tags = fmt.Sprintf(`%v,夜读`, a.Tags)
} else {
a.Tags = `夜读`
}
}
if len(article.Images) > 0 {
var imgarr []string
for _, img := range article.Images {
if CheckImage(img) {
imgarr = append(imgarr, img)
}
}
if len(imgarr) > 0 {
a.Images = strings.Join(imgarr, ";")
// log.Println(`img`, a.Images)
}
}
// // 增加音频标签
// if a.Audio != `` {
// if a.Tags != `` {
// a.Tags = fmt.Sprintf(`%v,音频`, a.Tags)
// } else {
// a.Tags = `音频`
// }
// }
// // 视频标签
// if a.Video != `` {
// if a.Tags != `` {
// a.Tags = fmt.Sprintf(`%v,视频`, a.Tags)
// } else {
// a.Tags = `视频`
// }
// }
categories, err := a.AiGetCategories()
if err == nil {
var carr1 []string
for _, t := range categories.Item.TopCategory {
carr1 = append(carr1, t.Tag)
}
if len(carr1) > 0 {
categoryStr1 := strings.Join(carr1, ",")
a.Category = categoryStr1 // 一级分类
}
var carr []string
for _, t := range categories.Item.SecondCatrgory { //二级分类
carr = append(carr, t.Tag)
}
if len(carr) > 0 {
categoryStr := strings.Join(carr, ",")
a.Categories = categoryStr
}
}
a.Save()
log.Println("spider", a.ID, a.Title, a.URL, a.Category, a.Categories, a.Tags)
}
return nil
}
//PublishArticle 发布本地文章
func PublishArticle() error {
var a Article
rows := a.GetPlanPublushArticle()
for _, row := range rows {
time.Sleep(time.Second * 2)
pistID, e := PostArticle(row)
if e == nil {
row.PublishAt = time.Now().Unix()
row.PostID = pistID
row.Save()
log.Println("post", row.ID, row.Title, row.URL, row.PublishAt)
} else {
row.PublishAt = -1
row.Save()
log.Println("post err", row.ID, row.Title, row.URL)
}
}
return nil
}
//GetArticles 获取文章列表
func GetArticles() []Article {
var a Article
rows := a.GetArticles()
return rows
}
//PostMessage 发布数据返回消息 成功返回 id 失败返回json
type PostMessage struct {
ID int64 `json:"id"`
Message string `json:"message"`
}
//PostArticle 发布文章
func PostArticle(article Article) (int64, error) {
client := http.Client{}
// 获取系统配置(发送地址和授权令牌)
cf := GetConf()
data := make(url.Values)
data["title"] = []string{article.Title}
data["app_id"] = []string{article.AppID}
data["app_name"] = []string{article.AppName}
data["app_cover"] = []string{article.OriHead}
data["url"] = []string{article.URL}
data["intro"] = []string{article.Intro}
data["body"] = []string{article.Cont}
data["mdbody"] = []string{article.Body}
data["cover"] = []string{article.Cover}
data["author"] = []string{article.Author}
data["wxid"] = []string{article.WxID}
data["wxintro"] = []string{article.WxIntro}
data["tags"] = []string{article.Tags}
data["images"] = []string{article.Images}
data["category"] = []string{article.Category}
data["categories"] = []string{article.Categories}
data["copyright"] = []string{article.Copyright}
data["video"] = []string{article.Video}
data["audio"] = []string{article.Audio}
// post 时把授权密码放在数据包里面
// data["authorization_token"] = []string{cf.PostConfig.AuthorizationToken}
i64, err := strconv.ParseInt(article.PubAt, 10, 64)
if err != nil {
// fmt.Println(err)
return 0, errors.New("时间转化失败")
}
pubAt := time.Unix(i64, 0).Format("2006-01-02 15:04:05")
data["pub_at"] = []string{pubAt}
// 把数据包post到配置的位置
resp, err := client.PostForm(cf.PostConfig.ServeURL, data)
// resp, err := http.NewRequest("POST", cf.PostConfig.ServeURL, strings.NewReader(data.Encode()))
// log.Println(cf.PostConfig.ServeURL)
resp.Header.Add("accept", `application/json`)
resp.Header.Add("Authorization", fmt.Sprintf(`Bearer %v`, cf.PostConfig.AuthorizationToken))
if err != nil {
// log.Println(" %v ", err.Error)
return 0, err
}
// formPost, err := goquery.NewDocumentFromReader(resp.Body)
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
resp.Body.Close()
//byte数组直接转成string,优化内存
// postMsg := string(respBytes)
// postMsg, err := formPost.Html()
// // // panic(err)
// log.Println(" %s ", postMsg)
if err != nil {
// log.Println(" %s ", err.Error)
return 0, err
}
var pm PostMessage
err = json.Unmarshal(respBytes, &pm)
if err != nil {
log.Println(string(respBytes))
return 0, errors.New(`服务器 返回数据出错`)
}
if pm.Message != `` {
return 0, errors.New(pm.Message)
}
fmt.Println(pm)
return pm.ID, nil
// i64, err = strconv.ParseInt(postMsg, 10, 64)
// if err != nil {
// log.Println(postMsg)
// // log.Println(" %s ", err.Error)
// return 0, err
// }
// fmt.Println("posted id", i64)
// return i64, nil
// if b := strings.Contains(postMsg, `mp.weixin.qq.com`); b == true {
// return nil
// }
// return errors.New(`发布失败了`)
}