Skip to content

Commit

Permalink
retry
Browse files Browse the repository at this point in the history
  • Loading branch information
eggplants committed Jan 4, 2025
1 parent 747ef47 commit 854ed91
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
24 changes: 12 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@ repos:
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.1
rev: v0.8.6
hooks:
- id: ruff
args: [--fix]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.13.0
rev: v1.14.1
hooks:
- id: mypy
files: ^getjump/
args: [--strict]
additional_dependencies:
- rich
- types-beautifulsoup4
- types-requests
- types-pillow
- repo: https://github.com/murilo-cunha/mirrors-pyre
rev: v0.9.23
hooks:
- id: pyre-check
additional_dependencies:
- types-pillow
- pytest
- rich
- types-beautifulsoup4
- types-requests
# - repo: https://github.com/murilo-cunha/mirrors-pyre
# rev: v0.9.23
# hooks:
# - id: pyre-check
# additional_dependencies:
# - types-pillow
# - pytest
# - rich
# - types-beautifulsoup4
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.43.0
hooks:
Expand Down
20 changes: 15 additions & 5 deletions getjump/getjump.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import warnings
from io import BytesIO
from pathlib import Path
from typing import TypedDict
from typing import TYPE_CHECKING, TypedDict
from urllib.parse import urlparse

import requests
from bs4 import BeautifulSoup
from bs4.element import Tag
from PIL import Image
from requests import Session
from requests.adapters import HTTPAdapter, Retry
from rich.progress import (
BarColumn,
MofNCompleteColumn,
Expand All @@ -24,6 +25,9 @@
TimeRemainingColumn,
)

if TYPE_CHECKING:
from requests import Response

HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) "
Expand Down Expand Up @@ -77,7 +81,13 @@ class NeedPurchase(Warning):
class GetJump:
def __init__(self) -> None:
self._logged_in_hosts: list[str] = []
self._session: requests.Session = requests.Session()

session = Session()
retry_adapter = HTTPAdapter(max_retries=Retry(total=10, backoff_factor=1))
session.mount("https://", retry_adapter)
session.mount("http://", retry_adapter)

self._session = session

def get( # noqa: PLR0913
self,
Expand Down Expand Up @@ -165,7 +175,7 @@ def login(
username: str | None = None,
password: str | None = None,
overwrite: bool = False,
) -> requests.Response | None:
) -> Response | None:
if username is None and password is None:
return None # needless to login
o = urlparse(url)
Expand Down Expand Up @@ -246,7 +256,7 @@ def __save_images(
progress.update(task_save, advance=1)

def __get_image(self, image_src: str, div: int = 4, mul: int = 8) -> Image.Image:
img = Image.open(BytesIO(requests.get(image_src, timeout=10).content))
img = Image.open(BytesIO(self._session.get(image_src, timeout=10).content))
img_width, img_height = img.size
fixed_width = int(float(img_width) / (div * mul)) * mul
fixed_height = int(float(img_height) / (div * mul)) * mul
Expand Down
11 changes: 5 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,20 @@ target-version = [
[tool.ruff]
line-length = 120

select = [
lint.select = [
"ALL",
]
ignore = [
lint.ignore = [
"D",
"ANN101",
]
per-file-ignores."main.py" = [
lint.per-file-ignores."main.py" = [
"T201", # `print` found
]
per-file-ignores."tests/*test_*.py" = [
lint.per-file-ignores."tests/*test_*.py" = [
"INP001", # File tests/test_*.py is part of an implicit namespace package. Add an __init__.py.
"S101", # Use of assert detected
]
mccabe.max-complexity = 18
lint.mccabe.max-complexity = 18

[tool.mypy]
pretty = true
Expand Down

0 comments on commit 854ed91

Please sign in to comment.