Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookies saved in home cache directory #51

Merged
merged 3 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ jobs:
steps:
- uses: actions/checkout@v1

- name: Cache cache directory
uses: actions/cache@v1
env:
cache-name: cache-cache-dir
with:
path: ~/.cache
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ matrix.python-version }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
Expand All @@ -40,7 +52,7 @@ jobs:
run: |
pip install black
black --check .
if: matrix.python-version == '3.8'
if: matrix.python-version != '2.7' && matrix.python-version != '3.5'

- name: Test with pytest
run: |
Expand Down
6 changes: 6 additions & 0 deletions gdown/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ def main():
type=file_size,
help="download speed limit in second (e.g., '10MB' -> 10MB/s).",
)
parser.add_argument(
"--no-cookies",
action="store_true",
help="don't use cookies in ~/.cache/gdown/cookies.json",
)

args = parser.parse_args()

Expand All @@ -97,6 +102,7 @@ def main():
quiet=args.quiet,
proxy=args.proxy,
speed=args.speed,
use_cookies=not args.no_cookies,
)


Expand Down
23 changes: 22 additions & 1 deletion 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 @@ -57,7 +59,9 @@ def get_url_from_gdrive_confirmation(contents):
raise RuntimeError(error)


def download(url, output=None, quiet=False, proxy=None, speed=None):
def download(
url, output=None, quiet=False, proxy=None, speed=None, use_cookies=True
):
"""Download file from URL.

Parameters
Expand All @@ -72,6 +76,8 @@ def download(url, output=None, quiet=False, proxy=None, speed=None):
Proxy.
speed: float
Download byte size per second (e.g., 256KB/s = 256 * 1024).
use_cookies: bool
Flag to use cookies. Default is True.

Returns
-------
Expand All @@ -81,6 +87,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) and use_cookies:
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 +113,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