Skip to content

Commit

Permalink
Use MozillaCookieJar instead of json to save cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Jan 20, 2024
1 parent f68cdfe commit 30fa8e7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gdown/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def main():
parser.add_argument(
"--no-cookies",
action="store_true",
help="don't use cookies in ~/.cache/gdown/cookies.json",
help="don't use cookies in ~/.cache/gdown/cookies.txt",
)
parser.add_argument(
"--no-check-certificate",
Expand Down
27 changes: 10 additions & 17 deletions gdown/download.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import os
import os.path as osp
import re
Expand All @@ -8,6 +7,7 @@
import textwrap
import time
import urllib
from http.cookiejar import MozillaCookieJar

import requests
import tqdm
Expand Down Expand Up @@ -62,12 +62,11 @@ def _get_session(proxy, use_cookies, user_agent, return_cookies_file=False):
print("Using proxy:", proxy, file=sys.stderr)

# Load cookies if exists
cookies_file = osp.join(home, ".cache/gdown/cookies.json")
if osp.exists(cookies_file) and use_cookies:
with open(cookies_file) as f:
cookies = json.load(f)
for k, v in cookies:
sess.cookies[k] = v
cookies_file = osp.join(home, ".cache/gdown/cookies.txt")
if use_cookies and osp.exists(cookies_file):
cookie_jar = MozillaCookieJar(cookies_file)
cookie_jar.load()
sess.cookies.update(cookie_jar)

if return_cookies_file:
return sess, cookies_file
Expand Down Expand Up @@ -206,16 +205,10 @@ def download(
continue

if use_cookies:
if not osp.exists(osp.dirname(cookies_file)):
os.makedirs(osp.dirname(cookies_file))
# Save cookies
with open(cookies_file, "w") as f:
cookies = [
(k, v)
for k, v in sess.cookies.items()
if not k.startswith("download_warning_")
]
json.dump(cookies, f, indent=2)
cookie_jar = MozillaCookieJar(cookies_file)
for cookie in sess.cookies:
cookie_jar.set_cookie(cookie)
cookie_jar.save()

if "Content-Disposition" in res.headers:
# This is the file
Expand Down

0 comments on commit 30fa8e7

Please sign in to comment.