完成评论服务端化

This commit is contained in:
deepzz0
2016-10-23 00:22:04 +08:00
parent de79f8bd51
commit 3ce2b21588
7 changed files with 174 additions and 59 deletions

View File

@@ -28,8 +28,12 @@ superfeedr: deepzz
disqus:
shortname: deepzz
publickey: wdSgxRm9rdGAlLKFcFdToBe3GT4SibmV7Y8EjJQ0r4GWXeKtxpopMAeIeoI2dTEg
url: https://disqus.com/api/3.0/threads/set.json
postscount: https://disqus.com/api/3.0/threads/set.json
postslist: https://disqus.com/api/3.0/threads/listPosts.json
interval: 5
# 热搜词配置
hotwords:
- docker
# 运行模式
mode:
# you can fix certfile, keyfile, domain

2
db.go
View File

@@ -92,7 +92,7 @@ func init() {
// 启动定时器
go timer()
// 获取评论数量
go CommentsCount()
go PostsCount()
}
// 读取或初始化帐号信息

View File

@@ -22,18 +22,18 @@ type result struct {
}
}
func CommentsCount() {
if setting.Conf.Disqus.URL == "" || setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.ShortName == "" {
func PostsCount() {
if setting.Conf.Disqus.PostsCount == "" || setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.ShortName == "" {
return
}
baseUrl := setting.Conf.Disqus.URL + "?api_key=" + setting.Conf.Disqus.PublicKey + "&forum=" + setting.Conf.Disqus.ShortName + "&"
baseUrl := setting.Conf.Disqus.PostsCount + "?api_key=" + setting.Conf.Disqus.PublicKey + "&forum=" + setting.Conf.Disqus.ShortName + "&"
var count, index int
for index < len(Ei.Articles) {
logd.Debugf("count=====%d, index=======%d, length=======%d, bool=========%t", count, index, len(Ei.Articles), index < len(Ei.Articles) && count < 10)
logd.Debugf("count=====%d, index=======%d, length=======%d, bool=========%t", count, index, len(Ei.Articles), index < len(Ei.Articles) && count < 50)
var threads []string
for ; index < len(Ei.Articles) && count < 20; index++ {
for ; index < len(Ei.Articles) && count < 50; index++ {
artc := Ei.Articles[index]
threads = append(threads, fmt.Sprintf("thread=link:https://%s/post/%s.html", setting.Conf.Mode.Domain, artc.Slug))
threads = append(threads, fmt.Sprintf("thread:ident=post-%s", artc.Slug))
count++
}
count = 0
@@ -49,16 +49,16 @@ func CommentsCount() {
logd.Error(err)
break
}
if resp.StatusCode != http.StatusOK {
logd.Error(string(b))
break
}
rst := result{}
err = json.Unmarshal(b, &rst)
if err != nil {
logd.Error(err)
break
}
if rst.Code != SUCCESS {
logd.Error(rst.Code)
break
}
for _, v := range rst.Response {
i := strings.Index(v.Identifiers[0], "-")
artc := Ei.MapArticles[v.Identifiers[0][i+1:]]
@@ -67,5 +67,54 @@ func CommentsCount() {
}
}
}
time.AfterFunc(time.Duration(setting.Conf.Disqus.Interval)*time.Hour, CommentsCount)
time.AfterFunc(time.Duration(setting.Conf.Disqus.Interval)*time.Hour, PostsCount)
}
type postsList struct {
Cursor struct {
HasNext bool
Next string
}
Code int
Response []struct {
Parent int
Id string
CreatedAt string
Message string
Author struct {
Name string
ProfileUrl string
Avatar struct {
Cache string
}
}
}
}
func PostsList(slug, cursor string) *postsList {
if setting.Conf.Disqus.PostsList == "" || setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.ShortName == "" {
return nil
}
url := setting.Conf.Disqus.PostsList + "?limit=50&api_key=" + setting.Conf.Disqus.PublicKey + "&forum=" + setting.Conf.Disqus.ShortName + "&cursor=" + cursor + "&thread:ident=post-" + slug
resp, err := http.Get(url)
if err != nil {
return nil
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
logd.Error(err)
return nil
}
if resp.StatusCode != http.StatusOK {
logd.Error(string(b))
return nil
}
pl := &postsList{}
err = json.Unmarshal(b, pl)
if err != nil {
logd.Error(err)
return nil
}
return pl
}

View File

@@ -158,13 +158,12 @@ func HandleSearchPage(c *gin.Context) {
h["CurrentPage"] = "search-post"
q := c.Query("q")
start, err := strconv.Atoi(c.Query("start"))
if start < 1 || err != nil {
start = 1
}
if q != "" {
start, err := strconv.Atoi(c.Query("start"))
if start < 1 || err != nil {
start = 1
}
h["Word"] = q
h["HotWords"] = []string{"docker"}
var result *ESSearchResult
vals := c.Request.URL.Query()
reg := regexp.MustCompile(`^[a-z]+:\w+$`)
@@ -191,6 +190,8 @@ func HandleSearchPage(c *gin.Context) {
h["Next"] = vals.Encode()
}
}
} else {
h["HotWords"] = setting.Conf.HotWords
}
c.Status(http.StatusOK)
RenderHTMLFront(c, "search", h)
@@ -242,41 +243,58 @@ func HandleBeacon(c *gin.Context) {
}
// 服务端获取评论详细
type DisqusComments struct {
ErrNo int `json:"errno"`
ErrMsg string `json:"errmsg"`
Data struct {
Next string `json:"next"`
Total int `json:"total,omitempty"`
Comments []commentsDetail `json:"comments"`
} `json:"data"`
}
type commentsDetail struct {
Id string `json:"id"`
Parent int `json:"parent"`
Name string `json:"name"`
Url string `json:"url"`
Avatar string `json:"avatar"`
CreatedAt string `json:"createdAt"`
CreatedAtStr string `json:"createdAtStr"`
Message string `json:"message"`
}
func HandleDisqus(c *gin.Context) {
slug := c.Query("slug")
logd.Debug(slug)
// TODO comments
var ss = map[string]interface{}{
"errno": 0,
"errmsg": "",
"data": map[string]interface{}{
"next": "",
"total": 3,
"comments": []map[string]interface{}{
map[string]interface{}{
"id": "2361914870",
"name": "Rekey Luo",
"parent": 0,
"url": "https://disqus.com/by/rekeyluo/",
"avatar": "//a.disquscdn.com/uploads/users/15860/7550/avatar92.jpg?1438917750",
"createdAt": "2015-11-16T05:00:02",
"createdAtStr": "9 months ago",
"message": "你最近对 http2 ssl 相关关注好多啊。",
},
map[string]interface{}{
"id": "2361915528",
"name": "Jerry Qu",
"parent": 0,
"url": "https://disqus.com/by/JerryQu/",
"avatar": "//a.disquscdn.com/uploads/users/1668/8837/avatar92.jpg?1472281172",
"createdAt": "2015-11-16T05:01:05",
"createdAtStr": "9 months ago",
"message": "嗯,最近对 web 性能优化这一块研究得比较多。",
},
},
},
slug := c.Param("slug")
cursor := c.Query("cursor")
dcs := DisqusComments{}
postsList := PostsList(slug, cursor)
if postsList != nil {
dcs.ErrNo = postsList.Code
if postsList.Cursor.HasNext {
dcs.Data.Next = postsList.Cursor.Next
}
if cursor == "" {
dcs.Data.Total = Ei.MapArticles[slug].Count
}
dcs.Data.Comments = make([]commentsDetail, len(postsList.Response))
for i, v := range postsList.Response {
dcs.Data.Comments[i] = commentsDetail{
Id: v.Id,
Name: v.Author.Name,
Parent: v.Parent,
Url: v.Author.ProfileUrl,
Avatar: v.Author.Avatar.Cache,
CreatedAt: v.CreatedAt,
CreatedAtStr: ConvertStr(v.CreatedAt),
Message: v.Message,
}
}
} else {
dcs.ErrNo = FAIL
dcs.ErrMsg = "系统错误"
}
c.JSON(http.StatusOK, ss)
c.JSON(http.StatusOK, dcs)
}
func RenderHTMLFront(c *gin.Context, name string, data gin.H) {

View File

@@ -8,6 +8,9 @@ import (
"io/ioutil"
"path"
"regexp"
"time"
"github.com/eiblog/utils/logd"
)
const (
@@ -69,3 +72,42 @@ func PickFirstImage(html string) string {
}
return ""
}
// 2016-10-22T07:03:01
const (
JUST_NOW = "几秒前"
MINUTES_AGO = "%d分钟前"
HOURS_AGO = "%d小时前"
DAYS_AGO = "%d天前"
MONTH_AGO = "%d月前"
YEARS_AGO = "%d年前"
)
func ConvertStr(str string) string {
t, err := time.Parse("2006-01-02T15:04:05", str)
if err != nil {
logd.Error(err, str)
return JUST_NOW
}
now := time.Now()
year1, month1, day1 := t.Date()
year2, month2, day2 := now.Date()
if y := year2 - year1; y > 0 {
return fmt.Sprintf(YEARS_AGO, y)
}
if m := month2 - month1; m > 0 {
return fmt.Sprintf(MONTH_AGO, m)
}
if d := day2 - day1; d > 0 {
return fmt.Sprintf(DAYS_AGO, d)
}
hour1, minute1, _ := t.Clock()
hour2, minute2, _ := now.Clock()
if h := hour2 - hour1; h > 0 {
return fmt.Sprintf(HOURS_AGO, h)
}
if m := minute2 - minute1; m > 0 {
return fmt.Sprintf(MINUTES_AGO, m)
}
return JUST_NOW
}

View File

@@ -36,14 +36,16 @@ type Config struct {
SearchURL string // elasticsearch 地址
Superfeedr string // superfeedr
Disqus struct { // 获取文章数量相关
ShortName string
PublicKey string
URL string
Interval int
ShortName string
PublicKey string
PostsCount string
PostsList string
Interval int
}
Mode RunMode // 运行模式
Twitter string // twitter地址
Blogger struct { // 初始化数据
HotWords []string // 热搜词
Mode RunMode // 运行模式
Twitter string // twitter地址
Blogger struct { // 初始化数据
BlogName string
SubTitle string
BeiAn string

File diff suppressed because one or more lines are too long