-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.go
72 lines (63 loc) · 1.86 KB
/
parsers.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
package cdlogic
import (
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/KJHJason/Cultured-Downloader-Logic/api"
"github.com/KJHJason/Cultured-Downloader-Logic/cdlerrors"
"github.com/KJHJason/Cultured-Downloader-Logic/httpfuncs"
"github.com/KJHJason/Cultured-Downloader-Logic/parsers"
)
// parse the Netscape cookie file generated by extensions like Get cookies.txt LOCALLY
func ParseNetscapeCookieFile(filePath, website, userAgent string, captchaHandler httpfuncs.CaptchaHandler) ([]*http.Cookie, error) {
if filePath == "" {
return nil, fmt.Errorf(
"error %d: file path cannot be empty for website %q",
cdlerrors.INPUT_ERROR,
website,
)
}
sessionCookieInfo := parsers.GetSessionCookieInfo(website)
sessionCookieName := sessionCookieInfo.Name
sessionCookieSameSite := sessionCookieInfo.SameSite
f, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf(
"error %d: opening cookie file at %s, more info => %w",
cdlerrors.OS_ERROR,
filePath,
err,
)
}
defer f.Close()
var cookies []*http.Cookie
cookieArgs := parsers.NewCookieInfoArgs(sessionCookieName, sessionCookieSameSite)
if ext := filepath.Ext(filePath); ext == ".txt" {
cookies, err = parsers.ParseTxtCookieFile(f, filePath, cookieArgs)
} else if ext == ".json" {
cookies, err = parsers.ParseJsonCookieFile(f, filePath, cookieArgs)
} else {
err = fmt.Errorf(
"error %d: invalid cookie file extension, %q, at %s...\nOnly .txt and .json files are supported",
cdlerrors.INPUT_ERROR,
ext,
filePath,
)
}
if err != nil {
return nil, err
}
if len(cookies) == 0 {
return nil, fmt.Errorf(
"error %d: no session cookie found in cookie file at %s for website %q",
cdlerrors.INPUT_ERROR,
filePath,
website,
)
}
if err := api.VerifyCookies(website, userAgent, cookies, captchaHandler); err != nil {
return nil, err
}
return cookies, nil
}