Skip to content

Commit

Permalink
Add cookies saved in home cache directory
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Apr 29, 2020
1 parent 364c77a commit 7b81b24
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gdown/download.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import print_function

import json
import os
import os.path as osp
import re
Expand All @@ -16,6 +17,7 @@
from .parse_url import parse_url

CHUNK_SIZE = 512 * 1024 # 512KB
home = osp.expanduser("~")


if hasattr(textwrap, "indent"):
Expand Down Expand Up @@ -81,6 +83,17 @@ def download(url, output=None, quiet=False, proxy=None, speed=None):
url_origin = url
sess = requests.session()

# Load cookies
cache_dir = osp.join(home, ".cache", "gdown")
if not osp.exists(cache_dir):
os.makedirs(cache_dir)
cookies_file = osp.join(cache_dir, "cookies.json")
if osp.exists(cookies_file):
with open(cookies_file) as f:
cookies = json.load(f)
for k, v in cookies:
sess.cookies[k] = v

if proxy is not None:
sess.proxies = {"http": proxy, "https": proxy}
print("Using proxy:", proxy, file=sys.stderr)
Expand All @@ -96,6 +109,10 @@ def download(url, output=None, quiet=False, proxy=None, speed=None):
print(e, file=sys.stderr)
return

# Save cookies
with open(cookies_file, "w") as f:
json.dump(sess.cookies.items(), f, indent=2)

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

0 comments on commit 7b81b24

Please sign in to comment.