From: qydysky Date: Sun, 29 Dec 2024 10:52:52 +0000 (+0800) Subject: Add cookie路径 (#136) X-Git-Tag: v0.14.27~5 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=d154567d8792fa05ca6a13103e03280e793b75aa;p=bili_danmu%2F.git Add cookie路径 (#136) --- diff --git a/F/CookieCrypo.go b/F/CookieCrypo.go index 1af41db..e82b658 100644 --- a/F/CookieCrypo.go +++ b/F/CookieCrypo.go @@ -19,7 +19,7 @@ var ( cookieLock sync.RWMutex ) -func CookieGet() []byte { +func CookieGet(path string) []byte { clog := clog.Base_add(`获取`) cookieLock.RLock() @@ -48,7 +48,7 @@ func CookieGet() []byte { pri = d } } else { - if d, e := FileLoad(`cookie.txt`); e != nil { + if d, e := FileLoad(path); e != nil { clog.L(`E: `, e, `cookie保存格式`) return []byte{} } else if string(d[:6]) == `t=nol;` { @@ -61,7 +61,7 @@ func CookieGet() []byte { } } } - if d, e := FileLoad(`cookie.txt`); e != nil { + if d, e := FileLoad(path); e != nil { clog.L(`E: `, e, `cookie保存格式`) return []byte{} } else if string(d[:6]) == `t=pem;` { @@ -84,7 +84,7 @@ func CookieGet() []byte { } } -func CookieSet(source []byte) { +func CookieSet(path string, source []byte) { clog := clog.Base_add(`设置`) cookieLock.Lock() @@ -99,7 +99,7 @@ func CookieSet(source []byte) { pub = d } } else { - f := file.New(`cookie.txt`, 0, true) + f := file.New(path, 0, true) _ = f.Delete() _, _ = f.Write(append([]byte("t=nol;"), source...), true) return @@ -109,7 +109,7 @@ func CookieSet(source []byte) { clog.L(`E: `, e) return } else { - f := file.New(`cookie.txt`, 0, true) + f := file.New(path, 0, true) _ = f.Delete() _, _ = f.Write(append([]byte("t=pem;"), source...), true) } diff --git a/F/api.go b/F/api.go index 9ca695a..9485a30 100644 --- a/F/api.go +++ b/F/api.go @@ -921,8 +921,13 @@ func (t *GetFunc) Get_cookie() (missKey []string) { } }() - if file.New("cookie.txt", 0, true).IsExist() { //读取cookie文件 - if cookieString := string(CookieGet()); cookieString != `` { + savepath := "./cookie.txt" + if tmp, ok := t.K_v.LoadV("cookie路径").(string); ok && tmp != "" { + savepath = tmp + } + + if file.New(savepath, 0, true).IsExist() { //读取cookie文件 + if cookieString := string(CookieGet(savepath)); cookieString != `` { for k, v := range reqf.Cookies_String_2_Map(cookieString) { //cookie存入全局变量syncmap t.Cookie.Store(k, v) } @@ -1396,16 +1401,13 @@ func (t *GetFunc) Silver_2_coin() (missKey []string) { var ErrNoCookiesSave = errors.New("ErrNoCookiesSave") -func save_cookie(Cookies []*http.Cookie, cs ...*c.Common) error { +func save_cookie(Cookies []*http.Cookie, cs *c.Common) error { if len(Cookies) == 0 { return ErrNoCookiesSave } for k, v := range reqf.Cookies_List_2_Map(Cookies) { c.C.Cookie.Store(k, v) - for i := 0; i < len(cs); i++ { - cs[i].Cookie.Store(k, v) - } } Cookie := make(map[string]string) @@ -1413,7 +1415,12 @@ func save_cookie(Cookies []*http.Cookie, cs ...*c.Common) error { Cookie[k.(string)] = v.(string) return true }) - CookieSet([]byte(reqf.Map_2_Cookies_String(Cookie))) + + savepath := "./cookie.txt" + if tmp, ok := cs.K_v.LoadV("cookie路径").(string); ok && tmp != "" { + savepath = tmp + } + CookieSet(savepath, []byte(reqf.Map_2_Cookies_String(Cookie))) biliApi.SetCookies(Cookies) return nil } diff --git a/README.md b/README.md index 2352bf5..92d62f1 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,9 @@ ### 说明 本项目使用github action自动构建,构建过程详见[yml](https://github.com/qydysky/bili_danmu/blob/master/.github/workflows/go.yml) +#### cookie自定义位置 +配置文件添加配置项`cookie路径`(>v0.14.26),默认为`./cookie.txt` + #### 获取视频切片 当请求`http://{Web服务地址}{直播Web服务路径}stream?ref={录播文件夹名}&st={起始时间}&dur={片段时长}`时,将返回从录播文件的切片视频(>v0.14.21) diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index ebc0bc2..3886c90 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -190,6 +190,8 @@ "" ], "自动弹幕机_发送间隔s": 30, + "cookie路径-help": "为空时,为程序所在目录./cookie.txt", + "cookie路径": "./cookie.txt", "cookie保护": "下填路径", "cookie加密公钥": "public.pem", "cookie解密私钥": "private.pem",