]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add cookie路径 (#136)
authorqydysky <qydysky@foxmail.com>
Sun, 29 Dec 2024 10:52:52 +0000 (18:52 +0800)
committerGitHub <noreply@github.com>
Sun, 29 Dec 2024 10:52:52 +0000 (18:52 +0800)
F/CookieCrypo.go
F/api.go
README.md
demo/config/config_K_v.json

index 1af41db6c31486bca31d092a0ebc5deac9adcc39..e82b658d99bed8049d90478b772f26f53126b3a5 100644 (file)
@@ -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)
        }
index 9ca695a0908fc2c2a533784f2a46ac54d514f763..9485a3077f439d05255dc77db559e7989880e957 100644 (file)
--- 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
 }
index 2352bf5fa63d1e9cea67112401b07c715b3e143f..92d62f1df213c54527f7ecc91f2f035f9d783656 100644 (file)
--- 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)
 
index ebc0bc26d3f6c0463fccddead6e5f3008309c832..3886c908b11bf89b4e9e13c4c63c3d4c2ab9dca4 100644 (file)
         ""
     ],
     "自动弹幕机_发送间隔s": 30,
+    "cookie路径-help": "为空时,为程序所在目录./cookie.txt",
+    "cookie路径": "./cookie.txt",
     "cookie保护": "下填路径",
     "cookie加密公钥": "public.pem",
     "cookie解密私钥": "private.pem",