-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
185 lines (161 loc) · 4.73 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
// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author: polaris polaris@studygolang.com
package model
import (
"encoding/json"
"strconv"
"strings"
"time"
"github.com/ue4-community/learnue/modules/logger"
"xorm.io/xorm"
)
const (
ArticleStatusNew = iota
ArticleStatusOnline
ArticleStatusOffline
)
var LangSlice = []string{"中文", "英文"}
var ArticleStatusSlice = []string{"未上线", "已上线", "已下线"}
// 抓取的文章信息
type Article struct {
Id int `json:"id" xorm:"pk autoincr"`
Domain string `json:"domain"`
Name string `json:"name"`
Title string `json:"title"`
Cover string `json:"cover"`
Author string `json:"author"`
AuthorTxt string `json:"author_txt"`
Lang int `json:"lang"`
PubDate string `json:"pub_date"`
Url string `json:"url"`
Content string `json:"content"`
Txt string `json:"txt"`
Tags string `json:"tags"`
Css string `json:"css"`
Viewnum int `json:"viewnum"`
Cmtnum int `json:"cmtnum"`
Likenum int `json:"likenum"`
Lastreplyuid int `json:"lastreplyuid"`
Lastreplytime OftenTime `json:"lastreplytime"`
Top uint8 `json:"top"`
Markdown bool `json:"markdown"`
GCTT bool `json:"gctt" xorm:"gctt"`
CloseReply bool `json:"close_reply"`
Status int `json:"status"`
OpUser string `json:"op_user"`
Ctime OftenTime `json:"ctime" xorm:"created"`
Mtime OftenTime `json:"mtime" xorm:"<-"`
IsSelf bool `json:"is_self" xorm:"-"`
User *User `json:"-" xorm:"-"`
// 排行榜阅读量
RankView int `json:"rank_view" xorm:"-"`
LastReplyUser *User `json:"last_reply_user" xorm:"-"`
}
func (this *Article) AfterSet(name string, cell xorm.Cell) {
if name == "id" {
this.IsSelf = strconv.Itoa(this.Id) == this.Url
}
}
func (this *Article) BeforeInsert() {
if this.Tags == "" {
this.Tags = AutoTag(this.Title, this.Txt, 4)
}
this.Lastreplytime = NewOftenTime()
}
func (this *Article) AfterInsert() {
go func() {
// AfterInsert 时,自增 ID 还未赋值,这里 sleep 一会,确保自增 ID 有值
for {
if this.Id > 0 {
PublishFeed(this, nil, nil)
return
}
time.Sleep(100 * time.Millisecond)
}
}()
}
func (*Article) TableName() string {
return "articles"
}
type ArticleGCTT struct {
ArticleID int `xorm:"article_id pk"`
Author string
AuthorURL string `xorm:"author_url"`
Translator string
Checker string
URL string `xorm:"url"`
Avatar string `xorm:"-"`
Checkers []string `xorm:"-"`
}
func (*ArticleGCTT) TableName() string {
return "article_gctt"
}
func (this *ArticleGCTT) AfterSet(name string, cell xorm.Cell) {
if name == "checker" {
this.Checkers = strings.Split(this.Checker, ",")
}
}
// 抓取网站文章的规则
type CrawlRule struct {
Id int `json:"id" xorm:"pk autoincr"`
Domain string `json:"domain"`
Subpath string `json:"subpath"`
Lang int `json:"lang"`
Name string `json:"name"`
Title string `json:"title"`
Author string `json:"author"`
InUrl bool `json:"in_url"`
PubDate string `json:"pub_date"`
Content string `json:"content"`
Ext string `json:"ext"`
OpUser string `json:"op_user"`
Ctime string `json:"ctime" xorm:"<-"`
}
func (this *CrawlRule) ParseExt() map[string]string {
if this.Ext == "" {
return nil
}
extMap := make(map[string]string)
err := json.Unmarshal([]byte(this.Ext), &extMap)
if err != nil {
logger.Errorln("parse crawl rule ext error:", err)
return nil
}
return extMap
}
const (
AutoCrawlOn = 0
AutoCrawOff = 1
)
// 网站自动抓取规则
type AutoCrawlRule struct {
Id int `json:"id" xorm:"pk autoincr"`
Website string `json:"website"`
AllUrl string `json:"all_url"`
IncrUrl string `json:"incr_url"`
Keywords string `json:"keywords"`
ListSelector string `json:"list_selector"`
ResultSelector string `json:"result_selector"`
PageField string `json:"page_field"`
MaxPage int `json:"max_page"`
Ext string `json:"ext"`
OpUser string `json:"op_user"`
Mtime string `json:"mtime" xorm:"<-"`
ExtMap map[string]string `json:"-" xorm:"-"`
}
func (this *AutoCrawlRule) AfterSet(name string, cell xorm.Cell) {
if name == "ext" {
if this.Ext == "" {
return
}
this.ExtMap = make(map[string]string)
err := json.Unmarshal([]byte(this.Ext), &this.ExtMap)
if err != nil {
logger.Errorln("parse auto crawl rule ext error:", err)
return
}
}
}