Skip to content

Commit 173424c

Browse files
committed
Implement passing cookie name (Fixes #4)
1 parent d1c593f commit 173424c

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ Available Commands:
2222
version Print the version number of sbstck-dl
2323

2424
Flags:
25-
--after string Download posts published after this date (format: YYYY-MM-DD)
26-
--before string Download posts published before this date (format: YYYY-MM-DD)
27-
-h, --help help for sbstck-dl
28-
-x, --proxy string Specify the proxy url
29-
-i, --sid string Specify the substack.sid required to download private newsletters (found in the Substack's cookie)
30-
-r, --rate int Specify the rate of requests per second (default 2)
31-
-v, --verbose Enable verbose output
25+
--after string Download posts published after this date (format: YYYY-MM-DD)
26+
--before string Download posts published before this date (format: YYYY-MM-DD)
27+
--cookie_name cookieName Either substack.sid or connect.sid, based on your cookie (required for private newsletters)
28+
--cookie_val string The substack.sid/connect.sid cookie value (required for private newsletters)
29+
-h, --help help for sbstck-dl
30+
-x, --proxy string Specify the proxy url
31+
-r, --rate int Specify the rate of requests per second (default 2)
32+
-v, --verbose Enable verbose output
3233

3334
Use "sbstck-dl [command] --help" for more information about a command.
3435
```
@@ -74,9 +75,22 @@ Global Flags:
7475
-v, --verbose Enable verbose output
7576
```
7677
78+
### Private Newsletters
79+
80+
In order to download the full text of private newsletters you need to provide the cookie name and value of your session.
81+
The cookie name is either `substack.sid` or `connect.sid`, based on your cookie.
82+
To get the cookie value you can use the developer tools of your browser.
83+
Once you have the cookie name and value, you can pass them to the downloader using the `--cookie_name` and `--cookie_val` flags.
84+
85+
#### Example
86+
87+
```bash
88+
sbstck-dl download --url https://example.substack.com --cookie_name substack.sid --cookie_val COOKIE_VALUE
89+
```
90+
7791
## Thanks
7892
79-
- [wemoveon2](https://github.com/wemoveon2) for the discussion and help implementing the support for private newsletters
93+
- [wemoveon2](https://github.com/wemoveon2) and [lenzj](https://github.com/lenzj) for the discussion and help implementing the support for private newsletters
8094
8195
## TODO
8296

cmd/root.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"errors"
56
"log"
67
"net/http"
78
"net/url"
@@ -13,13 +14,39 @@ import (
1314

1415
// rootCmd represents the base command when called without any subcommands
1516

17+
type cookieName string
18+
19+
const (
20+
substackSid cookieName = "substack.sid"
21+
connectSid cookieName = "connect.sid"
22+
)
23+
24+
func (c *cookieName) String() string {
25+
return string(*c)
26+
}
27+
28+
func (c *cookieName) Set(val string) error {
29+
switch val {
30+
case "substack.sid", "connect.sid":
31+
*c = cookieName(val)
32+
default:
33+
return errors.New("invalid cookie name: must be either substack.sid or connect.sid")
34+
}
35+
return nil
36+
}
37+
38+
func (c *cookieName) Type() string {
39+
return "cookieName"
40+
}
41+
1642
var (
1743
proxyURL string
1844
verbose bool
1945
ratePerSecond int
2046
beforeDate string
2147
afterDate string
22-
substackID string
48+
IdCookieName cookieName
49+
IdCookieVal string
2350
ctx = context.Background()
2451
parsedProxyURL *url.URL
2552
fetcher *lib.Fetcher
@@ -46,10 +73,20 @@ func Execute() {
4673
if ratePerSecond == 0 {
4774
log.Fatal("rate must be greater than 0")
4875
}
49-
if substackID != "" {
50-
cookie = &http.Cookie{
51-
Name: "substack.sid",
52-
Value: substackID,
76+
if IdCookieVal != "" && IdCookieName == "" {
77+
log.Fatal("You must specify the cookie name when using a cookie value")
78+
}
79+
if IdCookieVal != "" && IdCookieName != "" {
80+
if IdCookieName == substackSid {
81+
cookie = &http.Cookie{
82+
Name: "substack.sid",
83+
Value: IdCookieVal,
84+
}
85+
} else if IdCookieName == connectSid {
86+
cookie = &http.Cookie{
87+
Name: "connect.sid",
88+
Value: IdCookieVal,
89+
}
5390
}
5491
}
5592
fetcher = lib.NewFetcher(lib.WithRatePerSecond(ratePerSecond), lib.WithProxyURL(parsedProxyURL), lib.WithCookie(cookie))
@@ -62,7 +99,8 @@ func Execute() {
6299

63100
func init() {
64101
rootCmd.PersistentFlags().StringVarP(&proxyURL, "proxy", "x", "", "Specify the proxy url")
65-
rootCmd.PersistentFlags().StringVarP(&substackID, "sid", "i", "", "The substack.sid cookie value (required for private newsletters)")
102+
rootCmd.PersistentFlags().Var(&IdCookieName, "cookie_name", "Either \"substack.sid\" or \"connect.sid\", based on the cookie you have (required for private newsletters)")
103+
rootCmd.PersistentFlags().StringVar(&IdCookieVal, "cookie_val", "", "The substack.sid/connect.sid cookie value (required for private newsletters)")
66104
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output")
67105
rootCmd.PersistentFlags().IntVarP(&ratePerSecond, "rate", "r", lib.DefaultRatePerSecond, "Specify the rate of requests per second")
68106
rootCmd.PersistentFlags().StringVar(&beforeDate, "before", "", "Download posts published before this date (format: YYYY-MM-DD)")

0 commit comments

Comments
 (0)