ladon-hackernews 0.0.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.
- ladon_hackernews-0.0.1/LICENSE +179 -0
- ladon_hackernews-0.0.1/PKG-INFO +282 -0
- ladon_hackernews-0.0.1/README.md +247 -0
- ladon_hackernews-0.0.1/pyproject.toml +133 -0
- ladon_hackernews-0.0.1/setup.cfg +4 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/__init__.py +20 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/__main__.py +5 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/_api.py +15 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/cli.py +302 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/expander.py +67 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/plugin.py +48 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/records.py +67 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/repository.py +241 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/sink.py +56 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews/source.py +37 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews.egg-info/PKG-INFO +282 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews.egg-info/SOURCES.txt +26 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews.egg-info/dependency_links.txt +1 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews.egg-info/entry_points.txt +2 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews.egg-info/requires.txt +8 -0
- ladon_hackernews-0.0.1/src/ladon_hackernews.egg-info/top_level.txt +1 -0
- ladon_hackernews-0.0.1/tests/test_cli.py +564 -0
- ladon_hackernews-0.0.1/tests/test_expander.py +105 -0
- ladon_hackernews-0.0.1/tests/test_plugin.py +36 -0
- ladon_hackernews-0.0.1/tests/test_records.py +64 -0
- ladon_hackernews-0.0.1/tests/test_repository.py +165 -0
- ladon_hackernews-0.0.1/tests/test_sink.py +98 -0
- ladon_hackernews-0.0.1/tests/test_source.py +61 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other
|
|
42
|
+
transformations represent, as a whole, an original work of authorship.
|
|
43
|
+
For the purposes of this License, Derivative Works shall not include
|
|
44
|
+
works that remain separable from, or merely link (or bind by name)
|
|
45
|
+
to the interfaces of, the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any
|
|
81
|
+
Contribution embodied within the Work constitutes direct or
|
|
82
|
+
contributory patent infringement, then any patent licenses granted to
|
|
83
|
+
You under this License for that Work shall terminate as of the date
|
|
84
|
+
such litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
92
|
+
Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Work
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file, You must include a
|
|
104
|
+
readable copy of the attribution notices contained within such
|
|
105
|
+
NOTICE file, in at least one of the following places: within a
|
|
106
|
+
NOTICE text file distributed as part of the Derivative Works;
|
|
107
|
+
within the Source form or documentation, if provided along with
|
|
108
|
+
the Derivative Works; or, within a display generated by the
|
|
109
|
+
Derivative Works, if and wherever such third-party notices
|
|
110
|
+
normally appear. The contents of the NOTICE file are for
|
|
111
|
+
informational purposes only and do not modify the License. You
|
|
112
|
+
may add Your own attribution notices within Derivative Works
|
|
113
|
+
that You distribute, alongside or as an addendum to the NOTICE
|
|
114
|
+
text from the Work, provided that such additional attribution
|
|
115
|
+
notices cannot be construed as modifying the License.
|
|
116
|
+
|
|
117
|
+
You may add Your own license statement for Your modifications and
|
|
118
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
119
|
+
publish, distribute, sublicense, and/or sell copies of the
|
|
120
|
+
Derivative Works, as separate terms and conditions for use,
|
|
121
|
+
reproduction, or distribution of Your modifications, or for such
|
|
122
|
+
Derivative Works as a whole, subject to the terms and conditions
|
|
123
|
+
of this License.
|
|
124
|
+
|
|
125
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
126
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
127
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
128
|
+
this License, without any additional terms or conditions.
|
|
129
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
130
|
+
the terms of any separate license agreement you may have executed
|
|
131
|
+
with Licensor regarding such Contributions.
|
|
132
|
+
|
|
133
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
134
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
135
|
+
except as required for reasonable and customary use in describing the
|
|
136
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
137
|
+
|
|
138
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
139
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
140
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
141
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
142
|
+
implied, including, without limitation, any warranties or conditions
|
|
143
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
144
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
145
|
+
appropriateness of using or reproducing the Work and assume any
|
|
146
|
+
risks associated with Your exercise of permissions under this License.
|
|
147
|
+
|
|
148
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
149
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
150
|
+
unless required by applicable law (such as deliberate and grossly
|
|
151
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
152
|
+
liable to You for damages, including any direct, indirect, special,
|
|
153
|
+
incidental, or exemplary damages of any character arising as a
|
|
154
|
+
result of this License or out of the use or inability to use the
|
|
155
|
+
Work (even if such Contributor has been advised of the possibility
|
|
156
|
+
of such damages).
|
|
157
|
+
|
|
158
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
159
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
160
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
161
|
+
or other liability obligations and/or rights consistent with this
|
|
162
|
+
License. However, in accepting such obligations, You may offer only
|
|
163
|
+
conditions consistent with the terms of this License.
|
|
164
|
+
|
|
165
|
+
END OF TERMS AND CONDITIONS
|
|
166
|
+
|
|
167
|
+
Copyright 2026 MoonyFringers
|
|
168
|
+
|
|
169
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
170
|
+
you may not use this file except in compliance with the License.
|
|
171
|
+
You may obtain a copy of the License at
|
|
172
|
+
|
|
173
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
174
|
+
|
|
175
|
+
Unless required by applicable law or agreed to in writing, software
|
|
176
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
177
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
178
|
+
See the License for the specific language governing permissions and
|
|
179
|
+
limitations under the License.
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ladon-hackernews
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Hacker News adapter for the Ladon crawler framework.
|
|
5
|
+
Author-email: Alessio Pascucci <feeder81@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/MoonyFringers/ladon-hackernews
|
|
8
|
+
Project-URL: Repository, https://github.com/MoonyFringers/ladon-hackernews
|
|
9
|
+
Project-URL: Framework, https://github.com/MoonyFringers/ladon
|
|
10
|
+
Project-URL: Issues, https://github.com/MoonyFringers/ladon-hackernews/issues
|
|
11
|
+
Project-URL: Documentation, https://moonyfringers.github.io/ladon
|
|
12
|
+
Keywords: web-crawler,crawler,hackernews,hacker-news,ladon,ladon-adapter,duckdb,data-pipeline
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.11
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: ladon-crawl>=0.0.1
|
|
28
|
+
Requires-Dist: duckdb>=1.0.0
|
|
29
|
+
Requires-Dist: pytz>=2023.3
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
32
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
33
|
+
Requires-Dist: pre-commit; extra == "dev"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# ladon-hackernews
|
|
37
|
+
|
|
38
|
+
[](https://github.com/MoonyFringers/ladon-hackernews/actions/workflows/test.yml)
|
|
39
|
+
[](LICENSE)
|
|
40
|
+
[](https://www.python.org/downloads/)
|
|
41
|
+
|
|
42
|
+
Hacker News adapter for the [Ladon](https://github.com/MoonyFringers/ladon) crawler framework.
|
|
43
|
+
|
|
44
|
+
Crawls the HN top-stories list, expands each story into its direct comments,
|
|
45
|
+
and persists everything to a DuckDB database — ready to export as Parquet for
|
|
46
|
+
LLM training pipelines or downstream analysis.
|
|
47
|
+
|
|
48
|
+
## Quick start
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install ladon-hackernews
|
|
52
|
+
ladon-hackernews --top 30 --out hn.db
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
No authentication. No external server.
|
|
56
|
+
|
|
57
|
+
## What you get
|
|
58
|
+
|
|
59
|
+
Each run writes two tables to `hn.db`:
|
|
60
|
+
|
|
61
|
+
**`hn_comments`** — one row per HN comment (the leaf record):
|
|
62
|
+
|
|
63
|
+
| column | type | description |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| `id` | INTEGER | HN comment ID |
|
|
66
|
+
| `story_id` | INTEGER | Parent story ID |
|
|
67
|
+
| `parent_id` | INTEGER | Immediate parent (story or comment) |
|
|
68
|
+
| `by` | TEXT | Author username |
|
|
69
|
+
| `text` | TEXT | Raw HTML comment body |
|
|
70
|
+
| `time` | TIMESTAMPTZ | Comment timestamp (UTC) |
|
|
71
|
+
| `run_id` | TEXT | UUID of the crawl run that wrote this row |
|
|
72
|
+
|
|
73
|
+
**`ladon_runs`** — one row per story crawled (upserted twice: at start and finish):
|
|
74
|
+
|
|
75
|
+
| column | type | description |
|
|
76
|
+
|---|---|---|
|
|
77
|
+
| `run_id` | TEXT | UUID for this crawl run |
|
|
78
|
+
| `plugin_name` | TEXT | Always `"hackernews"` |
|
|
79
|
+
| `top_ref` | TEXT | HN item URL that was the root of this run |
|
|
80
|
+
| `started_at` | TIMESTAMPTZ | When the run started (UTC) |
|
|
81
|
+
| `finished_at` | TIMESTAMPTZ | When the run finished; NULL while running |
|
|
82
|
+
| `status` | TEXT | `done`, `partial`, `not_ready`, `failed`, or `running` |
|
|
83
|
+
| `leaves_consumed` | INTEGER | Comments for which `sink.consume()` succeeded |
|
|
84
|
+
| `leaves_persisted` | INTEGER | Comments successfully written to `hn_comments` |
|
|
85
|
+
| `leaves_failed` | INTEGER | Comments that failed to fetch or persist |
|
|
86
|
+
| `branch_errors` | INTEGER | Expander-level errors (branch could not be expanded) |
|
|
87
|
+
| `errors` | TEXT | JSON array of error message strings |
|
|
88
|
+
|
|
89
|
+
### Sample DuckDB query
|
|
90
|
+
|
|
91
|
+
```sql
|
|
92
|
+
-- Top commenters across all crawled stories
|
|
93
|
+
SELECT "by", COUNT(*) AS comments
|
|
94
|
+
FROM hn_comments
|
|
95
|
+
GROUP BY "by"
|
|
96
|
+
ORDER BY comments DESC
|
|
97
|
+
LIMIT 10;
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
HN comments are structured, human-authored, and high signal-to-noise — a useful
|
|
101
|
+
corpus for instruction tuning and dialogue modelling. A typical pipeline looks like:
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
ladon-hackernews --top 500 --out hn.db
|
|
105
|
+
→ export_parquet("hn.db", "hn.parquet")
|
|
106
|
+
→ training pipeline
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Export to Parquet
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from ladon_hackernews import export_parquet
|
|
113
|
+
|
|
114
|
+
export_parquet("hn.db", "hn.parquet")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## CLI reference
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
ladon-hackernews [--top N] [--out PATH] [--verbose]
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
| flag | default | description |
|
|
124
|
+
|---|---|---|
|
|
125
|
+
| `--top N` | `30` | Number of top stories to crawl (range: 1–500) |
|
|
126
|
+
| `--out PATH` | `hn.db` | Output DuckDB database path |
|
|
127
|
+
| `--verbose`, `-v` | off | Show DEBUG-level framework messages (leaf warnings, HTTP timings) |
|
|
128
|
+
|
|
129
|
+
In default mode the terminal shows one progress line per story and a summary at
|
|
130
|
+
the end. Framework-level noise (`leaf unavailable`, `expander branch failed`) is
|
|
131
|
+
suppressed; partial stories print a `↳` hint instead. Pass `--verbose` to expose
|
|
132
|
+
the raw framework log messages.
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Use as a library
|
|
137
|
+
|
|
138
|
+
The CLI is the simplest way to run a crawl, but `HNPlugin` and
|
|
139
|
+
`HNDuckDBRepository` can be used directly as a library — useful when you need
|
|
140
|
+
custom scheduling, batching, or integration into a larger pipeline.
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
import uuid
|
|
144
|
+
from datetime import datetime, timezone
|
|
145
|
+
|
|
146
|
+
from ladon.networking.client import HttpClient
|
|
147
|
+
from ladon.networking.config import HttpClientConfig
|
|
148
|
+
from ladon.persistence import RunAudit, RunRecord
|
|
149
|
+
from ladon.plugins.errors import ExpansionNotReadyError
|
|
150
|
+
from ladon.plugins.models import Ref
|
|
151
|
+
from ladon.runner import RunConfig, run_crawl
|
|
152
|
+
from ladon_hackernews import HNPlugin, HNDuckDBRepository
|
|
153
|
+
|
|
154
|
+
plugin = HNPlugin(top=10)
|
|
155
|
+
config = RunConfig()
|
|
156
|
+
client_config = HttpClientConfig(user_agent="my-bot/1.0")
|
|
157
|
+
|
|
158
|
+
with HNDuckDBRepository("hn.db") as repo, HttpClient(client_config) as client:
|
|
159
|
+
for story_ref in plugin.source.discover(client):
|
|
160
|
+
if not isinstance(story_ref, Ref):
|
|
161
|
+
raise TypeError(f"unexpected type {type(story_ref).__name__}")
|
|
162
|
+
run_id = str(uuid.uuid4())
|
|
163
|
+
run = RunRecord(
|
|
164
|
+
run_id=run_id,
|
|
165
|
+
plugin_name=plugin.name,
|
|
166
|
+
top_ref=story_ref.url,
|
|
167
|
+
started_at=datetime.now(tz=timezone.utc),
|
|
168
|
+
status="running",
|
|
169
|
+
)
|
|
170
|
+
if isinstance(repo, RunAudit):
|
|
171
|
+
repo.record_run(run)
|
|
172
|
+
|
|
173
|
+
try:
|
|
174
|
+
result = run_crawl(
|
|
175
|
+
story_ref, plugin, client, config,
|
|
176
|
+
# Default-argument capture binds run_id to each lambda.
|
|
177
|
+
on_leaf=lambda rec, _, _id=run_id: repo.write_leaf(rec, _id),
|
|
178
|
+
)
|
|
179
|
+
run.branch_errors = sum(
|
|
180
|
+
1 for e in result.errors if e.startswith("expander branch")
|
|
181
|
+
)
|
|
182
|
+
run.status = (
|
|
183
|
+
"partial"
|
|
184
|
+
if result.leaves_failed
|
|
185
|
+
or result.leaves_consumed > result.leaves_persisted
|
|
186
|
+
or run.branch_errors
|
|
187
|
+
else "done"
|
|
188
|
+
)
|
|
189
|
+
run.leaves_consumed = result.leaves_consumed
|
|
190
|
+
run.leaves_persisted = result.leaves_persisted
|
|
191
|
+
run.leaves_failed = result.leaves_failed
|
|
192
|
+
run.errors = result.errors
|
|
193
|
+
except ExpansionNotReadyError:
|
|
194
|
+
run.status = "not_ready"
|
|
195
|
+
except Exception as exc:
|
|
196
|
+
run.status = "failed"
|
|
197
|
+
run.errors = (str(exc),)
|
|
198
|
+
finally:
|
|
199
|
+
run.finished_at = datetime.now(tz=timezone.utc)
|
|
200
|
+
if isinstance(repo, RunAudit):
|
|
201
|
+
repo.record_run(run)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## How it works
|
|
205
|
+
|
|
206
|
+
This adapter implements the Ladon [SES (Source / Expander / Sink)](https://github.com/MoonyFringers/ladon/blob/main/docs/decisions/adr-004-ses-protocol-design.md) protocol against the [HN Firebase API](https://github.com/HackerNews/API).
|
|
207
|
+
|
|
208
|
+
### Pipeline
|
|
209
|
+
|
|
210
|
+
```mermaid
|
|
211
|
+
flowchart TB
|
|
212
|
+
subgraph plugin ["SES Plugin"]
|
|
213
|
+
direction LR
|
|
214
|
+
SRC["HNSource\ndiscover()"] -- "Ref × N" --> EXP["HNExpander\nexpand()"] -- "StoryRecord\nRef × M" --> SNK["HNSink\nconsume()"]
|
|
215
|
+
end
|
|
216
|
+
subgraph persistence ["Persistence"]
|
|
217
|
+
direction LR
|
|
218
|
+
REPO["HNDuckDBRepository"] --> DB[("hn.db")] -- "export_parquet()" --> PQ[("hn.parquet")]
|
|
219
|
+
end
|
|
220
|
+
SNK -- "CommentRecord" --> REPO
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Domain records
|
|
224
|
+
|
|
225
|
+
```mermaid
|
|
226
|
+
flowchart LR
|
|
227
|
+
SR["StoryRecord\n─────────────\nid · title · url\nby · score · time\ndescendants · comment_ids"]
|
|
228
|
+
CR["CommentRecord\n─────────────\nid · story_id · parent_id\nby · text · time"]
|
|
229
|
+
|
|
230
|
+
SR -- "expands into" --> CR
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### SES class map
|
|
234
|
+
|
|
235
|
+
| Layer | Class | HN API call |
|
|
236
|
+
|---|---|---|
|
|
237
|
+
| `Source` | `HNSource` | `GET /v0/topstories.json` → story ID list |
|
|
238
|
+
| `Expander` | `HNExpander` | `GET /v0/item/{story_id}.json` → comment refs |
|
|
239
|
+
| `Sink` | `HNSink` | `GET /v0/item/{comment_id}.json` → `CommentRecord` |
|
|
240
|
+
|
|
241
|
+
`HNDuckDBRepository` implements both `Repository` (leaf persistence) and
|
|
242
|
+
`RunAudit` (run history) from [`ladon.persistence`](https://github.com/MoonyFringers/ladon/blob/main/docs/decisions/adr-006-persistence-layer.md)
|
|
243
|
+
— structurally, with no Ladon base class imported.
|
|
244
|
+
|
|
245
|
+
Each story is one independent run. The run audit trail lets you resume from
|
|
246
|
+
the last successful crawl:
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
last = repo.get_last_run("hackernews") # most recent "done" run
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Writing your own adapter
|
|
253
|
+
|
|
254
|
+
`ladon-hackernews` is the canonical reference for building a Ladon adapter.
|
|
255
|
+
See the [Ladon documentation](https://moonyfringers.github.io/ladon/) and
|
|
256
|
+
[ADR-003](https://github.com/MoonyFringers/ladon/blob/main/docs/decisions/adr-003-plugin-adapter-interface.md)
|
|
257
|
+
for the full adapter authoring guide.
|
|
258
|
+
|
|
259
|
+
Key pattern: adapters implement Ladon protocols **structurally** — no
|
|
260
|
+
inheritance from any Ladon base class is required. Only `RunRecord` needs
|
|
261
|
+
to be imported for `RunAudit` implementations.
|
|
262
|
+
|
|
263
|
+
## Development
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
git clone https://github.com/MoonyFringers/ladon-hackernews
|
|
267
|
+
cd ladon-hackernews
|
|
268
|
+
pip install -e ".[dev]"
|
|
269
|
+
pre-commit install
|
|
270
|
+
pytest tests/ -v
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for full guidelines.
|
|
274
|
+
|
|
275
|
+
## License
|
|
276
|
+
|
|
277
|
+
Apache-2.0 — see [LICENSE](LICENSE).
|
|
278
|
+
|
|
279
|
+
The [Ladon](https://github.com/MoonyFringers/ladon) core framework is
|
|
280
|
+
AGPL-3.0-only. `ladon-hackernews` is Apache-2.0 but has a runtime
|
|
281
|
+
dependency on Ladon core; review the AGPL terms if you plan to distribute
|
|
282
|
+
or run this as a network service.
|