Skip to content

Commit

Permalink
добавил поддержку прокси
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rgeym committed Nov 14, 2024
1 parent 5b4c0ff commit 0306ada
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 25 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,12 @@ $ hh-applicant-tool refresh-token
| **Linux** | `~/.config/hh-applicant-tool/config.json` |


Через этот файл, например, можно задать кастомный `user_agent`:
Через конфиг можно задать дополнительные настройки:

```json
{
"user_agent": "Mozilla/5.0 YablanBrowser"
}
```
| Имя атрибута | Описание |
| `user_agent` | Кастомный юзерагент, передаваемый при кажом запросе, например, `Mozilla/5.0 YablanBrowser` |
| `proxy_url` | Прокси, используемый для всех запросов, например, `socks5h://127.0.0.1:9050` |
| `reply_message` | Сообщение для ответа работодателю при отклике на вакансии, см. формат сообщений |

### Описание команд

Expand Down
6 changes: 5 additions & 1 deletion hh_applicant_tool/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class BaseClient:
_: dataclasses.KW_ONLY
# TODO: сделать генерацию User-Agent'а как в приложении
user_agent: str | None = None
proxies: dict | None = None
session: Session | None = None
previous_request_time: float = 0.0
delay: float = 0.334
Expand All @@ -47,8 +48,8 @@ def __post_init__(self) -> None:
self.session = session = requests.session()
session.headers.update(
{
**self.additional_headers(),
"User-Agent": self.user_agent or self.default_user_agent(),
**self.additional_headers(),
}
)
logger.debug("Default Headers: %r", session.headers)
Expand Down Expand Up @@ -101,10 +102,13 @@ def request(
logger.debug("wait %fs before request", delay)
time.sleep(delay)
has_body = method in ["POST", "PUT"]
user_agent = self.user_agent or self.default_user_agent()
logger.debug(f"{user_agent = }")
response = self.session.request(
method,
url,
**{"data" if has_body else "params": params},
proxies=self.proxies,
allow_redirects=False,
)
try:
Expand Down
25 changes: 21 additions & 4 deletions hh_applicant_tool/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import sys
from abc import ABCMeta, abstractmethod
from importlib import import_module
from os import getenv
from pathlib import Path
from pkgutil import iter_modules
from typing import Sequence
from typing import Sequence, Literal
from .api import ApiClient
from .color_log import ColorHandler
from .utils import Config, get_config_path
from os import getenv

DEFAULT_CONFIG_PATH = (
get_config_path() / __package__.replace("_", "-") / "config.json"
Expand All @@ -34,15 +34,25 @@ class Namespace(argparse.Namespace):
config: Config
verbosity: int
delay: float
user_agent: str
proxy_url: str


def get_proxies(args: Namespace) -> dict[Literal["http", "https"], str | None]:
return {
"http": args.config["proxy_url"] or getenv("HTTP_PROXY"),
"https": args.config["proxy_url"] or getenv("HTTPS_PROXY"),
}


def get_api(args: Namespace) -> ApiClient:
token = args.config.get("token", {})
api = ApiClient(
access_token=token.get("access_token"),
refresh_token=token.get("refresh_token"),
user_agent=args.config["user_agent"],
delay=args.delay,
user_agent=args.config["user_agent"],
proxies=get_proxies(args),
)
return api

Expand Down Expand Up @@ -87,14 +97,21 @@ def create_parser(self) -> argparse.ArgumentParser:
default=0.334,
help="Задержка между запросами к API HH",
)
parser.add_argument(
"--user-agent", help="User-Agent для каждого запроса"
)
parser.add_argument(
"--proxy-url", help="Прокси, используемый для запросов к API"
)
subparsers = parser.add_subparsers(help="commands")
package_dir = Path(__file__).resolve().parent / OPERATIONS
for _, module_name, _ in iter_modules([str(package_dir)]):
mod = import_module(f"{__package__}.{OPERATIONS}.{module_name}")
op: BaseOperation = mod.Operation()
op_parser = subparsers.add_parser(
module_name.replace("_", "-"),
description=op.__doc__, formatter_class=self.ArgumentFormatter
description=op.__doc__,
formatter_class=self.ArgumentFormatter,
)
op_parser.set_defaults(run=op.run)
op.setup_parser(op_parser)
Expand Down
12 changes: 8 additions & 4 deletions hh_applicant_tool/operations/apply_similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from ..api import ApiClient, ApiError, BadRequest
from ..main import BaseOperation
from ..main import Namespace as BaseNamespace, get_api
from ..telemetry_client import TelemetryError
from ..telemetry_client import get_client as get_telemetry_client
from ..telemetry_client import TelemetryClient, TelemetryError
from ..types import ApiListResponse, VacancyItem
from ..utils import fix_datetime, print_err, truncate_string, random_text
from ..utils import fix_datetime, truncate_string, random_text
from requests import Session

logger = logging.getLogger(__package__)

Expand Down Expand Up @@ -159,7 +159,11 @@ def _apply_similar(
search: str | None = None,
reply_message: str | None = None,
) -> None:
telemetry_client = get_telemetry_client()
# TODO: вынести куда-нибудь в функцию
session = Session()
session.headers["User-Agent"] = "Mozilla/5.0 (HHApplicantTelemetry/1.0)"
session.proxies = dict(api.session.proxies)
telemetry_client = TelemetryClient(session=session)
telemetry_data = defaultdict(dict)

vacancies = self._get_vacancies(
Expand Down
6 changes: 0 additions & 6 deletions hh_applicant_tool/telemetry_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from urllib.parse import urljoin
import requests
from typing import Optional, Dict, Any
from functools import cache
import logging
import base64

Expand Down Expand Up @@ -64,8 +63,3 @@ def send_telemetry(
json.JSONDecodeError,
) as ex:
raise TelemetryError(str(ex)) from ex


@cache
def get_client() -> TelemetryClient:
return TelemetryClient()
3 changes: 2 additions & 1 deletion hh_applicant_tool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from typing import Any
from os import getenv
from .constants import INVALID_ISO8601_FORMAT
import re, random
import re
import random

print_err = partial(print, file=sys.stderr, flush=True)

Expand Down
15 changes: 14 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[tool.poetry]
name = "hh-applicant-tool"
version = "0.3.7"
version = "0.3.8"
description = ""
authors = ["Senior YAML Developer <yamldeveloper@proton.me>"]
readme = "README.md"
packages = [{include = "hh_applicant_tool"}]

[tool.poetry.dependencies]
python = "^3.10"
requests = "^2.28.2"
requests = {extras = ["socks"], version = "^2.32.3"}
prettytable = "^3.6.0"
pyqt6 = { version = "^6.7.1", optional = true }
pyqt6-webengine = { version = "^6.7.0", optional = true }
Expand Down

0 comments on commit 0306ada

Please sign in to comment.