Skip to content

Commit a5166f5

Browse files
committed
Fix linter/formatter findings
1 parent 73bbe5f commit a5166f5

6 files changed

Lines changed: 45 additions & 52 deletions

File tree

.github/workflows/unit_test.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ jobs:
1515
python-version: ${{ matrix.python-version }}
1616
- name: Install uv
1717
uses: astral-sh/setup-uv@v6
18-
- name: Install dependencies
19-
run: |
20-
uv sync --dev --python ${{ matrix.python-version }}
2118
- name: Run lint
2219
run: |
23-
make lint
20+
uv run --python ${{ matrix.python-version }} pylint src/ncoreparser
2421
- name: Check code format
2522
run: |
26-
make check-format
23+
uv run --python ${{ matrix.python-version }} black --check .
2724
- name: Perform tests
2825
run: |
29-
make test
26+
uv run --python ${{ matrix.python-version }} pytest

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ install:
66
uv sync --dev
77

88
lint:
9-
uv run pylint ncoreparser
10-
uv run mypy ncoreparser
9+
uv run pylint src/ncoreparser
10+
uv run mypy src/ncoreparser
1111

1212
check-format:
1313
uv run black --check .

src/ncoreparser/client.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2-
import httpx
32
from typing import Dict, Optional
3+
4+
import httpx
45
from typing_extensions import Any, Generator, Union # pylint: disable=no-name-in-module
6+
57
from ncoreparser.data import URLs, SearchParamType, SearchParamWhere, ParamSort, ParamSeq
68
from ncoreparser.error import NcoreConnectionError, NcoreCredentialError, NcoreDownloadError
79
from ncoreparser.parser import TorrentsPageParser, TorrenDetailParser, RssParser, ActivityParser, RecommendedParser
@@ -11,6 +13,7 @@
1113

1214

1315
class Client:
16+
# pylint: disable=too-many-instance-attributes
1417
def __init__(self, timeout: int = 1, cookies: Optional[Dict[str, str]] = None) -> None:
1518
self._client = httpx.Client(
1619
headers={"User-Agent": "python ncoreparser"}, timeout=timeout, follow_redirects=True
@@ -21,7 +24,7 @@ def __init__(self, timeout: int = 1, cookies: Optional[Dict[str, str]] = None) -
2124
self._rss_parser = RssParser()
2225
self._activity_parser = ActivityParser()
2326
self._recommended_parser = RecommendedParser()
24-
self._allowed_cookies = ['nick', 'pass', 'stilus', 'nyelv', 'PHPSESSID']
27+
self._allowed_cookies = ["nick", "pass", "stilus", "nyelv", "PHPSESSID"]
2528
if cookies:
2629
set_cookies_to_client(self._client, cookies, self._allowed_cookies, URLs.COOKIE_DOMAIN.value)
2730
if self._check_logged_in():
@@ -30,44 +33,38 @@ def __init__(self, timeout: int = 1, cookies: Optional[Dict[str, str]] = None) -
3033
def _check_logged_in(self) -> bool:
3134
try:
3235
r = self._client.get(URLs.INDEX.value)
33-
if 'login.php' in str(r.url) or '<title>nCore</title>' in r.text:
36+
if "login.php" in str(r.url) or "<title>nCore</title>" in r.text:
3437
return False
3538
return True
36-
except Exception:
39+
except Exception: # pylint: disable=broad-except
3740
return False
3841

3942
def login(self, username: str, password: str, twofactorcode: str = "") -> Dict[str, str]:
4043
if self._logged_in and self._check_logged_in():
41-
return extract_cookies_from_client(self._client, self._allowed_cookies, URLs.COOKIE_DOMAIN.value)
42-
44+
return extract_cookies_from_client(self._client, self._allowed_cookies)
45+
4346
self._client.cookies.clear()
4447
self._logged_in = False
45-
48+
4649
try:
47-
login_data = {
48-
"nev": username,
49-
"pass": password,
50-
"set_lang": "hu",
51-
"submitted": "1",
52-
"ne_leptessen_ki": "1"
53-
}
54-
50+
login_data = {"nev": username, "pass": password, "set_lang": "hu", "submitted": "1", "ne_leptessen_ki": "1"}
51+
5552
if twofactorcode:
5653
login_data["2factor"] = twofactorcode
57-
54+
5855
r = self._client.post(URLs.LOGIN.value, data=login_data)
5956
except Exception as e:
6057
raise NcoreConnectionError(f"Error while performing post method to url '{URLs.LOGIN.value}'.") from e
61-
62-
if r.url != URLs.INDEX.value or '<title>nCore</title>' in r.text:
58+
59+
if r.url != URLs.INDEX.value or "<title>nCore</title>" in r.text:
6360
self.logout()
6461
error_msg = f"Error while login, check credentials for user: '{username}'"
6562
if twofactorcode:
6663
error_msg += ". Invalid 2FA code or wait 5 minutes between login attempts."
6764
raise NcoreCredentialError(error_msg)
68-
65+
6966
self._logged_in = True
70-
return extract_cookies_from_client(self._client, self._allowed_cookies, URLs.COOKIE_DOMAIN.value)
67+
return extract_cookies_from_client(self._client, self._allowed_cookies)
7168

7269
@check_login
7370
# pylint: disable=too-many-arguments, too-many-positional-arguments
@@ -171,4 +168,4 @@ def download(self, torrent: Torrent, path: str, override: bool = False) -> str:
171168
def logout(self) -> None:
172169
self._client.cookies.clear()
173170
self._client.close()
174-
self._logged_in = False
171+
self._logged_in = False

src/ncoreparser/client_async.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# pylint: disable=duplicate-code
22

33
import os
4-
import httpx
54
from typing import Dict, Optional
5+
6+
import httpx
67
from typing_extensions import Any, AsyncGenerator, Union # pylint: disable=no-name-in-module
8+
79
from ncoreparser.data import URLs, SearchParamType, SearchParamWhere, ParamSort, ParamSeq
810
from ncoreparser.error import NcoreConnectionError, NcoreCredentialError, NcoreDownloadError
911
from ncoreparser.parser import TorrentsPageParser, TorrenDetailParser, RssParser, ActivityParser, RecommendedParser
@@ -13,6 +15,7 @@
1315

1416

1517
class AsyncClient:
18+
# pylint: disable=too-many-instance-attributes
1619
def __init__(self, timeout: int = 1, cookies: Optional[Dict[str, str]] = None) -> None:
1720
self._client = httpx.AsyncClient(
1821
headers={"User-Agent": "python ncoreparser"}, timeout=timeout, follow_redirects=True
@@ -23,52 +26,46 @@ def __init__(self, timeout: int = 1, cookies: Optional[Dict[str, str]] = None) -
2326
self._rss_parser = RssParser()
2427
self._activity_parser = ActivityParser()
2528
self._recommended_parser = RecommendedParser()
26-
self._allowed_cookies = ['nick', 'pass', 'stilus', 'nyelv', 'PHPSESSID']
29+
self._allowed_cookies = ["nick", "pass", "stilus", "nyelv", "PHPSESSID"]
2730
if cookies:
2831
set_cookies_to_client(self._client, cookies, self._allowed_cookies, URLs.COOKIE_DOMAIN.value)
2932
# Note: Cookie validation for async client happens on first operation since we can't await in __init__
3033

3134
async def _check_logged_in(self) -> bool:
3235
try:
3336
r = await self._client.get(URLs.INDEX.value)
34-
if 'login.php' in str(r.url) or '<title>nCore</title>' in r.text:
37+
if "login.php" in str(r.url) or "<title>nCore</title>" in r.text:
3538
return False
3639
return True
37-
except Exception:
40+
except Exception: # pylint: disable=broad-except
3841
return False
3942

4043
async def login(self, username: str, password: str, twofactorcode: str = "") -> Dict[str, str]:
4144
if self._logged_in and await self._check_logged_in():
42-
return extract_cookies_from_client(self._client, self._allowed_cookies, URLs.COOKIE_DOMAIN.value)
43-
45+
return extract_cookies_from_client(self._client, self._allowed_cookies)
46+
4447
self._client.cookies.clear()
4548
self._logged_in = False
46-
49+
4750
try:
48-
login_data = {
49-
"nev": username,
50-
"pass": password,
51-
"set_lang": "hu",
52-
"submitted": "1",
53-
"ne_leptessen_ki": "1"
54-
}
55-
51+
login_data = {"nev": username, "pass": password, "set_lang": "hu", "submitted": "1", "ne_leptessen_ki": "1"}
52+
5653
if twofactorcode:
5754
login_data["2factor"] = twofactorcode
58-
55+
5956
r = await self._client.post(URLs.LOGIN.value, data=login_data)
6057
except Exception as e:
6158
raise NcoreConnectionError(f"Error while performing post method to url '{URLs.LOGIN.value}'.") from e
62-
63-
if r.url != URLs.INDEX.value or '<title>nCore</title>' in r.text:
59+
60+
if r.url != URLs.INDEX.value or "<title>nCore</title>" in r.text:
6461
await self.logout()
6562
error_msg = f"Error while login, check credentials for user: '{username}'"
6663
if twofactorcode:
6764
error_msg += ". Invalid 2FA code or wait 5 minutes between login attempts."
6865
raise NcoreCredentialError(error_msg)
69-
66+
7067
self._logged_in = True
71-
return extract_cookies_from_client(self._client, self._allowed_cookies, URLs.COOKIE_DOMAIN.value)
68+
return extract_cookies_from_client(self._client, self._allowed_cookies)
7269

7370
@check_login
7471
# pylint: disable=too-many-arguments, too-many-positional-arguments

src/ncoreparser/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ class URLs(Enum):
114114
)
115115
DETAIL_PATTERN = TORRENTS_BASE + "?action=details&id={id}"
116116
DOWNLOAD_LINK = "https://ncore.pro/torrents.php?action=download&id={id}&key={key}"
117-
COOKIE_DOMAIN = "ncore.pro"
117+
COOKIE_DOMAIN = "ncore.pro"

src/ncoreparser/util.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ def parse_datetime(date: str, time: str) -> datetime.datetime:
8686
return datetime.datetime.strptime(f"{date}_{time}", "%Y-%m-%d_%H:%M:%S")
8787

8888

89-
def extract_cookies_from_client(client: Any, allowed_cookies: list[str], domain: str = URLs.COOKIE_DOMAIN.value) -> Dict[str, str]:
89+
def extract_cookies_from_client(client: Any, allowed_cookies: list[str]) -> Dict[str, str]:
9090
cookies_dict = {}
9191
for cookie in client.cookies.jar:
9292
if cookie.name in allowed_cookies:
9393
cookies_dict[cookie.name] = cookie.value
9494
return cookies_dict
9595

9696

97-
def set_cookies_to_client(client: Any, cookies: Dict[str, str], allowed_cookies: list[str], domain: str = URLs.COOKIE_DOMAIN.value) -> None:
97+
def set_cookies_to_client(
98+
client: Any, cookies: Dict[str, str], allowed_cookies: list[str], domain: str = URLs.COOKIE_DOMAIN.value
99+
) -> None:
98100
for name, value in cookies.items():
99101
if name in allowed_cookies:
100102
client.cookies.set(name, value, domain=domain)

0 commit comments

Comments
 (0)