Skip to content

Commit

Permalink
Merge pull request #1 from o98k-ok/feat/order-by
Browse files Browse the repository at this point in the history
[feat]: order by view ts and support config session
  • Loading branch information
o98k-ok authored Dec 30, 2022
2 parents 86ad4ab + 3186235 commit 277e927
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
28 changes: 20 additions & 8 deletions doc/feishu.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
)

type Lark struct {
Offset int
Count int
URL string
Client *http.Client
Offset int
Count int
URL string
Client *http.Client
Session *string
}

func NewLark() *Lark {
Expand All @@ -33,6 +34,11 @@ func (l *Lark) WithPage(offset, count int) *Lark {
return l
}

func (l *Lark) CustomSession(session string) *Lark {
l.Session = &session
return l
}

type Artitle struct {
Title string `json:"title"`
Preview string `json:"preview"`
Expand All @@ -59,12 +65,18 @@ type ArtitleResp struct {

func (l Lark) Query(query string) ([]Entity, error) {
offset, count := strconv.FormatInt(int64(l.Offset), 10), strconv.FormatInt(int64(l.Count), 10)
sessions := cookie.NewKooky("feishu.cn", "session").Filter()
if len(sessions) == 0 {
return nil, errors.New("no fuch session")
var session cookie.CookieItem
if l.Session == nil || len(*l.Session) == 0 {
sessions := cookie.NewKooky("feishu.cn", "session").Filter()
if len(sessions) == 0 {
return nil, errors.New("no fuch session")
}
session = sessions[0]
} else {
session.Name = "session"
session.Value = *l.Session
}

session := sessions[0]
resp, err := np.NewRequest(l.Client, l.URL).
AddParam("query", query).AddParam("offset", offset).AddParam("count", count).
AddHeader("cookie", fmt.Sprintf("%s=%s;", session.Name, session.Value)).Do()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/o98k-ok/aggregation
go 1.18

require (
github.com/o98k-ok/lazy v0.2.17
github.com/o98k-ok/lazy v0.2.18
github.com/zellyn/kooky v0.0.0-20221025221128-3e66d684c4db
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ github.com/keybase/go-keychain v0.0.0-20220408132150-ad3b4a8fd4a7 h1:ttxQhWhqiYE
github.com/keybase/go-keychain v0.0.0-20220408132150-ad3b4a8fd4a7/go.mod h1:enrU/ug069Om7vWxuFE6nikLI2BZNwevMiGSo43Kt5w=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/o98k-ok/lazy v0.2.17 h1:U4+KVuY1Wk0jWT23dRIWMENq3+ZXTx537/S5MMBSX34=
github.com/o98k-ok/lazy v0.2.17/go.mod h1:bFqS0Mpk2K3JZvBkb3O8QSHXJ/U65nNmXrn1EJlVCtA=
github.com/o98k-ok/lazy v0.2.18 h1:G7FhhupyD21rsCf9rHUT94yKQrmPWI2umN6ZI9EWzBc=
github.com/o98k-ok/lazy v0.2.18/go.mod h1:bFqS0Mpk2K3JZvBkb3O8QSHXJ/U65nNmXrn1EJlVCtA=
github.com/o98k-ok/pcurl v0.0.0-20220411095241-e8aed39adb6d h1:kGyMItE5ShkTYvmgq1VaEIwckR+C3t7N6NGvqhqnNCk=
github.com/o98k-ok/pcurl v0.0.0-20220411095241-e8aed39adb6d/go.mod h1:/jSLI/84xMBQHFJZ4CKmEIfRaf3YXVdEH2ZXQ51yzq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,30 @@ func intro(entity doc.Entity) string {
func entry() {
cli := alfred.NewApp("lark doc search plugin")
cli.Bind("query", func(s []string) {
params := strings.Join(s, " ")
env, err := alfred.GetFlowEnv()
if err != nil {
alfred.ErrItems("alfred get envs failed", err).Show()
return
}

entities, err := doc.NewLark().Query(params)
cli := doc.NewLark().CustomSession(env.GetAsString("session", "")).WithPage(0, env.GetAsInt("count", 9))
params := strings.Join(s, " ")
entities, err := cli.Query(params)
if err != nil {
alfred.ErrItems("query lark failed", err)
return
}

msg := alfred.NewItems()
for _, entity := range entities {
msg.Append(alfred.NewItem(title(entity), intro(entity), entity.Url))
item := alfred.NewItem(title(entity), intro(entity), entity.Url)
item.Extra = entity.ViewTS
msg.Append(item)
}

msg.Order(func(l, r *alfred.Item) bool {
return l.Extra.(uint32) > r.Extra.(uint32)
})
msg.Show()
})

Expand Down

0 comments on commit 277e927

Please sign in to comment.