enthusiast-source-wordpress 0.1.0__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.
@@ -0,0 +1 @@
1
+ from .source import WordpressDocumentSource as WordpressDocumentSource
@@ -0,0 +1,61 @@
1
+ import logging
2
+ from typing import Optional
3
+
4
+ import re
5
+ import requests
6
+ import urllib.parse
7
+ from enthusiast_common import DocumentSourcePlugin, DocumentDetails
8
+ from requests import Response
9
+
10
+ logger = logging.getLogger(__name__)
11
+
12
+ class WordpressDocumentSource(DocumentSourcePlugin):
13
+ def __init__(self, data_set_id, config: dict):
14
+ """
15
+ Initialize the plugin with the parameters to access shop.
16
+
17
+ Args:
18
+ data_set_id (int): identifier of a data set that documents are assigned to.
19
+ config (dict): configuration of the plugin
20
+ """
21
+ super().__init__(data_set_id, config)
22
+
23
+ self._wordpress_url = config.get("base_url")
24
+ self._user_agent = config.get("user_agent", None)
25
+
26
+ def fetch(self) -> list[DocumentDetails]:
27
+ session = self._create_http_session()
28
+ results = []
29
+
30
+ current_url = self._posts_url()
31
+ while current_url:
32
+ logger.info(f"Fetching {current_url}")
33
+ response = session.get(current_url)
34
+ posts = response.json()
35
+ results.extend([
36
+ DocumentDetails(
37
+ url=post['link'],
38
+ title=post['title']['rendered'],
39
+ content=post['content']['rendered']
40
+ )
41
+ for post in posts
42
+ ])
43
+ current_url = self._next_page_link_from_headers(response)
44
+ return results
45
+
46
+ def _next_page_link_from_headers(self, response: Response) -> Optional[str]:
47
+ link_header = response.headers['Link']
48
+
49
+ match = re.search(r'<(\S+?)>; rel="next"', link_header)
50
+ if match:
51
+ return match.group(1)
52
+ return None
53
+
54
+ def _posts_url(self):
55
+ return urllib.parse.urljoin(self._wordpress_url, 'wp-json/wp/v2/posts')
56
+
57
+ def _create_http_session(self):
58
+ session = requests.Session()
59
+ if self._user_agent:
60
+ session.headers.update({'User-Agent': self._user_agent})
61
+ return session
@@ -0,0 +1,20 @@
1
+ Metadata-Version: 2.1
2
+ Name: enthusiast-source-wordpress
3
+ Version: 0.1.0
4
+ Summary:
5
+ Author: Rafal Cymerys
6
+ Author-email: rafal@upsidelab.io
7
+ Requires-Python: >=3.9,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.9
10
+ Classifier: Programming Language :: Python :: 3.10
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Requires-Dist: enthusiast-common (>=0.2.0,<0.3.0)
14
+ Requires-Dist: requests (>=2.32.3,<3.0.0)
15
+ Description-Content-Type: text/markdown
16
+
17
+ # Enthusiast Source Wordpress
18
+
19
+ This Enthusiast plugin enables the import of product data directly from Wordpress.
20
+
@@ -0,0 +1,5 @@
1
+ enthusiast_source_wordpress/__init__.py,sha256=tyaI2dP242NDa1pYuZQ7CrDK1MPD-Z7aJnjrU6Q7cZc,70
2
+ enthusiast_source_wordpress/source.py,sha256=dSlmPChHjqrepAC_95pH4XNloflhZP8Ei0u_ow_SAvI,1991
3
+ enthusiast_source_wordpress-0.1.0.dist-info/METADATA,sha256=m9aasyu9nihBF7H_sxb1cC6WG_4Z-o0R1HsP_2nVLsg,663
4
+ enthusiast_source_wordpress-0.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
5
+ enthusiast_source_wordpress-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 1.9.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any