grabpy 0.0.1__py3-none-any.whl

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.
grabpy/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .scraper import Grabber
2
+
3
+ __all__ = ['Grabber']
grabpy/request.py ADDED
@@ -0,0 +1,27 @@
1
+ import requests
2
+ import sys
3
+
4
+
5
+ class Requester:
6
+ def __init__(self, retries: int):
7
+ """Set retries to -1 to retry indefinitely"""
8
+ self.retries = retries
9
+
10
+ def fetch(self, url: str) -> bytes:
11
+ retries: int = self.retries
12
+
13
+ while retries:
14
+ try:
15
+ response = requests.get(url)
16
+ response.raise_for_status()
17
+ except requests.exceptions.RequestException as err:
18
+ print(err, file=sys.stderr)
19
+
20
+ if err.response.status_code == 404:
21
+ break
22
+
23
+ retries -= 1
24
+ else:
25
+ return response.content
26
+
27
+ return b''
grabpy/robots.py ADDED
@@ -0,0 +1,60 @@
1
+ from urllib.robotparser import RobotFileParser
2
+ import urllib.parse
3
+ import time
4
+ from functools import lru_cache
5
+ import sys
6
+
7
+
8
+ class RobotsParser:
9
+ def __init__(self, useragent: str) -> None:
10
+ self.useragent: str = useragent
11
+
12
+ def __str__(self) -> str:
13
+ return f'RobotsParser-{self.useragent}'
14
+
15
+ @staticmethod
16
+ def _extract_url_base(url: str) -> str:
17
+ result = urllib.parse.urlparse(url)
18
+ netloc = result.netloc
19
+ scheme = result.scheme
20
+
21
+ if not all([netloc, scheme]):
22
+ raise ValueError(f'Invalid url schema: {url}')
23
+
24
+ return f'{scheme}://{netloc}'
25
+
26
+ @lru_cache(maxsize=128, typed=True)
27
+ def _get_parser(self, url: str) -> RobotFileParser:
28
+ base = self._extract_url_base(url)
29
+ url = urllib.parse.urljoin(base, 'robots.txt')
30
+
31
+ rp = RobotFileParser()
32
+ rp.set_url(url)
33
+ rp.read()
34
+
35
+ return rp
36
+
37
+ def _can_scrape(self, parser: RobotFileParser, url: str) -> bool:
38
+ return parser.can_fetch(self.useragent, url)
39
+
40
+ def _scrape_delay(self, parser: RobotFileParser) -> None:
41
+ delay = parser.crawl_delay(self.useragent)
42
+
43
+ if not delay:
44
+ return
45
+
46
+ time.sleep(float(delay))
47
+
48
+ def be_respectful(self, url: str) -> bool:
49
+ try:
50
+ parser = self._get_parser(url)
51
+ except ValueError as err:
52
+ print(err, file=sys.stderr)
53
+ return False
54
+
55
+ if not self._can_scrape(parser, url):
56
+ return False
57
+
58
+ self._scrape_delay(parser)
59
+
60
+ return True
grabpy/scraper.py ADDED
@@ -0,0 +1,22 @@
1
+ from .request import Requester
2
+ from .robots import RobotsParser
3
+ from functools import lru_cache
4
+
5
+
6
+ class Grabber:
7
+ def __init__(self, useragent: str, retries: int = 3) -> None:
8
+ self.robots_parser = RobotsParser(useragent)
9
+ self.requester = Requester(retries)
10
+
11
+ def __enter__(self) -> 'Grabber':
12
+ return self
13
+
14
+ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
15
+ return
16
+
17
+ @lru_cache(maxsize=8, typed=True)
18
+ def get(self, url: str) -> bytes:
19
+ if not self.robots_parser.be_respectful(url):
20
+ return b''
21
+
22
+ return self.requester.fetch(url)
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.4
2
+ Name: grabpy
3
+ Version: 0.0.1
4
+ Summary: Simple package for requesting web pages that respects robots.txt
5
+ Author-email: Riain Ó Tuathail <rotuathail01@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/belfastkeyboard/grabpy
8
+ Project-URL: Issues, https://github.com/belfastkeyboard/grabpy/issues
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENCE
14
+ Requires-Dist: requests==2.32.5
15
+ Dynamic: license-file
16
+
17
+ # Grabpy
18
+
19
+ Simple respectful web scraper
@@ -0,0 +1,9 @@
1
+ grabpy/__init__.py,sha256=lh27thZnh_FCMi4YDcgyDWtD8zUP9yc3IdD7bR3qrx4,52
2
+ grabpy/request.py,sha256=NyfgXf1JSTKswNqknfZVhVP4iklU3U0FFd2Iy8iPBlk,671
3
+ grabpy/robots.py,sha256=GE08Qi0wZxakpUs0dIlt4Ym_8hcNdaWIW9OworCWcDE,1551
4
+ grabpy/scraper.py,sha256=MTSGG1pTTbSQeQaiclXIguN0eqD7WpQi9h0hqdVtz2I,606
5
+ grabpy-0.0.1.dist-info/licenses/LICENCE,sha256=0ldxwBhIbfESmnAHjCsiGAutVM7VPltW6oC_odQeK_g,1060
6
+ grabpy-0.0.1.dist-info/METADATA,sha256=zsaIpDHccnZGUxU1EAH42Uc3hYUGOkA086xuH5UruiE,615
7
+ grabpy-0.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ grabpy-0.0.1.dist-info/top_level.txt,sha256=Bdj1abPGSUhEP9o64HuSrA1GKMLbAgTEWke7xEv6XYQ,7
9
+ grabpy-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2025 Riain Ó Tuathail
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ grabpy