Skip to content

Commit

Permalink
update: platform interface
Browse files Browse the repository at this point in the history
  • Loading branch information
k8scat committed Dec 31, 2022
1 parent beb09df commit ba52a4c
Show file tree
Hide file tree
Showing 34 changed files with 154 additions and 178 deletions.
14 changes: 5 additions & 9 deletions README.md
Expand Up @@ -23,14 +23,10 @@ https://k8scat.github.io/Articli

```go
type Platform interface {
// Name Platform name
Name() string
// Auth Authenticate with raw auth data, like cookie or user:pass
Auth(raw string) (username string, err error)
// Publish Post article
Publish(r io.Reader) (url string, err error)
// ParseMark Parse markdown meta data
ParseMark(mark *markdown.Mark) (params map[string]any, err error)
Name() string
Auth(raw string) (string, error)
NewArticle(r io.Reader) error
Publish() (string, error)
}
```

Expand All @@ -39,7 +35,7 @@ type Platform interface {
```go
// pkg/platform/hub.go
func init() {
register(new(another.Platform))
register(new(another.Platform))
}
```

Expand Down
35 changes: 14 additions & 21 deletions docs/README.md
Expand Up @@ -38,14 +38,14 @@ make
# 标题
title: "标题"
# 文章概要
brief_content: "文章概要"
brief_content: ""
# 封面图片地址
cover_images:
- https://img.alicdn.com/tfs/TB1.jpg
# 前缀内容
prefix_content: "文章前缀内容"
# 后缀内容
suffix_content: "文章后缀内容"
# 文章前缀内容
prefix_content: ""
# 文章后缀内容
suffix_content: ""

# 掘金平台文章配置
juejin:
Expand All @@ -56,7 +56,6 @@ juejin:
# 标签名称
tags:
- "Go"
- "Mac"
# 分类名称
category: "后端"
# 是否同步到组织,个人账号不支持
Expand All @@ -68,12 +67,10 @@ csdn:
article_id: ""
# 分类名称
categories:
- Golang
- 后端
- "Golang"
# 标签名称
tags:
- cli
- csdn
- "CLI"
# 发布形式,可选值:全部可见 public、仅我可见 private、VIP可见 read_need_vip、粉丝可见 read_need_fans,默认 public
read_type: public
# 发布状态,可选值:发布 publish、草稿 draft,默认 publish
Expand All @@ -94,7 +91,7 @@ oschina:
# 分类名称
category: "Golang"
# 推广专区名称
technical_field_id: "程序人生"
technical_field: "程序人生"
# 禁止评论
deny_comment: false
# 置顶
Expand All @@ -110,7 +107,7 @@ segmentfault:
article_id: ""
# 标签名称
tags:
- "kubernetes"
- "go"
# 注明版权
license: false
# 文章类型,可选值:原创 1、转载 2、翻译 3,默认 1
Expand Down Expand Up @@ -179,14 +176,10 @@ acli pub -p <platform> --file <article-file>

```go
type Platform interface {
// Name Platform name
Name() string
// Auth Authenticate with raw auth data, like cookie or user:pass
Auth(raw string) (username string, err error)
// Publish Post article
Publish(r io.Reader) (url string, err error)
// ParseMark Parse markdown meta data
ParseMark(mark *markdown.Mark) (params map[string]any, err error)
Name() string
Auth(raw string) (string, error)
NewArticle(r io.Reader) error
Publish() (string, error)
}
```

Expand All @@ -195,6 +188,6 @@ type Platform interface {
```go
// pkg/platform/hub.go
func init() {
register(new(another.Platform))
register(new(another.Platform))
}
```
5 changes: 4 additions & 1 deletion internal/cmd/platform/publish.go
Expand Up @@ -30,8 +30,11 @@ var (
if err != nil {
return err
}
if err = pf.NewArticle(f); err != nil {
return err
}

url, err := pf.Publish(f)
url, err := pf.Publish()
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion internal/config/config.go
Expand Up @@ -64,7 +64,6 @@ func save(f string, cfg *Config) error {
return err
}

// GetConfigDir Get dir which store the config file
func GetConfigDir() string {
homeDir, err := homedir.Dir()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/csdn/api_gateway.go
Expand Up @@ -9,7 +9,7 @@ var (
UserGateway *sign.APIGateway
)

func InitResourceGateway() error {
func initResourceGateway() error {
gateway, err := sign.NewAPIGateway(ResourceAppKey, ResourceAppSecret)
if err != nil {
return err
Expand All @@ -18,7 +18,7 @@ func InitResourceGateway() error {
return nil
}

func InitUserGateway() error {
func initUserGateway() error {
gateway, err := sign.NewAPIGateway(UserAppKey, UserAppSecret)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions pkg/csdn/article.go
Expand Up @@ -21,21 +21,21 @@ type SaveArticleResponse struct {
BaseResponse
}

func (c *Client) SaveArticle(params map[string]any) (string, error) {
rawurl := BuildBizAPIURL("/blog-console-api/v3/mdeditor/saveArticle")
b, err := json.Marshal(params)
func (c *Client) saveArticle() (string, error) {
rawurl := buildBizAPIURL("/blog-console-api/v3/mdeditor/saveArticle")
b, err := json.Marshal(c.params)
if err != nil {
return "", err
}

if ResourceGateway == nil {
if err = InitResourceGateway(); err != nil {
if err = initResourceGateway(); err != nil {
return "", err
}
}

body := bytes.NewReader(b)
resp, err := c.Post(rawurl, nil, body, ResourceGateway)
resp, err := c.post(rawurl, nil, body, ResourceGateway)
if err != nil {
return "", err
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/csdn/client.go
Expand Up @@ -33,6 +33,7 @@ const (

type Client struct {
cookie string
params map[string]any
}

func (c *Client) Name() string {
Expand All @@ -42,23 +43,24 @@ func (c *Client) Name() string {
func (c *Client) Auth(cookie string) (string, error) {
c.cookie = cookie
var err error
info, err := c.GetAuthInfo()
info, err := c.getAuthInfo()
if err != nil {
return "", err
}
return info.Basic.Nickname, nil
}

func (c *Client) Publish(r io.Reader) (string, error) {
func (c *Client) NewArticle(r io.Reader) error {
mark, err := markdown.Parse(r)
if err != nil {
return "", err
}
params, err := c.ParseMark(mark)
if err != nil {
return "", err
return err
}
url, err := c.SaveArticle(params)
c.params, err = c.parseMark(mark)
return err
}

func (c *Client) Publish() (string, error) {
url, err := c.saveArticle()
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/csdn/markdown.go
Expand Up @@ -40,8 +40,7 @@ const (
ArticleLevel3 = 3
)

// ParseMark parse mark to article params
func (c *Client) ParseMark(mark *markdown.Mark) (params map[string]any, err error) {
func (c *Client) parseMark(mark *markdown.Mark) (params map[string]any, err error) {
v := mark.Meta.Get(c.Name())
if v == nil {
err = fmt.Errorf("meta not found for %s", c.Name())
Expand Down
12 changes: 6 additions & 6 deletions pkg/csdn/request.go
Expand Up @@ -11,7 +11,7 @@ import (
sign "github.com/k8scat/aliyun-api-gateway-sign-golang"
)

func (c *Client) Request(req *http.Request, apiGateway ...*sign.APIGateway) (*http.Response, error) {
func (c *Client) request(req *http.Request, apiGateway ...*sign.APIGateway) (*http.Response, error) {
req.Header.Set("Cookie", c.cookie)
req.Header.Set("User-Agent", browser.Computer())

Expand All @@ -25,31 +25,31 @@ func (c *Client) Request(req *http.Request, apiGateway ...*sign.APIGateway) (*ht
return resp, err
}

func (c *Client) Get(rawurl string, query url.Values, apiGateway ...*sign.APIGateway) (*http.Response, error) {
func (c *Client) get(rawurl string, query url.Values, apiGateway ...*sign.APIGateway) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, rawurl, nil)
if err != nil {
return nil, err
}
if query != nil {
req.URL.RawQuery = query.Encode()
}
resp, err := c.Request(req, apiGateway...)
resp, err := c.request(req, apiGateway...)
return resp, err
}

func (c *Client) Post(rawurl string, query url.Values, body io.Reader, apiGateway ...*sign.APIGateway) (*http.Response, error) {
func (c *Client) post(rawurl string, query url.Values, body io.Reader, apiGateway ...*sign.APIGateway) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, rawurl, body)
if err != nil {
return nil, err
}
if query != nil {
req.URL.RawQuery = query.Encode()
}
resp, err := c.Request(req, apiGateway...)
resp, err := c.request(req, apiGateway...)
return resp, err
}

func BuildBizAPIURL(path string) string {
func buildBizAPIURL(path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/csdn/user.go
Expand Up @@ -70,21 +70,21 @@ type AuthInfo struct {
} `json:"general"`
}

func (c *Client) GetAuthInfo() (info *AuthInfo, err error) {
rawurl := BuildBizAPIURL("/community-personal/v1/get-personal-info")
func (c *Client) getAuthInfo() (info *AuthInfo, err error) {
rawurl := buildBizAPIURL("/community-personal/v1/get-personal-info")
var resp *http.Response
apiGateway := &sign.APIGateway{
Key: UserAppKey,
Secret: UserAppSecret,
}

if UserGateway == nil {
if err = InitUserGateway(); err != nil {
if err = initUserGateway(); err != nil {
return nil, err
}
}

resp, err = c.Get(rawurl, nil, apiGateway, UserGateway)
resp, err = c.get(rawurl, nil, apiGateway, UserGateway)
if err != nil {
return
}
Expand Down
25 changes: 11 additions & 14 deletions pkg/juejin/article.go
Expand Up @@ -7,40 +7,38 @@ import (
"github.com/tidwall/gjson"
)

// SaveArticle create an article if id is empty, otherwise update the article
func (c *Client) SaveArticle(params map[string]any) (string, error) {
articleID, _ := params["article_id"].(string)
draftID, _ := params["draft_id"].(string)
func (c *Client) saveArticle() (string, error) {
articleID, _ := c.params["article_id"].(string)
draftID, _ := c.params["draft_id"].(string)
if articleID != "" && draftID == "" {
article, err := c.GetArticle(articleID)
article, err := c.getArticle(articleID)
if err != nil {
return "", err
}
draftID = article.Info.DraftID
}

draftID, err := c.SaveDraft(params)
draftID, err := c.saveDraft()
if err != nil {
return "", err
}
fmt.Printf("draft_id: %s\n", draftID)

syncToOrg, _ := params["sync_to_org"].(bool)
articleID, err = c.PublishArticle(draftID, syncToOrg)
syncToOrg, _ := c.params["sync_to_org"].(bool)
articleID, err = c.publishArticle(draftID, syncToOrg)
if err != nil {
return "", err
}
fmt.Printf("article_id: %s\n", articleID)
return BuildArticleURL(articleID), nil
}

// GetArticle get article detail
func (c *Client) GetArticle(id string) (*Article, error) {
func (c *Client) getArticle(id string) (*Article, error) {
endpoint := buildArticleEndpoint("detail")
payload := map[string]interface{}{
"article_id": id,
}
raw, err := c.Post(endpoint, payload)
raw, err := c.post(endpoint, payload)
if err != nil {
return nil, err
}
Expand All @@ -50,14 +48,13 @@ func (c *Client) GetArticle(id string) (*Article, error) {
return article, err
}

// PublishArticle publish a draft
func (c *Client) PublishArticle(draftID string, syncToOrg bool) (string, error) {
func (c *Client) publishArticle(draftID string, syncToOrg bool) (string, error) {
endpoint := buildArticleEndpoint("publish")
payload := map[string]any{
"draft_id": draftID,
"sync_to_org": syncToOrg,
}
raw, err := c.Post(endpoint, payload)
raw, err := c.post(endpoint, payload)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/juejin/category.go
Expand Up @@ -17,7 +17,7 @@ type CategoryItem struct {
// ListCategories list all categories
func (c *Client) listCategories() ([]*CategoryItem, error) {
endpoint := "/tag_api/v1/query_category_list"
raw, err := c.Post(endpoint, nil)
raw, err := c.post(endpoint, nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ba52a4c

Please sign in to comment.