-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (96 loc) · 2.47 KB
/
main.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
package main
import (
"errors"
"fmt"
"github.com/nukosuke/go-annict/annict"
"github.com/nukosuke/go-alfred"
"github.com/spf13/viper"
"github.com/urfave/cli"
"golang.org/x/oauth2"
"os"
)
const (
bundleID = "tech.shibuya.alfred-annict-workflow"
version = "0.0.0"
)
func newAnnictClient() (*annict.Client, error) {
var client *annict.Client
viper.SetConfigName("config")
viper.AddConfigPath(os.Getenv("HOME") + "/.annict")
viper.SetConfigType("json")
err := viper.ReadInConfig()
if err != nil {
return nil, err
}
access_token := viper.GetString("access_token")
if access_token == "" {
client = annict.NewClient(nil)
return client, errors.New("access_token was not found")
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: access_token})
tc := oauth2.NewClient(oauth2.NoContext, ts)
client = annict.NewClient(tc)
return client, nil
}
func main() {
app := cli.NewApp()
app.Name = "alfred-annict-workflow"
app.Version = version
app.Commands = []cli.Command{
{
Name: "works",
Action: func(c *cli.Context) error {
query := c.Args()
client, err := newAnnictClient()
if err != nil {
return err
}
res, _, err := client.Works.List(&annict.WorksListOptions{
Fields: []string{"id", "title", "media_text", "season_name_text"},
FilterTitle: query[0],
})
if err != nil {
return err
}
list := alfred.NewResponse()
for _, item := range res.Works {
list.AddItem(&alfred.AlfredResponseItem{
Valid: true,
Uid: fmt.Sprintf("%d", item.Id),
Title: item.Title,
Arg: fmt.Sprintf("https://annict.com/works/%d", item.Id),
Subtitle: item.MediaText + " " + item.SeasonNameText,
})
}
list.Print()
return nil
},
},
{
Name: "watching",
Action: func(c *cli.Context) error {
client, err := newAnnictClient()
res, _, err := client.Me.Works.List(&annict.MeWorksListOptions{
Fields: []string{"id", "title", "media_text", "season_name_text"},
FilterStatus: "watching",
})
if err != nil {
return err
}
list := alfred.NewResponse()
for _, item := range res.Works {
list.AddItem(&alfred.AlfredResponseItem{
Valid: true,
Uid: fmt.Sprintf("%d", item.Id),
Title: item.Title,
Arg: fmt.Sprintf("https://annict.com/works/%d", item.Id),
Subtitle: item.MediaText + " " + item.SeasonNameText,
})
}
list.Print()
return nil
},
},
}
app.Run(os.Args)
}