article 0.1.5__tar.gz → 0.1.7__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.
- {article-0.1.5 → article-0.1.7}/PKG-INFO +1 -1
- {article-0.1.5 → article-0.1.7}/article/__init__.py +7 -0
- {article-0.1.5 → article-0.1.7}/article/adapters/substack.py +73 -2
- article-0.1.7/article/published.py +790 -0
- {article-0.1.5 → article-0.1.7}/pyproject.toml +1 -1
- article-0.1.7/tests/data/substack_post.html +27 -0
- article-0.1.7/tests/test_published.py +231 -0
- {article-0.1.5 → article-0.1.7}/tests/test_substack.py +26 -0
- {article-0.1.5 → article-0.1.7}/tests/test_substack_live.py +12 -1
- {article-0.1.5 → article-0.1.7}/.editorconfig +0 -0
- {article-0.1.5 → article-0.1.7}/.env.example +0 -0
- {article-0.1.5 → article-0.1.7}/.gitattributes +0 -0
- {article-0.1.5 → article-0.1.7}/.github/workflows/ci.yml +0 -0
- {article-0.1.5 → article-0.1.7}/.gitignore +0 -0
- {article-0.1.5 → article-0.1.7}/LICENSE +0 -0
- {article-0.1.5 → article-0.1.7}/README.md +0 -0
- {article-0.1.5 → article-0.1.7}/article/__main__.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/adapters/__init__.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/adapters/dev_to.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/adapters/hashnode.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/adapters/medium.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/base.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/config.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/engine.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/md_source.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/misc/CHANGELOG.md +0 -0
- {article-0.1.5 → article-0.1.7}/article/registry.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/state.py +0 -0
- {article-0.1.5 → article-0.1.7}/article/util.py +0 -0
- {article-0.1.5 → article-0.1.7}/examples/article.example.json +0 -0
- {article-0.1.5 → article-0.1.7}/tests/conftest.py +0 -0
- {article-0.1.5 → article-0.1.7}/tests/test_cli.py +0 -0
- {article-0.1.5 → article-0.1.7}/tests/test_pipeline.py +0 -0
|
@@ -88,6 +88,9 @@ from .engine import (
|
|
|
88
88
|
syndicate_secondary,
|
|
89
89
|
)
|
|
90
90
|
|
|
91
|
+
# Read a published post back into ordered items
|
|
92
|
+
from .published import PublishedPost, fetch_published, parse_published
|
|
93
|
+
|
|
91
94
|
# Importing the adapters package registers all built-in adapters.
|
|
92
95
|
from . import adapters # noqa: F401 (side-effect: populate the registry)
|
|
93
96
|
|
|
@@ -138,6 +141,10 @@ __all__ = [
|
|
|
138
141
|
"build_engine",
|
|
139
142
|
"publish_primary",
|
|
140
143
|
"syndicate_secondary",
|
|
144
|
+
# published posts
|
|
145
|
+
"PublishedPost",
|
|
146
|
+
"fetch_published",
|
|
147
|
+
"parse_published",
|
|
141
148
|
]
|
|
142
149
|
|
|
143
150
|
__version__ = "0.1.0"
|
|
@@ -46,7 +46,9 @@ from __future__ import annotations
|
|
|
46
46
|
|
|
47
47
|
import asyncio
|
|
48
48
|
import hashlib
|
|
49
|
+
import json
|
|
49
50
|
import re
|
|
51
|
+
import struct
|
|
50
52
|
import tempfile
|
|
51
53
|
from collections import Counter
|
|
52
54
|
from pathlib import Path
|
|
@@ -369,6 +371,69 @@ def preflight(
|
|
|
369
371
|
# --------------------------------------------------------------------------- #
|
|
370
372
|
|
|
371
373
|
|
|
374
|
+
def image_size(path: Union[str, Path]) -> Optional[tuple[int, int]]:
|
|
375
|
+
"""``(width, height)`` of a PNG, GIF or JPEG file read from its header, else ``None``.
|
|
376
|
+
|
|
377
|
+
Stdlib only; other formats are measured with Pillow when it is installed.
|
|
378
|
+
"""
|
|
379
|
+
with open(path, "rb") as f:
|
|
380
|
+
head = f.read(26)
|
|
381
|
+
if head[:8] == b"\x89PNG\r\n\x1a\n":
|
|
382
|
+
return struct.unpack(">II", head[16:24])
|
|
383
|
+
if head[:6] in (b"GIF87a", b"GIF89a"):
|
|
384
|
+
return struct.unpack("<HH", head[6:10])
|
|
385
|
+
if head[:2] == b"\xff\xd8":
|
|
386
|
+
f.seek(2)
|
|
387
|
+
while True:
|
|
388
|
+
marker = f.read(4)
|
|
389
|
+
if len(marker) < 4 or marker[0] != 0xFF:
|
|
390
|
+
break
|
|
391
|
+
code, length = marker[1], struct.unpack(">H", marker[2:4])[0]
|
|
392
|
+
if 0xC0 <= code <= 0xCF and code not in (0xC4, 0xC8, 0xCC):
|
|
393
|
+
h, w = struct.unpack(">HH", f.read(5)[1:5])
|
|
394
|
+
return w, h
|
|
395
|
+
f.seek(length - 2, 1)
|
|
396
|
+
try:
|
|
397
|
+
from PIL import Image
|
|
398
|
+
|
|
399
|
+
with Image.open(path) as im:
|
|
400
|
+
return im.size
|
|
401
|
+
except Exception:
|
|
402
|
+
return None
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
class _MeasuringApi:
|
|
406
|
+
"""Wraps a python-substack ``Api``, remembering the pixel size of each upload.
|
|
407
|
+
|
|
408
|
+
python-substack gives every image node a default 1456x819; the sizes
|
|
409
|
+
recorded here are written back by :func:`_apply_image_sizes`.
|
|
410
|
+
"""
|
|
411
|
+
|
|
412
|
+
def __init__(self, api: Any):
|
|
413
|
+
self._api = api
|
|
414
|
+
self.sizes: dict[str, tuple[int, int]] = {}
|
|
415
|
+
|
|
416
|
+
def get_image(self, path):
|
|
417
|
+
uploaded = self._api.get_image(path)
|
|
418
|
+
size = image_size(path)
|
|
419
|
+
if size and isinstance(uploaded, Mapping) and uploaded.get("url"):
|
|
420
|
+
self.sizes[uploaded["url"]] = size
|
|
421
|
+
return uploaded
|
|
422
|
+
|
|
423
|
+
def __getattr__(self, name):
|
|
424
|
+
return getattr(self._api, name)
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def _apply_image_sizes(node: Any, sizes: Mapping[str, tuple[int, int]]) -> None:
|
|
428
|
+
if isinstance(node, dict):
|
|
429
|
+
attrs = node.get("attrs")
|
|
430
|
+
if node.get("type") == "image2" and isinstance(attrs, dict):
|
|
431
|
+
if attrs.get("src") in sizes:
|
|
432
|
+
attrs["width"], attrs["height"] = sizes[attrs["src"]]
|
|
433
|
+
for child in node.get("content") or []:
|
|
434
|
+
_apply_image_sizes(child, sizes)
|
|
435
|
+
|
|
436
|
+
|
|
372
437
|
def draft_document(
|
|
373
438
|
article: Article,
|
|
374
439
|
markdown: str,
|
|
@@ -392,8 +457,14 @@ def draft_document(
|
|
|
392
457
|
user_id=user_id,
|
|
393
458
|
audience=config.audience,
|
|
394
459
|
)
|
|
395
|
-
|
|
396
|
-
|
|
460
|
+
measuring = _MeasuringApi(api) if api is not None else None
|
|
461
|
+
post.from_markdown(markdown, api=measuring)
|
|
462
|
+
draft = post.get_draft()
|
|
463
|
+
if measuring and measuring.sizes:
|
|
464
|
+
body = json.loads(draft["draft_body"])
|
|
465
|
+
_apply_image_sizes(body, measuring.sizes)
|
|
466
|
+
draft["draft_body"] = json.dumps(body)
|
|
467
|
+
return draft
|
|
397
468
|
|
|
398
469
|
|
|
399
470
|
def make_substack_api(
|