enthusiast-source-wordpress 0.1.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.
- enthusiast_source_wordpress-0.1.0/PKG-INFO +20 -0
- enthusiast_source_wordpress-0.1.0/README.md +3 -0
- enthusiast_source_wordpress-0.1.0/enthusiast_source_wordpress/__init__.py +1 -0
- enthusiast_source_wordpress-0.1.0/enthusiast_source_wordpress/source.py +61 -0
- enthusiast_source_wordpress-0.1.0/pyproject.toml +16 -0
|
@@ -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 @@
|
|
|
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,16 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "enthusiast-source-wordpress"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = ""
|
|
5
|
+
authors = ["Rafal Cymerys <rafal@upsidelab.io>"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
|
|
8
|
+
[tool.poetry.dependencies]
|
|
9
|
+
python = "^3.9"
|
|
10
|
+
enthusiast-common = "^0.2.0"
|
|
11
|
+
requests = "^2.32.3"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
[build-system]
|
|
15
|
+
requires = ["poetry-core"]
|
|
16
|
+
build-backend = "poetry.core.masonry.api"
|