enthusiast-source-sanitycms 0.1.1__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_sanitycms-0.1.1/LICENCE.md +7 -0
- enthusiast_source_sanitycms-0.1.1/PKG-INFO +40 -0
- enthusiast_source_sanitycms-0.1.1/README.md +23 -0
- enthusiast_source_sanitycms-0.1.1/enthusiast_source_sanitycms/__init__.py +1 -0
- enthusiast_source_sanitycms-0.1.1/enthusiast_source_sanitycms/source.py +87 -0
- enthusiast_source_sanitycms-0.1.1/pyproject.toml +16 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2025 Upside Lab sp. z o.o.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: enthusiast-source-sanitycms
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: This Enthusiast plugin enables the import of document data from Sanity CMS.
|
|
5
|
+
Author: Kuba Szczęśniak
|
|
6
|
+
Author-email: kuba.szczesniak@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,<1)
|
|
14
|
+
Requires-Dist: requests (>=2.32.3,<3.0.0)
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# Enthusiast Source Sanity CMS
|
|
18
|
+
|
|
19
|
+
This Enthusiast plugin enables the import of document data from Sanity CMS.
|
|
20
|
+
|
|
21
|
+
# Usage
|
|
22
|
+
Assumption: schema type looks like this:
|
|
23
|
+
```
|
|
24
|
+
export default {
|
|
25
|
+
name: "blogPost",
|
|
26
|
+
type: "document",
|
|
27
|
+
title: "Blog Post",
|
|
28
|
+
fields: [
|
|
29
|
+
{ name: "title", type: "string", title: "Title" },
|
|
30
|
+
{ name: "content", type: "array", title: "Content", of: [{ type: "block" }] },
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
Fetch all blog posts using below commands:
|
|
35
|
+
```
|
|
36
|
+
from source import SanityCMSDocumentSource
|
|
37
|
+
s = SanityCMSDocumentSource(1, {'project_id': 'your-project-id-here', 'dataset': 'production', 'schema_type': 'blogPost', 'title_field_name': 'title', 'content_field_name': 'content'})
|
|
38
|
+
s.fetch()
|
|
39
|
+
```
|
|
40
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Enthusiast Source Sanity CMS
|
|
2
|
+
|
|
3
|
+
This Enthusiast plugin enables the import of document data from Sanity CMS.
|
|
4
|
+
|
|
5
|
+
# Usage
|
|
6
|
+
Assumption: schema type looks like this:
|
|
7
|
+
```
|
|
8
|
+
export default {
|
|
9
|
+
name: "blogPost",
|
|
10
|
+
type: "document",
|
|
11
|
+
title: "Blog Post",
|
|
12
|
+
fields: [
|
|
13
|
+
{ name: "title", type: "string", title: "Title" },
|
|
14
|
+
{ name: "content", type: "array", title: "Content", of: [{ type: "block" }] },
|
|
15
|
+
],
|
|
16
|
+
};
|
|
17
|
+
```
|
|
18
|
+
Fetch all blog posts using below commands:
|
|
19
|
+
```
|
|
20
|
+
from source import SanityCMSDocumentSource
|
|
21
|
+
s = SanityCMSDocumentSource(1, {'project_id': 'your-project-id-here', 'dataset': 'production', 'schema_type': 'blogPost', 'title_field_name': 'title', 'content_field_name': 'content'})
|
|
22
|
+
s.fetch()
|
|
23
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .source import SanityCMSDocumentSource as SanityCMSDocumentSource
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
|
|
3
|
+
from enthusiast_common import DocumentSourcePlugin, DocumentDetails
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class SanityCMSDocumentSource(DocumentSourcePlugin):
|
|
7
|
+
def __init__(self, data_set_id, config: dict):
|
|
8
|
+
"""
|
|
9
|
+
Initialize the plugin with the parameters to access a source system.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
data_set_id (int): identifier of a data set that documents are assigned to.
|
|
13
|
+
config (dict): configuration of the plugin
|
|
14
|
+
"""
|
|
15
|
+
super().__init__(data_set_id, config)
|
|
16
|
+
|
|
17
|
+
# Sanity CMS related parameters.
|
|
18
|
+
self._project_id = config.get('project_id')
|
|
19
|
+
self._dataset = config.get('dataset')
|
|
20
|
+
self._schema_type = config.get('schema_type')
|
|
21
|
+
self._title_field_name = config.get('title_field_name')
|
|
22
|
+
self._content_field_name = config.get('content_field_name')
|
|
23
|
+
self._base_url = f'https://{self._project_id}.api.sanity.io/v1/data/query/{self._dataset}'
|
|
24
|
+
self._api_key = config.get('api_key')
|
|
25
|
+
|
|
26
|
+
def fetch(self) -> list[DocumentDetails]:
|
|
27
|
+
results = []
|
|
28
|
+
offset = 0 # Starting point for product list pagination.
|
|
29
|
+
limit = 100 # Page size.
|
|
30
|
+
|
|
31
|
+
headers = {
|
|
32
|
+
"Authorization": f"Bearer {self._api_key}"
|
|
33
|
+
} if self._api_key else {}
|
|
34
|
+
|
|
35
|
+
while True:
|
|
36
|
+
query = (
|
|
37
|
+
f'*[_type == "{self._schema_type}"] | order(_createdAt asc) '
|
|
38
|
+
f'[{offset}...{offset + limit}] {{'
|
|
39
|
+
f' "content": {self._content_field_name},'
|
|
40
|
+
f' "title": {self._title_field_name},'
|
|
41
|
+
f' "url": _type + "/" + _id'
|
|
42
|
+
f' }}'
|
|
43
|
+
)
|
|
44
|
+
response = requests.get(self._base_url, headers=headers, params={'query': query})
|
|
45
|
+
response.raise_for_status()
|
|
46
|
+
data = response.json()
|
|
47
|
+
|
|
48
|
+
sanity_posts = data.get("result", [])
|
|
49
|
+
|
|
50
|
+
# Loop through each blog post in the response
|
|
51
|
+
for sanity_post in sanity_posts:
|
|
52
|
+
document = self._get_document(sanity_post)
|
|
53
|
+
results.append(document)
|
|
54
|
+
|
|
55
|
+
if len(sanity_posts) < limit:
|
|
56
|
+
break
|
|
57
|
+
|
|
58
|
+
offset += limit
|
|
59
|
+
|
|
60
|
+
return results
|
|
61
|
+
|
|
62
|
+
def _get_document(self, sanity_post: dict) -> DocumentDetails:
|
|
63
|
+
title = sanity_post.get(f'{self._title_field_name}')
|
|
64
|
+
content_blocks = sanity_post.get(f'{self._content_field_name}', [])
|
|
65
|
+
content = self._extract_content(content_blocks)
|
|
66
|
+
url = sanity_post.get('url')
|
|
67
|
+
document = DocumentDetails(
|
|
68
|
+
url=url,
|
|
69
|
+
title=title,
|
|
70
|
+
content=content
|
|
71
|
+
)
|
|
72
|
+
return document
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@staticmethod
|
|
76
|
+
def _extract_content(content_blocks):
|
|
77
|
+
"""
|
|
78
|
+
Converts the Sanity content array into a single string.
|
|
79
|
+
"""
|
|
80
|
+
content = []
|
|
81
|
+
|
|
82
|
+
for block in content_blocks:
|
|
83
|
+
if block.get("_type") == "block" and "children" in block:
|
|
84
|
+
text = "".join(child.get("text", "") for child in block["children"])
|
|
85
|
+
content.append(text)
|
|
86
|
+
|
|
87
|
+
return "\n\n".join(content)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "enthusiast-source-sanitycms"
|
|
3
|
+
version = "0.1.1"
|
|
4
|
+
description = "This Enthusiast plugin enables the import of document data from Sanity CMS."
|
|
5
|
+
authors = ["Kuba Szczęśniak <kuba.szczesniak@upsidelab.io>"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
|
|
8
|
+
[tool.poetry.dependencies]
|
|
9
|
+
python = "^3.9"
|
|
10
|
+
enthusiast-common = ">=0.2,<1"
|
|
11
|
+
requests = "^2.32.3"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
[build-system]
|
|
15
|
+
requires = ["poetry-core"]
|
|
16
|
+
build-backend = "poetry.core.masonry.api"
|