Skip to content

Commit

Permalink
Merge pull request #9 from wuqinqiang/feat/proxy
Browse files Browse the repository at this point in the history
add  flag proxy-URL for proxy
  • Loading branch information
wuqinqiang authored Mar 5, 2023
2 parents 1c165d7 + 840b954 commit 8bfc534
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ notify: # 通知配置,目前支持telegram,dingtalk,lark可以全配,
webhook: "xx"
```
#### 代理
由于众所周知的原因,所以你可能需要代理,
环境变量:
```dotenv
# socks5或者http
# etc.
PROXY_URI=socks5://ip:port
```

### 单词短语推送器

指定单词数量,随机选择单词,生成一段小短文,推送到用户指定平台。
Expand All @@ -48,6 +59,7 @@ notify: # 通知配置,目前支持telegram,dingtalk,lark可以全配,

**参数说明**
这个程序有以下可选项:

- files:默认导入 CET4.txt 单词文件,你可以通过逗号同时导入多个单词文件,它们都存储在 library 文件夹下。
- spec:表示推送频率设置,默认为每小时生成一个新的短语,具体时间规则使用的是 [robif/cron](https://github.com/robfig/cron)
库,请参考该库的文档自行设置。
Expand Down
21 changes: 15 additions & 6 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"os"

"github.com/wuqinqiang/helloword/collector/file"

"github.com/wuqinqiang/helloword/collector/bbdc"
Expand Down Expand Up @@ -44,17 +42,29 @@ var DaemonCmd = &cli.Command{
Name: "conf",
Aliases: []string{"c"},
},
&cli.StringFlag{
Name: "bbdc-cookie",
EnvVars: []string{"BBDC_COOKIE"},
},
&cli.StringFlag{
Name: "proxy-url",
EnvVars: []string{"PROXY_URI"},
},
},

Action: func(cctx *cli.Context) error {
settings, err := conf.GetConf(cctx.String("conf"))
if err != nil {
return err
}
generator := gpt3.NewClient(settings.GptToken)
generator, err := gpt3.NewClient(settings.GptToken, cctx.String("proxy-url"))
if err != nil {
return err
}

var collectors []collector.Collector
bbcdCookie := os.Getenv("BBDC_COOKIE")

bbcdCookie := cctx.String("bbdc-cookie")
if bbcdCookie != "" {
collectors = append(collectors, bbdc.New(bbcdCookie))
}
Expand All @@ -64,15 +74,14 @@ var DaemonCmd = &cli.Command{
files = cctx.String("files")
}
collectors = append(collectors, file.New(files))

importer := collector.NewImporter(collectors...)

strategy := selector.Random
if cctx.String("strategy") == string(selector.LeastRecentlyUsed) {
strategy = selector.LeastRecentlyUsed
}

s := selector.New(strategy, selector.WithWordNumber(cctx.Int("word-number")))

n := notify.New(settings.Senders())
core := core.New(generator, importer, s, n, core.WithSpec(cctx.String("spec")))

Expand Down
9 changes: 8 additions & 1 deletion cmd/phrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var PhraseCmd = &cli.Command{
Name: "conf",
Aliases: []string{"c"},
},
&cli.StringFlag{
Name: "proxy-url",
EnvVars: []string{"PROXY_URL"},
},
},
Action: func(cctx *cli.Context) error {
req := cctx.Args().Get(0)
Expand All @@ -33,7 +37,10 @@ var PhraseCmd = &cli.Command{
if err != nil {
return err
}
client := gpt3.NewClient(conf.GptToken)
client, err := gpt3.NewClient(conf.GptToken, cctx.String("proxy-url"))
if err != nil {
return err
}
phrase, err := client.Generate(cctx.Context, strings.Split(req, ","))
if err != nil {
return err
Expand Down
28 changes: 25 additions & 3 deletions generator/gpt3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package gpt3
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"

gogpt "github.com/sashabaranov/go-gpt3"
)
Expand All @@ -14,9 +18,27 @@ type Client struct {
*gogpt.Client
}

func NewClient(token string) *Client {
gpt := gogpt.NewClient(token)
return &Client{gpt}
func NewClient(token string, proxyUrl string) (*Client, error) {
conf := gogpt.DefaultConfig(token)

if proxyUrl != "" {
proxy, err := url.Parse(proxyUrl)
if err != nil {
return nil, err
}
dialer := net.Dialer{
Timeout: 30 * time.Second,
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxy),
DialContext: dialer.DialContext,
MaxIdleConns: 50,
IdleConnTimeout: 60 * time.Second,
}
conf.HTTPClient.Transport = transport
}
gpt := gogpt.NewClientWithConfig(conf)
return &Client{gpt}, nil
}

func (client *Client) Generate(ctx context.Context, words []string) (phrase string, err error) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.6.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/tools v0.6.0 // indirect
gorm.io/datatypes v1.1.0 // indirect
gorm.io/driver/mysql v1.4.6 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,19 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
Expand Down

0 comments on commit 8bfc534

Please sign in to comment.