Skip to content

Commit

Permalink
Follow redirects when downloading a file. Fixes #30. (#31)
Browse files Browse the repository at this point in the history
* Follow redirects when downloading a file. Fixes #30.

* Update Python 3.11 to rc2

* Add pre-commit

* Improve type hints
  • Loading branch information
glujan authored Sep 30, 2022
1 parent d92ed31 commit 01afc1e
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 24 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9, '3.10', '3.11.0-beta.1']
python-version: [3.8, 3.9, '3.10', '3.11.0-rc.2']

steps:
- uses: actions/checkout@v2
Expand All @@ -39,3 +39,8 @@ jobs:
- name: Test with dependency matrix
run: |
tox
- name: pypi-publish
uses: pypa/[email protected]
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
with:
password: ${{ secrets.PYPI_API_TOKEN }}
35 changes: 35 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
minimum_pre_commit_version: "2.20.0"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: "v4.3.0"
hooks:
- id: check-ast
- id: check-case-conflict
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v2.38.2
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/PyCQA/isort
rev: "5.10.1"
hooks:
- id: isort
args: ["--profile=black"]
- repo: https://github.com/psf/black
rev: "22.8.0"
hooks:
- id: black
args: ["--target-version=py38"]
- repo: https://github.com/PyCQA/flake8
rev: "5.0.4"
hooks:
- id: flake8
args: ["--config=setup.cfg"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v0.981"
hooks:
- id: mypy
args: ["--ignore-missing-imports", "--python-version=3.10"]
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2022.9.0
* support httpx 0.20-0.22+

## 2022.2.0
* support httpx 0.16-0.22+

Expand Down
8 changes: 4 additions & 4 deletions drpg/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import httpx

if TYPE_CHECKING: # pragma: no cover
from typing import Iterator, List, TypedDict
from typing import Iterator, TypedDict

class TokenResponse(TypedDict):
customers_id: str
Expand All @@ -21,13 +21,13 @@ class Product(TypedDict):
products_id: str
publishers_name: str
products_name: str
files: List[DownloadItem]
files: list[DownloadItem]

class DownloadItem(TypedDict):
filename: str
last_modified: str
bundle_id: str
checksums: List[Checksum]
checksums: list[Checksum]

class Checksum(TypedDict):
checksum: str
Expand Down Expand Up @@ -100,7 +100,7 @@ def file_task(self, product_id: str, item_id: str) -> FileTasksResponse:
logger.debug("Got download link for: %s - %s", product_id, item_id)
return data

def _product_page(self, page: int, per_page: int) -> List[Product]:
def _product_page(self, page: int, per_page: int) -> list[Product]:
"""
List products from a specified page.
Expand Down
12 changes: 6 additions & 6 deletions drpg/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

if TYPE_CHECKING: # pragma: no cover
from types import FrameType, TracebackType
from typing import List, Optional, Type

CliArgs = List[str]
CliArgs = list[str]

__all__ = ["run"]

Expand All @@ -29,10 +28,11 @@ def run() -> None:
sys.excepthook = _excepthook
config = _parse_cli()
_setup_logger(config.log_level)

DrpgSync(config).sync()


def _parse_cli(args: Optional[CliArgs] = None) -> Config:
def _parse_cli(args: CliArgs | None = None) -> Config:
parser = argparse.ArgumentParser(
prog="drpg",
description="Download and keep up to date your purchases from DriveThruRPG",
Expand Down Expand Up @@ -78,7 +78,7 @@ def _default_dir() -> Path:
if os_name == "Linux":
xdg_config = Path(environ.get("XDG_CONFIG_HOME", "~/.config")).expanduser()
try:
with open(xdg_config / "user-dirs.dirs", "r") as f:
with open(xdg_config / "user-dirs.dirs") as f:
raw_config = "[xdg]\n" + f.read().replace('"', "")
config = configparser.ConfigParser()
config.read_string(raw_config)
Expand All @@ -105,13 +105,13 @@ def _setup_logger(level_name: str) -> None:
)


def _handle_signal(sig: int, frame: FrameType) -> None:
def _handle_signal(sig: int, frame: FrameType | None) -> None:
logging.getLogger("drpg").info("Stopping...")
sys.exit(0)


def _excepthook(
exc_type: Type[BaseException], exc: BaseException, tb: TracebackType
exc_type: type[BaseException], exc: BaseException, tb: TracebackType | None
) -> None:
logger = logging.getLogger("drpg")
logger.error("Unexpected error occurred, stopping!")
Expand Down
10 changes: 6 additions & 4 deletions drpg/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

if TYPE_CHECKING: # pragma: no cover
from pathlib import Path
from typing import Any, Callable, Optional, Type
from typing import Any, Callable

from drpg.api import DownloadItem, Product
from drpg.config import Config
Expand All @@ -28,7 +28,7 @@
logger = logging.getLogger("drpg")


def suppress_errors(*errors: Type[Exception]) -> Decorator:
def suppress_errors(*errors: type[Exception]) -> Decorator:
"""Silence but log provided errors."""

def decorator(func: NoneCallable) -> NoneCallable:
Expand Down Expand Up @@ -74,7 +74,9 @@ def _process_item(self, product: Product, item: DownloadItem) -> None:
logger.info("Processing: %s - %s", product["products_name"], item["filename"])

url_data = self._api.file_task(product["products_id"], item["bundle_id"])
file_response = httpx.get(url_data["download_url"], timeout=30.0)
file_response = httpx.get(
url_data["download_url"], timeout=30.0, follow_redirects=True
)

path = self._file_path(product, item)
path.parent.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -120,7 +122,7 @@ def _escape_path_part(part: str) -> str:
return part


def _newest_checksum(item: DownloadItem) -> Optional[str]:
def _newest_checksum(item: DownloadItem) -> str | None:
return max(
item["checksums"],
default={"checksum": None},
Expand Down
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
httpx>=0.16
httpx>=0.20
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import setuptools


if __name__ == "__main__":
# setuptools does not support setting install_requires from a file in setup.cfg
with open("requirements.in", "r", encoding="utf-8") as fh:
with open("requirements.in", encoding="utf-8") as fh:
install_requires = fh.read().splitlines()

setuptools.setup(install_requires=install_requires)
6 changes: 0 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
[tox]
skipsdist = true
envlist =
py3-httpx{016,017}-respx016
py3-httpx{018,019}-respx017
py3-httpx{020,100}-respx018
py3-httpx{021,022,023}-respx019

[testenv]
deps =
py3: -rrequirements.in
httpx016: httpx>=0.16,<0.17
httpx017: httpx>=0.17,<0.18
httpx018: httpx>=0.18,<0.19
httpx019: httpx>=0.19,<0.20
httpx020: httpx>=0.20,<0.21
httpx021: httpx>=0.21,<0.22
httpx022: httpx>=0.22,<0.23
Expand Down

0 comments on commit 01afc1e

Please sign in to comment.