完善 disqus 评论

This commit is contained in:
deepzz0
2017-06-26 23:51:13 +08:00
parent 66811830b0
commit 2825bbfeae
6 changed files with 127 additions and 54 deletions

132
disqus.go
View File

@@ -4,16 +4,19 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/deepzz0/logd"
"github.com/eiblog/eiblog/setting"
"github.com/eiblog/utils/logd"
)
var ErrDisqusConfig = errors.New("disqus config incorrect")
type result struct {
Code int
Response []struct {
@@ -23,12 +26,21 @@ type result struct {
}
}
func PostsCount() {
// 定时获取所有文章评论数量
func PostsCount() error {
if setting.Conf.Disqus.PostsCount == "" ||
setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" {
return
return ErrDisqusConfig
}
time.AfterFunc(time.Duration(setting.Conf.Disqus.Interval)*time.Hour, func() {
err := PostsCount()
if err != nil {
logd.Error(err)
}
})
baseUrl := setting.Conf.Disqus.PostsCount +
"?api_key=" + setting.Conf.Disqus.PublicKey +
"&forum=" + setting.Conf.Disqus.ShortName + "&"
@@ -44,24 +56,22 @@ func PostsCount() {
url := baseUrl + strings.Join(threads, "&")
resp, err := http.Get(url)
if err != nil {
logd.Error(err)
break
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
logd.Error(err)
break
return err
}
if resp.StatusCode != http.StatusOK {
logd.Error(string(b))
break
return errors.New(string(b))
}
rst := result{}
err = json.Unmarshal(b, &rst)
if err != nil {
logd.Error(err)
break
return err
}
for _, v := range rst.Response {
i := strings.Index(v.Identifiers[0], "-")
@@ -72,7 +82,8 @@ func PostsCount() {
}
}
}
time.AfterFunc(time.Duration(setting.Conf.Disqus.Interval)*time.Hour, PostsCount)
return nil
}
type postsList struct {
@@ -97,35 +108,34 @@ type postsList struct {
}
}
func PostsList(slug, cursor string) *postsList {
if setting.Conf.Disqus.PostsList == "" || setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.ShortName == "" {
return nil
// 获取文章评论列表
func PostsList(slug, cursor string) (*postsList, error) {
if setting.Conf.Disqus.PostsList == "" ||
setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" {
return nil, ErrDisqusConfig
}
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 {
logd.Error(err)
return nil
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
logd.Error(err)
return nil
return nil, err
}
if resp.StatusCode != http.StatusOK {
logd.Error(string(b))
return nil
return nil, errors.New(string(b))
}
pl := &postsList{}
err = json.Unmarshal(b, pl)
if err != nil {
logd.Error(err)
return nil
return nil, err
}
return pl
return pl, nil
}
type PostCreate struct {
@@ -146,9 +156,12 @@ type PostResponse struct {
} `json:"response"`
}
func PostComment(pc *PostCreate) string {
if setting.Conf.Disqus.PostsList == "" || setting.Conf.Disqus.PublicKey == "" || setting.Conf.Disqus.ShortName == "" {
return ""
// 评论文章
func PostComment(pc *PostCreate) (string, error) {
if setting.Conf.Disqus.PostsList == "" ||
setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" {
return "", ErrDisqusConfig
}
url := setting.Conf.Disqus.PostCreate +
"?api_key=E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F" +
@@ -158,31 +171,72 @@ func PostComment(pc *PostCreate) string {
request, err := http.NewRequest("POST", url, nil)
if err != nil {
logd.Error(err)
return ""
return "", err
}
request.Header.Set("Referer", "https://disqus.com")
resp, err := http.DefaultClient.Do(request)
if err != nil {
logd.Error(err)
return ""
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
logd.Error(err)
return ""
return "", err
}
if resp.StatusCode != http.StatusOK {
logd.Error(string(b))
return ""
return "", errors.New(string(b))
}
pr := &PostResponse{}
err = json.Unmarshal(b, pr)
if err != nil {
logd.Error(err)
return ""
return "", err
}
logd.Print(pr.Response.Id)
return pr.Response.Id
return pr.Response.Id, nil
}
type ApprovedResponse struct {
Code int `json:"code"`
Response []struct {
Id string `json:"id"`
} `json:"response"`
}
// 批准评论通过
func PostApprove(post string) error {
if setting.Conf.Disqus.PostsList == "" ||
setting.Conf.Disqus.PublicKey == "" ||
setting.Conf.Disqus.ShortName == "" {
return ErrDisqusConfig
}
url := setting.Conf.Disqus.PostApprove +
"?api_key=" + setting.Conf.Disqus.PublicKey +
"&access_token=" + setting.Conf.Disqus.AccessToken +
"&post=" + post
request, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
request.Header.Set("Referer", "https://disqus.com")
resp, err := http.DefaultClient.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New(string(b))
}
ar := &ApprovedResponse{}
err = json.Unmarshal(b, ar)
if err != nil {
return err
}
return nil
}