Skip to content

Commit

Permalink
新增 --cookies-from-browser 的說明內容
Browse files Browse the repository at this point in the history
在 --cookies-from-browser 加入可指定讀入 profile 的 cookie,仿 yt-dlp 格式製作
  • Loading branch information
saintliao committed Mar 20, 2023
1 parent d71ac30 commit 3481319
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
34 changes: 22 additions & 12 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ var (
basePath string
)

func getBrowserCookiePaths(browserName string) ([]string, error) {
func getBrowserCookiePaths(browserName, profileName string) ([]string, error) {
browserName = strings.ToLower(browserName)
if profileName == "" {
profileName = "Default"
}

var cookieFile string

Expand Down Expand Up @@ -90,7 +93,7 @@ func getBrowserCookiePaths(browserName string) ([]string, error) {
}

profileBasePath := filepath.Join(basePath, profileDir)
profiles, err := getProfiles(profileBasePath, browserName)
profiles, err := getProfiles(profileBasePath, browserName, profileName)
if err != nil {
return nil, err
}
Expand All @@ -116,7 +119,7 @@ func getBrowserCookiePaths(browserName string) ([]string, error) {
return cookiePaths, nil
}

func getProfiles(profileBasePath string, browserName string) ([]string, error) {
func getProfiles(profileBasePath, browserName, profileName string) ([]string, error) {
file, err := os.Open(profileBasePath)
if err != nil {
return nil, err
Expand All @@ -135,13 +138,12 @@ func getProfiles(profileBasePath string, browserName string) ([]string, error) {
profiles = append(profiles, entry.Name())
} else if browserName != "firefox" {
// if entry.Name() == "Default" || strings.HasPrefix(entry.Name(), "Profile ") {
if entry.Name() == "Default" {
if strings.Contains(entry.Name(), profileName) {
profiles = append(profiles, entry.Name())
}
}
}
}

return profiles, nil
}

Expand Down Expand Up @@ -304,29 +306,35 @@ func decryptChromiumValue(encryptedValue, key []byte) (string, error) {
return "", errors.New("unknown platform")
}

func (di *DownloadInfo) GetCookieFromBrowser(browser string) (*cookiejar.Jar, error) {
func (di *DownloadInfo) GetCookieFromBrowser(browser, profile string) (*cookiejar.Jar, int, error) {
count := 0

jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
if err != nil {
return nil, err
return nil, count, err
}

cookiePaths, err := getBrowserCookiePaths(browser)
cookiePaths, err := getBrowserCookiePaths(browser, profile)
if err != nil {
LogError("Error getting browser cookie paths: %s", err.Error())
return nil, err
return nil, count, err
}

if len(cookiePaths) == 0 {
LogError("No cookie paths found for %s", browser)
return nil, count, errors.New("no cookie paths found")
}

var chromiumKey []byte
if browser == "chrome" || browser == "edge" || browser == "brave" {
chromiumKey, err = getChromiumKey(browser)
if err != nil {
LogError("Error getting Chromium key: %v\n", err)
return nil, err
return nil, count, err
}
}

cookieMap := make(map[string][]*http.Cookie)
for _, cookiePath := range cookiePaths {
cookies, err := getCookies(cookiePath, browser, chromiumKey)
Expand All @@ -340,6 +348,7 @@ func (di *DownloadInfo) GetCookieFromBrowser(browser string) (*cookiejar.Jar, er
cookieMap[cookie.Domain] = make([]*http.Cookie, 0)
}
cookieMap[cookie.Domain] = append(cookieMap[cookie.Domain], cookie)
count++
}
}

Expand All @@ -354,5 +363,6 @@ func (di *DownloadInfo) GetCookieFromBrowser(browser string) (*cookiejar.Jar, er
}
}
}
return jar, nil

return jar, count, nil
}
21 changes: 19 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ Options:
the script to access members-only content if you are a member
for the given stream's user. Must be netscape cookie format.
--cookies-from-browser BROWSER[:PROFILE]
Attempt to automatically load cookies from your browser. Currently
supported browsers are: firefox, chrome, brave, edge. PROFILE is
optional and only applies to chromium core browser. If not provided,
the default profile will be used. For example, to load cookies from
Firefox's default profile, use '--cookie-from-browser firefox', And
wanna load cookies from Chrome's 'Profile 1', use
'--cookies-from-browser chrome:"Profile 1"'.
--debug
Print a lot of extra information.
Expand Down Expand Up @@ -583,14 +592,22 @@ func run() int {
}

if client.Jar == nil && len(cookieFromBrowser) > 0 {
cjar, err := info.GetCookieFromBrowser(cookieFromBrowser)
arr := strings.Split(cookieFromBrowser, ":")
profile := ""
if len(arr) == 2 {
cookieFromBrowser = arr[0]
profile = arr[1]
} else {
cookieFromBrowser = arr[0]
}
cjar, cookieSize, err := info.GetCookieFromBrowser(cookieFromBrowser, profile)
if err != nil {
LogError("Failed to load cookies from browser: %s", err)
return 1
}

client.Jar = cjar
LogGeneral("Loaded [%s] cookies.", cookieFromBrowser)
LogGeneral("The [%s] browser has (%d) cookies loaded.", cookieFromBrowser, cookieSize)
}

if !info.GVideoDDL && !info.GetVideoInfo() {
Expand Down

0 comments on commit 3481319

Please sign in to comment.