torrent-tracker-search 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. torrent_tracker_search-0.2.0/PKG-INFO +52 -0
  2. torrent_tracker_search-0.2.0/README.md +33 -0
  3. torrent_tracker_search-0.2.0/pyproject.toml +17 -0
  4. torrent_tracker_search-0.2.0/torrent_tracker_search/ClientBuilder.py +20 -0
  5. torrent_tracker_search-0.2.0/torrent_tracker_search/__init__.py +0 -0
  6. torrent_tracker_search-0.2.0/torrent_tracker_search/common/TrackerClient.py +19 -0
  7. torrent_tracker_search-0.2.0/torrent_tracker_search/common/TrackerRequestException.py +3 -0
  8. torrent_tracker_search-0.2.0/torrent_tracker_search/common/TrackerResponse.py +25 -0
  9. torrent_tracker_search-0.2.0/torrent_tracker_search/common/__init__.py +0 -0
  10. torrent_tracker_search-0.2.0/torrent_tracker_search/source/__init__.py +0 -0
  11. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/NNMClubClient.py +47 -0
  12. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/NNMClubService.py +72 -0
  13. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/NNMClubURL.py +15 -0
  14. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/__init__.py +0 -0
  15. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/model/NNMClubAuthorLink.py +7 -0
  16. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/model/NNMClubForumLink.py +7 -0
  17. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/model/NNMClubSearchItem.py +18 -0
  18. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/model/NNMClubTopicLink.py +7 -0
  19. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/model/NNMClubTorrentFile.py +10 -0
  20. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/model/NNMClubTorrentItem.py +14 -0
  21. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/model/__init__.py +0 -0
  22. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/parser/NNMClubBBCodeParser.py +15 -0
  23. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/parser/NNMClubSearchParser.py +55 -0
  24. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/parser/NNMClubTorrentFilesParser.py +96 -0
  25. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/parser/NNMClubTorrentParser.py +30 -0
  26. torrent_tracker_search-0.2.0/torrent_tracker_search/source/nnmclub/parser/__init__.py +25 -0
  27. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/RuTrackerClient.py +47 -0
  28. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/RuTrackerService.py +98 -0
  29. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/RuTrackerURL.py +16 -0
  30. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/__init__.py +0 -0
  31. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/model/RuTrackerAuthorLink.py +7 -0
  32. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/model/RuTrackerForumLink.py +7 -0
  33. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/model/RuTrackerSearchItem.py +21 -0
  34. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/model/RuTrackerTorrentFile.py +9 -0
  35. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/model/RuTrackerTorrentItem.py +14 -0
  36. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/model/__init__.py +0 -0
  37. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/parser/RuTrackerSearchParser.py +54 -0
  38. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/parser/RuTrackerTorrentFilesParser.py +57 -0
  39. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/parser/RuTrackerTorrentParser.py +32 -0
  40. torrent_tracker_search-0.2.0/torrent_tracker_search/source/rutracker/parser/__init__.py +23 -0
@@ -0,0 +1,52 @@
1
+ Metadata-Version: 2.3
2
+ Name: torrent-tracker-search
3
+ Version: 0.2.0
4
+ Summary: Torrent Tracker Search Lib
5
+ Home-page: https://gitlab.com/Bitnik212/torrent-tracker-search
6
+ License: MIT
7
+ Author: Николай Витальевич Никоноров
8
+ Author-email: nnv@bitt.moe
9
+ Requires-Python: >=3.10.16,<4.0.0
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Requires-Dist: aiohttp (==3.11.11)
16
+ Requires-Dist: beautifulsoup4 (==4.12.3)
17
+ Project-URL: Repository, https://gitlab.com/Bitnik212/torrent-tracker-search
18
+ Description-Content-Type: text/markdown
19
+
20
+ # Torrent Tracker Search Lib
21
+
22
+ ## Integrations
23
+
24
+ - rutracker.org
25
+ - nnmclub.to
26
+
27
+ ## Example
28
+
29
+ ```python
30
+ from torrent_tracker_search.ClientBuilder import ClientBuilder
31
+ from torrent_tracker_search.source.rutracker.RuTrackerService import RuTrackerService
32
+ from torrent_tracker_search.source.rutracker.RuTrackerClient import RuTrackerClient
33
+ from torrent_tracker_search.source.nnmclub.NNMClubService import NNMClubService
34
+ from torrent_tracker_search.source.nnmclub.NNMClubClient import NNMClubClient
35
+
36
+ builder = ClientBuilder()
37
+ rutracker_client, nnmclub_client = builder.build(clients=[RuTrackerClient(), NNMClubClient()])
38
+ rutracker_service = RuTrackerService(rutracker_client)
39
+ nnmclub_service = NNMClubService(rutracker_client)
40
+
41
+
42
+ async def main(query: str):
43
+ await rutracker_service.login(login="", password="")
44
+ nnmclub_service.login(session_id="")
45
+ result_from_rutracker = await rutracker_service.search(query)
46
+ result_from_nnmclub = await nnmclub_service.search(query)
47
+ print(f"{result_from_rutracker=}")
48
+ print(f"{result_from_nnmclub=}")
49
+ await builder.session.close()
50
+
51
+
52
+ ```
@@ -0,0 +1,33 @@
1
+ # Torrent Tracker Search Lib
2
+
3
+ ## Integrations
4
+
5
+ - rutracker.org
6
+ - nnmclub.to
7
+
8
+ ## Example
9
+
10
+ ```python
11
+ from torrent_tracker_search.ClientBuilder import ClientBuilder
12
+ from torrent_tracker_search.source.rutracker.RuTrackerService import RuTrackerService
13
+ from torrent_tracker_search.source.rutracker.RuTrackerClient import RuTrackerClient
14
+ from torrent_tracker_search.source.nnmclub.NNMClubService import NNMClubService
15
+ from torrent_tracker_search.source.nnmclub.NNMClubClient import NNMClubClient
16
+
17
+ builder = ClientBuilder()
18
+ rutracker_client, nnmclub_client = builder.build(clients=[RuTrackerClient(), NNMClubClient()])
19
+ rutracker_service = RuTrackerService(rutracker_client)
20
+ nnmclub_service = NNMClubService(rutracker_client)
21
+
22
+
23
+ async def main(query: str):
24
+ await rutracker_service.login(login="", password="")
25
+ nnmclub_service.login(session_id="")
26
+ result_from_rutracker = await rutracker_service.search(query)
27
+ result_from_nnmclub = await nnmclub_service.search(query)
28
+ print(f"{result_from_rutracker=}")
29
+ print(f"{result_from_nnmclub=}")
30
+ await builder.session.close()
31
+
32
+
33
+ ```
@@ -0,0 +1,17 @@
1
+ [tool.poetry]
2
+ name = "torrent-tracker-search"
3
+ version = "0.2.0"
4
+ description = "Torrent Tracker Search Lib"
5
+ authors = ["Николай Витальевич Никоноров <nnv@bitt.moe>"]
6
+ license = "MIT"
7
+ readme = "README.md"
8
+ repository = "https://gitlab.com/Bitnik212/torrent-tracker-search"
9
+
10
+ [tool.poetry.dependencies]
11
+ python = "^3.10.16"
12
+ aiohttp = "3.11.11"
13
+ beautifulsoup4 = "4.12.3"
14
+
15
+ [build-system]
16
+ requires = ["poetry-core"]
17
+ build-backend = "poetry.core.masonry.api"
@@ -0,0 +1,20 @@
1
+ from typing import List, Optional, Final, Dict
2
+
3
+ from aiohttp import ClientSession
4
+
5
+ from torrent_tracker_search.common.TrackerClient import TrackerClient
6
+
7
+
8
+ class ClientBuilder:
9
+
10
+ DEFAULT_HEADERS: Final[Dict[str, str]] = {
11
+ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
12
+ }
13
+
14
+ def __init__(self, session: Optional[ClientSession] = None):
15
+ self.session: ClientSession = session or ClientSession(headers=self.DEFAULT_HEADERS)
16
+
17
+ def build(self, clients: List[TrackerClient]) -> List[TrackerClient]:
18
+ for client in clients:
19
+ client.session = self.session
20
+ return clients
@@ -0,0 +1,19 @@
1
+ from typing import Optional
2
+
3
+ from aiohttp import ClientSession
4
+
5
+ from torrent_tracker_search.common.TrackerResponse import TrackerResponse
6
+
7
+
8
+ class TrackerClient:
9
+
10
+ session: Optional[ClientSession] = None
11
+
12
+ async def get(self, url: str, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse: ...
13
+
14
+ async def post_raw(self, url: str, body: dict, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse: ...
15
+
16
+ async def post_form_data(self, url: str, body: dict, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse: ...
17
+
18
+ async def post_form_data_encoded(self, url: str, body: dict, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse: ...
19
+
@@ -0,0 +1,3 @@
1
+
2
+
3
+ class TrackerRequestException(Exception): ...
@@ -0,0 +1,25 @@
1
+ from dataclasses import dataclass
2
+ from typing import Dict
3
+
4
+ from multidict import CIMultiDict
5
+
6
+
7
+ @dataclass
8
+ class TrackerResponse:
9
+ headers: CIMultiDict[str]
10
+ status_code: int
11
+ body: bytes
12
+
13
+ @property
14
+ def cookies(self) -> Dict[str, str]:
15
+ cookies = {}
16
+ cookie_string = getattr(self.headers, "cookies", "")
17
+ if cookie_string == "":
18
+ return cookies
19
+ cookie_strings = cookie_string.split(";")
20
+ for cookie_string in cookie_strings:
21
+ cookie_name, cookie_value = cookie_string.split("=")
22
+ cookie_name = cookie_name.strip()
23
+ cookies[cookie_name] = cookie_value
24
+ return cookies
25
+
@@ -0,0 +1,47 @@
1
+ from typing import Final, Optional
2
+
3
+ from torrent_tracker_search.common.TrackerClient import TrackerClient
4
+ from torrent_tracker_search.common.TrackerRequestException import TrackerRequestException
5
+ from torrent_tracker_search.common.TrackerResponse import TrackerResponse
6
+
7
+
8
+ class NNMClubClient(TrackerClient):
9
+ BASE_URL: Final[str] = "https://nnmclub.to/forum"
10
+ SESSION_COOKIE_KEY: Final[str] = "phpbb2mysql_4_sid"
11
+
12
+ async def get(self, url: str, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse:
13
+ async with self.session.get(url=url, params=params, ssl=False, allow_redirects=allow_redirects) as response:
14
+ success_status_code = 200 if allow_redirects else 302
15
+ if response.status != success_status_code:
16
+ raise TrackerRequestException()
17
+ else:
18
+ return TrackerResponse(
19
+ headers=response.headers.copy(),
20
+ status_code=response.status,
21
+ body=await response.content.read()
22
+ )
23
+
24
+ async def post_form_data_encoded(self, url: str, body: dict, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse:
25
+ async with self.session.post(url=url, data=body, ssl=False, allow_redirects=allow_redirects, params=params) as response:
26
+ success_status_code = 200 if allow_redirects else 302
27
+ if response.status == success_status_code:
28
+ return TrackerResponse(
29
+ headers=response.headers.copy(),
30
+ status_code=response.status,
31
+ body=await response.content.read()
32
+ )
33
+ else:
34
+ raise TrackerRequestException()
35
+
36
+ async def post_form_data(self, url: str, body: dict, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse:
37
+ async with self.session.post(url=url, data=body, ssl=False, allow_redirects=allow_redirects, params=params) as response:
38
+ success_status_code = 200 if allow_redirects else 302
39
+ if response.status == success_status_code:
40
+ return TrackerResponse(
41
+ headers=response.headers.copy(),
42
+ status_code=response.status,
43
+ body=await response.content.read()
44
+ )
45
+ else:
46
+ raise TrackerRequestException()
47
+
@@ -0,0 +1,72 @@
1
+ from typing import List, Optional
2
+
3
+ from torrent_tracker_search.common.TrackerClient import TrackerClient
4
+ from torrent_tracker_search.source.nnmclub.NNMClubClient import NNMClubClient
5
+ from torrent_tracker_search.source.nnmclub.NNMClubURL import NNMClubURL
6
+ from torrent_tracker_search.source.nnmclub.model.NNMClubSearchItem import NNMClubSearchItem
7
+ from torrent_tracker_search.source.nnmclub.model.NNMClubTorrentFile import NNMClubTorrentFile
8
+ from torrent_tracker_search.source.nnmclub.model.NNMClubTorrentItem import NNMClubTorrentItem
9
+ from torrent_tracker_search.source.nnmclub.parser.NNMClubBBCodeParser import NNMClubBBCodeParser
10
+ from torrent_tracker_search.source.nnmclub.parser.NNMClubSearchParser import NNMClubSearchParser
11
+ from torrent_tracker_search.source.nnmclub.parser.NNMClubTorrentFilesParser import NNMClubTorrentFilesParser
12
+ from torrent_tracker_search.source.nnmclub.parser.NNMClubTorrentParser import NNMClubTorrentParser
13
+
14
+
15
+ class NNMClubService:
16
+
17
+ def __init__(self, client: TrackerClient):
18
+ self._client: TrackerClient = client
19
+
20
+ async def search(self, query: str) -> List[NNMClubSearchItem]:
21
+ response = await self._client.get(
22
+ url=NNMClubURL.TRACKER.url,
23
+ params={
24
+ "nm": query,
25
+ "f": "-1",
26
+ "search_submit": ""
27
+ }
28
+ )
29
+ body = response.body.decode("Windows-1251")
30
+ return NNMClubSearchParser.parse(body)
31
+
32
+ def login(self, session_id: str):
33
+ self._client.session.cookie_jar.update_cookies({NNMClubClient.SESSION_COOKIE_KEY: session_id})
34
+
35
+ async def torrent_files(self, torrent_id: int) -> Optional[NNMClubTorrentFile]:
36
+ response = await self._client.get(
37
+ url=NNMClubURL.FILE_LIST.url,
38
+ params={
39
+ "attach_id": torrent_id
40
+ }
41
+ )
42
+ return NNMClubTorrentFilesParser.parse(response.body.decode("Windows-1251"))
43
+
44
+ async def torrent_bb_code(self, torrent_post_id: int) -> Optional[str]:
45
+ response = await self._client.get(
46
+ url=NNMClubURL.POSTING.url,
47
+ params={
48
+ "mode": "quote",
49
+ "p": torrent_post_id
50
+ }
51
+ )
52
+ return NNMClubBBCodeParser.parse(response.body.decode("Windows-1251"))
53
+
54
+ async def torrent(self, topic_id: int) -> Optional[NNMClubTorrentItem]:
55
+ response = await self._client.get(
56
+ url=NNMClubURL.VIEW_TOPIC.url,
57
+ params={
58
+ "t": topic_id
59
+ }
60
+ )
61
+ data = NNMClubTorrentParser.parse(response.body.decode("Windows-1251"))
62
+ if data is not None:
63
+ return NNMClubTorrentItem(
64
+ id=data["id"],
65
+ post_id=data["post_id"],
66
+ html_code=data["html_code"],
67
+ magnet_link=data["magnet_link"],
68
+ bb_code=await self.torrent_bb_code(torrent_post_id=data["post_id"]),
69
+ files=await self.torrent_files(torrent_id=data["id"])
70
+ )
71
+ else:
72
+ return None
@@ -0,0 +1,15 @@
1
+ from enum import Enum
2
+
3
+ from torrent_tracker_search.source.nnmclub.NNMClubClient import NNMClubClient
4
+
5
+
6
+ class NNMClubURL(Enum):
7
+ MAIN = "index.php"
8
+ TRACKER = "tracker.php"
9
+ POSTING = "posting.php"
10
+ VIEW_TOPIC = "viewtopic.php"
11
+ FILE_LIST = "filelst.php"
12
+
13
+ @property
14
+ def url(self) -> str:
15
+ return f"{NNMClubClient.BASE_URL}/{self.value}"
@@ -0,0 +1,7 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class NNMClubAuthorLink:
6
+ id: int
7
+ name: str
@@ -0,0 +1,7 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class NNMClubForumLink:
6
+ id: int
7
+ name: str
@@ -0,0 +1,18 @@
1
+ from dataclasses import dataclass
2
+ from datetime import datetime
3
+
4
+ from torrent_tracker_search.source.nnmclub.model.NNMClubAuthorLink import NNMClubAuthorLink
5
+ from torrent_tracker_search.source.nnmclub.model.NNMClubForumLink import NNMClubForumLink
6
+ from torrent_tracker_search.source.nnmclub.model.NNMClubTopicLink import NNMClubTopicLink
7
+
8
+
9
+ @dataclass
10
+ class NNMClubSearchItem:
11
+ id: int # id for download torrent file
12
+ author: NNMClubAuthorLink
13
+ forum: NNMClubForumLink
14
+ topic: NNMClubTopicLink
15
+ size: int
16
+ seed_count: int
17
+ leech_count: int
18
+ added_at: datetime
@@ -0,0 +1,7 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class NNMClubTopicLink:
6
+ id: int
7
+ name: str
@@ -0,0 +1,10 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class NNMClubTorrentFile:
6
+ name: str
7
+ is_folder: bool
8
+ files: list
9
+ size: int
10
+
@@ -0,0 +1,14 @@
1
+ from dataclasses import dataclass
2
+ from typing import Optional, List
3
+
4
+ from torrent_tracker_search.source.nnmclub.model.NNMClubTorrentFile import NNMClubTorrentFile
5
+
6
+
7
+ @dataclass
8
+ class NNMClubTorrentItem:
9
+ id: int
10
+ post_id: int
11
+ html_code: str
12
+ magnet_link: str
13
+ bb_code: Optional[str] = None
14
+ files: Optional[List[NNMClubTorrentFile]] = None
@@ -0,0 +1,15 @@
1
+ from typing import Optional
2
+
3
+ from bs4 import BeautifulSoup
4
+
5
+
6
+ class NNMClubBBCodeParser:
7
+
8
+ @classmethod
9
+ def parse(cls, html: str) -> Optional[str]:
10
+ soup = BeautifulSoup(html, "html.parser")
11
+ textarea = soup.find("textarea")
12
+ if textarea is not None:
13
+ return textarea.text
14
+ else:
15
+ return None
@@ -0,0 +1,55 @@
1
+ from datetime import datetime
2
+ from typing import List
3
+
4
+ from bs4 import BeautifulSoup, Tag
5
+
6
+ from torrent_tracker_search.source.nnmclub.model.NNMClubAuthorLink import NNMClubAuthorLink
7
+ from torrent_tracker_search.source.nnmclub.model.NNMClubForumLink import NNMClubForumLink
8
+ from torrent_tracker_search.source.nnmclub.model.NNMClubSearchItem import NNMClubSearchItem
9
+ from torrent_tracker_search.source.nnmclub.model.NNMClubTopicLink import NNMClubTopicLink
10
+ from torrent_tracker_search.source.nnmclub.parser import get_forum_id, get_user_id, get_torrent_id, get_topic_id
11
+
12
+
13
+ class NNMClubSearchParser:
14
+
15
+ @classmethod
16
+ def parse(cls, html: str) -> List[NNMClubSearchItem]:
17
+ soup = BeautifulSoup(html, "html.parser")
18
+ content_table = soup.find("table", attrs={"class": "forumline tablesorter"})
19
+ result = list()
20
+ for item_tr in content_table.tbody.childGenerator():
21
+ if type(item_tr) is Tag:
22
+ result.append(cls.parse_item(item_tr))
23
+ return result
24
+
25
+ @classmethod
26
+ def parse_item(cls, item_tr: Tag) -> NNMClubSearchItem:
27
+ items = [td for td in item_tr.find_all("td")]
28
+ if len(items) == 10:
29
+ _, forum_td, topic_td, author_td, torrent_td, size_td, seed_td, leech_td, reply_td, added_at_td = items
30
+ elif len(items) == 11:
31
+ _, forum_td, topic_td, author_td, torrent_td, size_td, _, seed_td, leech_td, reply_td, added_at_td = items
32
+ else:
33
+ raise Exception(f"Allowed only 10 items. Parser is broken. Now items count {len(items)}")
34
+
35
+ forum = NNMClubForumLink(
36
+ id=get_forum_id(forum_td),
37
+ name=str(forum_td.text).strip("\n")
38
+ )
39
+ author = NNMClubAuthorLink(
40
+ id=get_user_id(author_td),
41
+ name=str(author_td.text).strip("\n")
42
+ )
43
+ return NNMClubSearchItem(
44
+ id=get_torrent_id(torrent_td),
45
+ topic=NNMClubTopicLink(
46
+ id=get_topic_id(topic_td),
47
+ name=topic_td.a.text,
48
+ ),
49
+ author=author,
50
+ forum=forum,
51
+ size=int(size_td.u.text),
52
+ seed_count=int(seed_td.text),
53
+ leech_count=int(leech_td.text),
54
+ added_at=datetime.fromtimestamp(int(added_at_td.u.text))
55
+ )
@@ -0,0 +1,96 @@
1
+ import os
2
+ from typing import List, Optional
3
+
4
+ from bs4 import BeautifulSoup, Tag
5
+
6
+ from torrent_tracker_search.source.nnmclub.model.NNMClubTorrentFile import NNMClubTorrentFile
7
+
8
+
9
+ class NNMClubTorrentFilesParser:
10
+
11
+ @classmethod
12
+ def parse(cls, html: str) -> Optional[NNMClubTorrentFile]:
13
+ soup = BeautifulSoup(html, "html.parser")
14
+ items: List[NNMClubTorrentFile] = []
15
+ content_table = soup.find("table")
16
+ if content_table is None:
17
+ return None
18
+ for item in content_table.tbody.childGenerator():
19
+ if type(item) is Tag:
20
+ cls.parse_item(item, items)
21
+ root_folder = items[0]
22
+ f = cls.reassemble_to_folders(items[1:])
23
+ root_folder.files = f.files
24
+ cls.calculate_folder_size(root_folder)
25
+ return root_folder
26
+
27
+ @classmethod
28
+ def calculate_folder_size(cls, folder: NNMClubTorrentFile):
29
+ """Calculate the total size of the folder by summing up the sizes of contained files and subfolders."""
30
+ if not folder.is_folder:
31
+ return folder.size
32
+
33
+ total_size = 0
34
+ for file in folder.files:
35
+ if file.is_folder:
36
+ total_size += cls.calculate_folder_size(file)
37
+ else:
38
+ total_size += file.size
39
+ folder.size = total_size
40
+ return folder.size
41
+
42
+ @classmethod
43
+ def reassemble_to_folders(cls, file_list):
44
+ def add_file_to_folder(folder, path_parts, size):
45
+ if not path_parts:
46
+ return
47
+
48
+ current_part = path_parts[0]
49
+ remaining_parts = path_parts[1:]
50
+ folder_dict = {file.name: file for file in folder.files}
51
+
52
+ if current_part not in folder_dict:
53
+ # If this part of the path is not in the folder, add it
54
+ if remaining_parts:
55
+ # If there are remaining parts, it's a folder
56
+ new_folder = NNMClubTorrentFile(name=current_part, is_folder=True, files=[], size=0)
57
+ folder.files.append(new_folder)
58
+ add_file_to_folder(new_folder, remaining_parts, size)
59
+ else:
60
+ # If there are no remaining parts, it's a file
61
+ new_file = NNMClubTorrentFile(name=current_part, is_folder=False, size=size, files=[])
62
+ folder.files.append(new_file)
63
+ else:
64
+ # If the folder exists, continue adding the remaining path
65
+ add_file_to_folder(folder_dict[current_part], remaining_parts, size)
66
+
67
+ root_folder = NNMClubTorrentFile(name='root', is_folder=True, files=[], size=0)
68
+
69
+ for file in file_list:
70
+ path_parts = file.name.split(os.sep)
71
+ add_file_to_folder(root_folder, path_parts, file.size)
72
+
73
+ return root_folder
74
+
75
+ @classmethod
76
+ def parse_item(cls, tr_code: Tag, items: list):
77
+ number, name, size_in_bytes, size_in_bites = list(tr_code.childGenerator())
78
+ if number.text.strip() == "Папка":
79
+ items.append(
80
+ NNMClubTorrentFile(
81
+ name=name.text.strip(),
82
+ is_folder=True,
83
+ files=[],
84
+ size=0
85
+ )
86
+ )
87
+ else:
88
+ items.append(
89
+ NNMClubTorrentFile(
90
+ name=name.text.strip(),
91
+ is_folder=False,
92
+ files=[],
93
+ size=int(size_in_bites.text.replace(",", ""))
94
+ )
95
+ )
96
+
@@ -0,0 +1,30 @@
1
+ from typing import Optional
2
+
3
+ from bs4 import BeautifulSoup, Tag
4
+
5
+ from torrent_tracker_search.source.nnmclub.parser import get_torrent_id
6
+
7
+
8
+ class NNMClubTorrentParser:
9
+
10
+ @classmethod
11
+ def parse(cls, html: str) -> Optional[dict]:
12
+ soup = BeautifulSoup(html, "html.parser")
13
+ table = soup.find("th", string="Сообщение").find_parent("table")
14
+ tables = [tag for tag in table.childGenerator() if type(tag) is Tag]
15
+ content_table = [tag for tag in tables[1].childGenerator() if type(tag) is Tag][1].table
16
+ if content_table is not None:
17
+ items = [tag for tag in content_table.childGenerator() if type(tag) is Tag]
18
+ header, _, torrent_page, torrent_details, _ = items
19
+ magnet_link = torrent_details.find("a", attrs={"title": "Примагнититься"})
20
+ torrent_id_a = torrent_details.find("a", string="Скачать")
21
+ torrent_id = get_torrent_id(torrent_id_a.parent)
22
+ return {
23
+ "id": int(torrent_id),
24
+ "post_id": int(content_table["id"].replace("post_", "")),
25
+ "html_code": torrent_page,
26
+ "magnet_link": magnet_link["href"]
27
+ }
28
+ else:
29
+ return None
30
+
@@ -0,0 +1,25 @@
1
+ from urllib.parse import urlparse
2
+
3
+ from bs4 import Tag
4
+
5
+
6
+ def get_query_param(source: Tag, param_key: str) -> str:
7
+ source_href = source.a["href"]
8
+ query = urlparse(source_href).query.split("&")
9
+ for item in query:
10
+ if item.startswith(f"{param_key}="):
11
+ query_name, query_value = item.split("=")
12
+ return query_value
13
+ raise Exception(f"Error on get quert param from {source}")
14
+
15
+ def get_forum_id(forum_td: Tag) -> int:
16
+ return int(get_query_param(forum_td,"f"))
17
+
18
+ def get_user_id(author_td: Tag) -> int:
19
+ return int(get_query_param(author_td, "pid"))
20
+
21
+ def get_torrent_id(torrent_td: Tag) -> int:
22
+ return int(get_query_param(torrent_td, "id"))
23
+
24
+ def get_topic_id(topic_td: Tag) -> int:
25
+ return int(get_query_param(topic_td, "t"))
@@ -0,0 +1,47 @@
1
+ from typing import Final, Optional
2
+
3
+ from torrent_tracker_search.common.TrackerClient import TrackerClient
4
+ from torrent_tracker_search.common.TrackerRequestException import TrackerRequestException
5
+ from torrent_tracker_search.common.TrackerResponse import TrackerResponse
6
+
7
+
8
+ class RuTrackerClient(TrackerClient):
9
+ BASE_URL: Final[str] = "https://rutracker.org/forum"
10
+ SESSION_COOKIE_KEY: Final[str] = "bb_session"
11
+
12
+ async def get(self, url: str, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse:
13
+ async with self.session.get(url=url, params=params, ssl=False, allow_redirects=allow_redirects) as response:
14
+ success_status_code = 200 if allow_redirects else 302
15
+ if response.status != success_status_code:
16
+ raise TrackerRequestException()
17
+ else:
18
+ return TrackerResponse(
19
+ headers=response.headers.copy(),
20
+ status_code=response.status,
21
+ body=await response.content.read()
22
+ )
23
+
24
+ async def post_form_data_encoded(self, url: str, body: dict, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse:
25
+ async with self.session.post(url=url, data=body, ssl=False, allow_redirects=allow_redirects, params=params) as response:
26
+ success_status_code = 200 if allow_redirects else 302
27
+ if response.status == success_status_code:
28
+ return TrackerResponse(
29
+ headers=response.headers.copy(),
30
+ status_code=response.status,
31
+ body=await response.content.read()
32
+ )
33
+ else:
34
+ raise TrackerRequestException()
35
+
36
+ async def post_form_data(self, url: str, body: dict, params: Optional[dict] = None, allow_redirects: bool = True) -> TrackerResponse:
37
+ async with self.session.post(url=url, data=body, ssl=False, allow_redirects=allow_redirects, params=params) as response:
38
+ success_status_code = 200 if allow_redirects else 302
39
+ if response.status == success_status_code:
40
+ return TrackerResponse(
41
+ headers=response.headers.copy(),
42
+ status_code=response.status,
43
+ body=await response.content.read()
44
+ )
45
+ else:
46
+ raise TrackerRequestException()
47
+
@@ -0,0 +1,98 @@
1
+ import base64
2
+ import json
3
+ from typing import Optional, List
4
+
5
+ from torrent_tracker_search.common.TrackerClient import TrackerClient
6
+ from torrent_tracker_search.source.rutracker.RuTrackerClient import RuTrackerClient
7
+ from torrent_tracker_search.source.rutracker.model.RuTrackerSearchItem import RuTrackerSearchItem
8
+ from torrent_tracker_search.source.rutracker.model.RuTrackerTorrentFile import RuTrackerTorrentFile
9
+ from torrent_tracker_search.source.rutracker.model.RuTrackerTorrentItem import RuTrackerTorrentItem
10
+ from torrent_tracker_search.source.rutracker.parser.RuTrackerSearchParser import RuTrackerSearchParser
11
+ from torrent_tracker_search.source.rutracker.RuTrackerURL import RuTrackerURL
12
+ from torrent_tracker_search.source.rutracker.parser.RuTrackerTorrentFilesParser import RuTrackerTorrentFilesParser
13
+ from torrent_tracker_search.source.rutracker.parser.RuTrackerTorrentParser import RuTrackerTorrentParser
14
+
15
+
16
+ class RuTrackerService:
17
+
18
+ def __init__(self, client: TrackerClient):
19
+ self._client: TrackerClient = client
20
+
21
+ async def search(self, query: str) -> List[RuTrackerSearchItem]:
22
+ response = await self._client.get(
23
+ url=RuTrackerURL.TRACKER.url,
24
+ params={
25
+ "nm": query
26
+ }
27
+ )
28
+ body = response.body.decode("Windows-1251")
29
+ return RuTrackerSearchParser.items(body)
30
+
31
+ async def torrent(self, torrent_id: int) -> Optional[RuTrackerTorrentItem]:
32
+ response = await self._client.get(
33
+ url=RuTrackerURL.VIEW_TOPIC.url,
34
+ params={
35
+ "t": torrent_id
36
+ }
37
+ )
38
+ body = response.body.decode("Windows-1251")
39
+ torrent_dict = RuTrackerTorrentParser.parse(body)
40
+ if torrent_dict is not None:
41
+ form_token = RuTrackerTorrentParser.form_token(body)
42
+ bb_code = await self.torrent_bb_code(post_id=torrent_dict["post_id"], form_token=form_token)
43
+ if bb_code is not None:
44
+ bb_code = base64.standard_b64encode(bb_code.encode()).decode()
45
+ return RuTrackerTorrentItem(
46
+ id=torrent_dict["torrent_id"],
47
+ post_id=torrent_dict["post_id"],
48
+ html_code=base64.standard_b64encode(torrent_dict["html_code"]).decode(),
49
+ magnet_link=torrent_dict["magnet_link"],
50
+ bb_code=bb_code,
51
+ files=await self.torrent_files(torrent_id=torrent_id)
52
+ )
53
+
54
+ async def torrent_files(self, torrent_id: int) -> List[RuTrackerTorrentFile]:
55
+ response = await self._client.post_form_data(
56
+ url=RuTrackerURL.VIEW_TORRENT.url,
57
+ body={
58
+ "t": torrent_id
59
+ }
60
+ )
61
+ body = response.body.decode("Windows-1251")
62
+ return RuTrackerTorrentFilesParser.items(body)
63
+
64
+ async def torrent_bb_code(self, post_id: int, form_token: str) -> Optional[str]:
65
+ response = await self._client.post_form_data(
66
+ url=RuTrackerURL.AJAX.url,
67
+ body={
68
+ "action": "view_post",
69
+ "post_id": post_id,
70
+ "mode": "text",
71
+ "form_token": form_token
72
+ }
73
+ )
74
+ body = response.body.decode()
75
+ body_json: dict = json.loads(body)
76
+ return body_json["post_text"]
77
+
78
+ async def login(self, login: str, password: str) -> Optional[str]:
79
+ response = await self._client.post_form_data_encoded(
80
+ url=RuTrackerURL.LOGIN.url,
81
+ body={
82
+ "login_username": login,
83
+ "login_password": password,
84
+ "login": ""
85
+ },
86
+ allow_redirects=False
87
+ )
88
+ new_cookies = response.headers.getall("Set-Cookie")
89
+ for new_cookie in new_cookies:
90
+ if new_cookie.startswith(RuTrackerClient.SESSION_COOKIE_KEY):
91
+ cookie_string = new_cookie.split(";")[0]
92
+ cookie_name, cookie_value = cookie_string.split("=")
93
+ self._client.session.cookie_jar.update_cookies({RuTrackerClient.SESSION_COOKIE_KEY: cookie_value})
94
+ return cookie_value
95
+ return None
96
+
97
+
98
+
@@ -0,0 +1,16 @@
1
+ from enum import Enum
2
+
3
+ from torrent_tracker_search.source.rutracker.RuTrackerClient import RuTrackerClient
4
+
5
+
6
+ class RuTrackerURL(Enum):
7
+ MAIN = "index.php"
8
+ LOGIN = "login.php"
9
+ TRACKER = "tracker.php"
10
+ VIEW_TOPIC = "viewtopic.php"
11
+ VIEW_TORRENT = "viewtorrent.php"
12
+ AJAX = "ajax.php"
13
+
14
+ @property
15
+ def url(self) -> str:
16
+ return f"{RuTrackerClient.BASE_URL}/{self.value}"
@@ -0,0 +1,7 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class RuTrackerAuthorLink:
6
+ id: int
7
+ name: str
@@ -0,0 +1,7 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class RuTrackerForumLink:
6
+ id: int
7
+ name: str
@@ -0,0 +1,21 @@
1
+ from dataclasses import dataclass
2
+ from datetime import datetime
3
+ from typing import List
4
+
5
+ from torrent_tracker_search.source.rutracker.model.RuTrackerAuthorLink import RuTrackerAuthorLink
6
+ from torrent_tracker_search.source.rutracker.model.RuTrackerForumLink import RuTrackerForumLink
7
+
8
+
9
+ @dataclass
10
+ class RuTrackerSearchItem:
11
+ id: int # id for download torrent file
12
+ is_verified: bool
13
+ name: str
14
+ author: RuTrackerAuthorLink
15
+ forum: RuTrackerForumLink
16
+ tags: List[str]
17
+ size: int
18
+ seed_count: int
19
+ leech_count: int
20
+ download_count: int
21
+ added_at: datetime
@@ -0,0 +1,9 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class RuTrackerTorrentFile:
6
+ name: str
7
+ is_folder: bool
8
+ files: list
9
+ size: int
@@ -0,0 +1,14 @@
1
+ from dataclasses import dataclass
2
+ from typing import Optional, List
3
+
4
+ from torrent_tracker_search.source.rutracker.model.RuTrackerTorrentFile import RuTrackerTorrentFile
5
+
6
+
7
+ @dataclass
8
+ class RuTrackerTorrentItem:
9
+ id: int
10
+ post_id: int
11
+ html_code: str
12
+ magnet_link: str
13
+ bb_code: Optional[str] = None
14
+ files: Optional[List[RuTrackerTorrentFile]] = None
@@ -0,0 +1,54 @@
1
+ from datetime import datetime
2
+ from typing import List
3
+
4
+ from bs4 import BeautifulSoup, Tag
5
+
6
+ from torrent_tracker_search.source.rutracker.model.RuTrackerAuthorLink import RuTrackerAuthorLink
7
+ from torrent_tracker_search.source.rutracker.model.RuTrackerForumLink import RuTrackerForumLink
8
+ from torrent_tracker_search.source.rutracker.model.RuTrackerSearchItem import RuTrackerSearchItem
9
+ from torrent_tracker_search.source.rutracker.parser import get_forum_id, get_user_id
10
+
11
+
12
+ class RuTrackerSearchParser:
13
+
14
+ @staticmethod
15
+ def items(html: str) -> List[RuTrackerSearchItem]:
16
+ soup = BeautifulSoup(html, "html.parser")
17
+ content_table = soup.find("table", attrs={"id": "tor-tbl"})
18
+ table_body = content_table.tbody
19
+ result = []
20
+ for item in table_body.childGenerator():
21
+ if type(item) is Tag:
22
+ result.append(RuTrackerSearchParser.item(item))
23
+ return result
24
+
25
+ @staticmethod
26
+ def item(item: Tag) -> RuTrackerSearchItem:
27
+ items = [td for td in item.find_all("td")]
28
+ if len(items) == 10:
29
+ id_td, is_verified_td, forum_td, torrent_td, author_td, size_td, seed_td, leech_td, download_td, added_at_td = items
30
+ forum = RuTrackerForumLink(
31
+ id=get_forum_id(forum_td),
32
+ name=str(forum_td.text).strip("\n")
33
+ )
34
+ author = RuTrackerAuthorLink(
35
+ id=get_user_id(author_td),
36
+ name=str(author_td.text).strip("\n")
37
+ )
38
+ torrent_div, tags_div = torrent_td.find_all("div")
39
+ return RuTrackerSearchItem(
40
+ id=int(id_td["id"]),
41
+ is_verified=is_verified_td.text == "√",
42
+ name=str(torrent_div.text).strip("\n"),
43
+ tags=[tag_span.text for tag_span in tags_div.childGenerator()],
44
+ forum=forum,
45
+ author=author,
46
+ size=int(size_td["data-ts_text"]),
47
+ seed_count=int(seed_td.text),
48
+ leech_count=int(leech_td.text),
49
+ download_count=int(download_td.text),
50
+ added_at=datetime.fromtimestamp(int(added_at_td["data-ts_text"]))
51
+ )
52
+ else:
53
+ raise Exception(f"Allowed only 10 items. Parser is broken. Now items count {len(items)}")
54
+
@@ -0,0 +1,57 @@
1
+ from typing import List
2
+
3
+ from bs4 import BeautifulSoup
4
+
5
+ from torrent_tracker_search.source.rutracker.model.RuTrackerTorrentFile import RuTrackerTorrentFile
6
+
7
+
8
+ class RuTrackerTorrentFilesParser:
9
+
10
+ @staticmethod
11
+ def items(html: str) -> List[RuTrackerTorrentFile]:
12
+ soup = BeautifulSoup(html, "html.parser")
13
+ return RuTrackerTorrentFilesParser.parse_tree(soup.ul)
14
+
15
+ @staticmethod
16
+ def parse_tree(element):
17
+ # Здесь мы собираем информацию обо всех элементах
18
+ data = []
19
+ if element.name == 'li':
20
+ # sub_data = {}
21
+ div = element.find('div')
22
+ if div:
23
+ # Получаем название
24
+ name = div.find('b').text
25
+ # sub_data['name'] = name
26
+ # Получаем возможный идентификатор
27
+ size = div.find('i')
28
+ if size:
29
+ size = int(size.text)
30
+ else:
31
+ size = 0
32
+
33
+ data.append(RuTrackerTorrentFile(
34
+ name=name,
35
+ size=size,
36
+ files=[],
37
+ is_folder=False
38
+ ))
39
+
40
+ # Обработка вложенного списка
41
+ nested_ul = element.find('ul')
42
+ if nested_ul:
43
+ nested_data = RuTrackerTorrentFilesParser.parse_tree(nested_ul)
44
+ if nested_data:
45
+ new_size = data[-1].size
46
+ for file in nested_data:
47
+ new_size += file.size
48
+ data[-1].is_folder = True
49
+ data[-1].size = new_size
50
+ data[-1].files = nested_data # Добавляем дочерние элементы
51
+
52
+ elif element.name == 'ul':
53
+ for li in element.find_all('li', recursive=False):
54
+ data.extend(RuTrackerTorrentFilesParser.parse_tree(li))
55
+
56
+ return data
57
+
@@ -0,0 +1,32 @@
1
+ import json
2
+ from typing import Optional
3
+
4
+ from bs4 import BeautifulSoup
5
+
6
+
7
+ class RuTrackerTorrentParser:
8
+
9
+ @staticmethod
10
+ def form_token(html: str) -> str:
11
+ form_token_start_index = html.index("form_token")
12
+ form_token_end_index = html[form_token_start_index:].index(",")
13
+ raw_form_token = html[form_token_start_index:form_token_start_index+form_token_end_index]
14
+ form_token = raw_form_token.strip().replace("form_token: ", "").replace("'", "")
15
+ return form_token
16
+
17
+ @staticmethod
18
+ def parse(html: str) -> Optional[dict]:
19
+ soup = BeautifulSoup(html, "html.parser")
20
+ torrent_html = soup.find("div", attrs={"class": "post_body"})
21
+ if torrent_html is not None:
22
+ props = json.loads(torrent_html["data-ext_link_data"])
23
+ post_id = int(props["p"])
24
+ torrent_id = int(props["t"])
25
+ magnet_link_a = soup.find("a", attrs={"data-topic_id": torrent_id})
26
+ magnet_link = magnet_link_a["href"]
27
+ return {
28
+ "post_id": post_id,
29
+ "torrent_id": torrent_id,
30
+ "magnet_link": magnet_link,
31
+ "html_code": torrent_html.encode()
32
+ }
@@ -0,0 +1,23 @@
1
+ from urllib.parse import urlparse
2
+
3
+ from bs4 import Tag
4
+
5
+
6
+ def get_forum_id(forum_td: Tag) -> int:
7
+ forum_href = forum_td.div.a["href"]
8
+ query = urlparse(forum_href).query.split("&")
9
+ for item in query:
10
+ if item.startswith("f="):
11
+ query_name, query_value = item.split("=")
12
+ return int(query_value)
13
+ raise Exception(f"Error on get forum id from {forum_href}")
14
+
15
+ def get_user_id(author_td: Tag) -> int:
16
+ author_href = author_td.div.a["href"]
17
+ query = urlparse(author_href).query.split("&")
18
+ for item in query:
19
+ if item.startswith("pid="):
20
+ query_name, query_value = item.split("=")
21
+ return int(query_value)
22
+ raise Exception(f"Error on get author id from {author_td}")
23
+