@oboapp/source-sredec-sofia-org 0.0.0-stage → 0.1.1
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.
- package/CHANGELOG.md +11 -0
- package/LICENSE +24 -0
- package/README.md +38 -2
- package/dist/cjs/category-posts.d.ts +7 -0
- package/dist/cjs/category-posts.js +69 -0
- package/dist/cjs/discovery.d.ts +12 -0
- package/dist/cjs/discovery.js +98 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/metadata.d.ts +2 -0
- package/dist/cjs/metadata.js +9 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/category-posts.d.ts +7 -0
- package/dist/esm/category-posts.js +66 -0
- package/dist/esm/discovery.d.ts +12 -0
- package/dist/esm/discovery.js +93 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +13 -0
- package/dist/esm/metadata.d.ts +2 -0
- package/dist/esm/metadata.js +6 -0
- package/package.json +65 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
- Fall back to the affected category's public WordPress posts API when its first RSS page returns HTTP 415.
|
|
6
|
+
|
|
7
|
+
## 0.1.0
|
|
8
|
+
|
|
9
|
+
- Extract Sredets discovery and article collection into a database-free source package.
|
|
10
|
+
- Preserve all five category feeds, pagination safeguards, historical URL spelling, publication dates, content selectors, and request delays.
|
|
11
|
+
- Provide isolated catalog metadata and ESM/CommonJS entrypoints through the source SDK.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Sredets source
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@oboapp/source-sredec-sofia-org` collects public notices from Sofia district Sredets. Its persisted source ID is `sredec-sofia-org`, with locality `bg.sofia`.
|
|
4
|
+
|
|
5
|
+
## Install and use
|
|
6
|
+
|
|
7
|
+
Use Node.js 20.3 or later and install the package with `pnpm add @oboapp/source-sredec-sofia-org`. Article collection uses Playwright Chromium. Install the browser revision required by `@oboapp/source-web-utils` and its operating-system dependencies in the runtime image.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createSource } from "@oboapp/source-sredec-sofia-org";
|
|
11
|
+
import { metadata } from "@oboapp/source-sredec-sofia-org/metadata";
|
|
12
|
+
|
|
13
|
+
const source = await createSource({ locality: "bg.sofia" });
|
|
14
|
+
try {
|
|
15
|
+
const page = await source.discoverItems();
|
|
16
|
+
for (const item of page.items) {
|
|
17
|
+
const content = await source.fetchItem(item);
|
|
18
|
+
console.log(metadata.id, item.identifier, content);
|
|
19
|
+
}
|
|
20
|
+
} finally {
|
|
21
|
+
await source.close?.();
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The `/metadata` entrypoint is safe for browser catalogs and does not load collection code. Obo owns scheduling, historical-record lookup, validation, persistence, and ingestion.
|
|
26
|
+
|
|
27
|
+
## Discovery and coverage
|
|
28
|
+
|
|
29
|
+
Discovery reads five category feeds: useful information, news, announcements, investment notices, and public discussions. RSS pagination uses `?paged=`. When the first RSS page returns HTTP 415, discovery reads that category through the site's public WordPress posts API. Discovery returns one complete page. URLs keep the provider's spelling, including percent encoding and query strings. Cross-category duplicates retain the metadata from the first configured category that listed them.
|
|
30
|
+
|
|
31
|
+
The scan covers items newer than midnight UTC minus 90 days. It waits two seconds before each listing request and stops at a page containing older items. RSS also stops at an empty page or a later-page HTTP 404. The API stops at its reported last page. A repeated page, an unavailable listing, or more than 100 pages makes discovery fail. The next scheduled scan lists again from the beginning.
|
|
32
|
+
|
|
33
|
+
Article content is fetched in a reused browser. The page title is preferred, with the listing title as fallback. Both discovery paths supply the publication timestamp. Scripts and styles are removed, and content is converted to Markdown. A successful article request is followed by a two-second delay. Each opened article page is closed after success or failure, and `close()` releases the shared browser.
|
|
34
|
+
|
|
35
|
+
Feed history and provider removals limit available notices to the current 90-day window. Article output retains document links for ingestion. Obo skips known identifiers, including after package updates, and historical identifiers remain unchanged across versions.
|
|
36
|
+
|
|
37
|
+
## Checks and releases
|
|
38
|
+
|
|
39
|
+
Run `pnpm check` from the sources repository root. Tests cover captured category feeds, HTTP 415 recovery through the posts API, pagination and age boundaries, exact identifiers and content, browser cleanup, and ESM/CommonJS entrypoints. See [RELEASING.md](../../RELEASING.md) for package inspection and independent SemVer releases. Obo's migration is tracked in [#586](https://github.com/oboapp/oboapp/issues/586) and [#609](https://github.com/oboapp/oboapp/issues/609).
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RssFeedItem } from "@oboapp/source-web-utils/rss";
|
|
2
|
+
export interface ListingPage {
|
|
3
|
+
items: RssFeedItem[];
|
|
4
|
+
hasNext: boolean;
|
|
5
|
+
}
|
|
6
|
+
export type FetchPosts = (url: string, options: RequestInit) => Promise<Response>;
|
|
7
|
+
export declare function readCategoryPosts(categoryId: number, page: number, fetchPosts: FetchPosts): Promise<ListingPage>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readCategoryPosts = readCategoryPosts;
|
|
4
|
+
const html_entities_1 = require("html-entities");
|
|
5
|
+
const POSTS_ORIGIN = "https://sredec-sofia.org";
|
|
6
|
+
const POSTS_TIMEOUT_MS = 30_000;
|
|
7
|
+
function parsePost(post) {
|
|
8
|
+
if (!post || typeof post !== "object"
|
|
9
|
+
|| !("link" in post) || typeof post.link !== "string"
|
|
10
|
+
|| !("date_gmt" in post) || typeof post.date_gmt !== "string"
|
|
11
|
+
|| !("title" in post)) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const { title: renderedTitle } = post;
|
|
15
|
+
if (!renderedTitle || typeof renderedTitle !== "object"
|
|
16
|
+
|| !("rendered" in renderedTitle) || typeof renderedTitle.rendered !== "string") {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const title = (0, html_entities_1.decode)(renderedTitle.rendered).trim();
|
|
20
|
+
const timestamp = Date.parse(`${post.date_gmt}Z`);
|
|
21
|
+
if (!title || Number.isNaN(timestamp) || !URL.canParse(post.link)) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
const url = new URL(post.link);
|
|
25
|
+
if (url.origin !== POSTS_ORIGIN || url.username || url.password) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
return { url: url.href, title, date: new Date(timestamp).toISOString() };
|
|
29
|
+
}
|
|
30
|
+
async function readCategoryPosts(categoryId, page, fetchPosts) {
|
|
31
|
+
const url = new URL("/wp-json/wp/v2/posts", POSTS_ORIGIN);
|
|
32
|
+
url.search = new URLSearchParams({
|
|
33
|
+
"categories[terms]": String(categoryId),
|
|
34
|
+
"categories[include_children]": "true",
|
|
35
|
+
orderby: "date",
|
|
36
|
+
order: "desc",
|
|
37
|
+
per_page: "10",
|
|
38
|
+
page: String(page),
|
|
39
|
+
_fields: "link,title,date_gmt",
|
|
40
|
+
}).toString();
|
|
41
|
+
const response = await fetchPosts(url.href, {
|
|
42
|
+
headers: {
|
|
43
|
+
"User-Agent": "Mozilla/5.0 (compatible; oboapp-crawler/1.0)",
|
|
44
|
+
Accept: "application/json",
|
|
45
|
+
},
|
|
46
|
+
redirect: "error",
|
|
47
|
+
signal: AbortSignal.timeout(POSTS_TIMEOUT_MS),
|
|
48
|
+
});
|
|
49
|
+
if (!response.ok) {
|
|
50
|
+
await response.body?.cancel().catch(() => undefined);
|
|
51
|
+
throw new Error(`Sredets posts API returned HTTP ${response.status} for ${url.href}`);
|
|
52
|
+
}
|
|
53
|
+
const pageCount = response.headers.get("X-WP-TotalPages");
|
|
54
|
+
const posts = await response.json().catch((cause) => {
|
|
55
|
+
throw new Error(`Invalid Sredets posts API JSON for ${url.href}`, { cause });
|
|
56
|
+
});
|
|
57
|
+
if (!Array.isArray(posts) || !pageCount || !/^\d+$/.test(pageCount)) {
|
|
58
|
+
throw new Error(`Invalid Sredets posts API listing for ${url.href}`);
|
|
59
|
+
}
|
|
60
|
+
const hasNext = page < Number(pageCount);
|
|
61
|
+
const items = posts.flatMap((post) => {
|
|
62
|
+
const item = parsePost(post);
|
|
63
|
+
return item ? [item] : [];
|
|
64
|
+
});
|
|
65
|
+
if (items.length === 0 && (posts.length > 0 || hasNext)) {
|
|
66
|
+
throw new Error(`No dated Sredets notices found in ${url.href}`);
|
|
67
|
+
}
|
|
68
|
+
return { items, hasNext };
|
|
69
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { fetchFeedXml, type RssFeedItem } from "@oboapp/source-web-utils/rss";
|
|
2
|
+
import { type FetchPosts } from "./category-posts.js";
|
|
3
|
+
export declare const FEED_URLS: string[];
|
|
4
|
+
export interface DiscoveryDependencies {
|
|
5
|
+
fetchFeed: typeof fetchFeedXml;
|
|
6
|
+
fetchPosts: FetchPosts;
|
|
7
|
+
delay: (milliseconds: number) => Promise<void>;
|
|
8
|
+
now: () => Date;
|
|
9
|
+
}
|
|
10
|
+
export declare function extractFeedItems(xml: string): RssFeedItem[];
|
|
11
|
+
/** Read the complete listing before returning candidates to the platform. */
|
|
12
|
+
export declare function discoverRecentItems(overrides?: Partial<DiscoveryDependencies>): Promise<RssFeedItem[]>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FEED_URLS = void 0;
|
|
4
|
+
exports.extractFeedItems = extractFeedItems;
|
|
5
|
+
exports.discoverRecentItems = discoverRecentItems;
|
|
6
|
+
const rss_1 = require("@oboapp/source-web-utils/rss");
|
|
7
|
+
const category_posts_js_1 = require("./category-posts.js");
|
|
8
|
+
const CATEGORIES = [
|
|
9
|
+
{ slug: "публикации/полезна-информация", id: 87 },
|
|
10
|
+
{ slug: "новини", id: 1 },
|
|
11
|
+
{ slug: "публикации/saobshtenia", id: 84 },
|
|
12
|
+
{ slug: "публикации/инвестиционни-намерения", id: 81 },
|
|
13
|
+
{ slug: "обществениобсъждания", id: 89 },
|
|
14
|
+
].map(({ slug, id }) => ({ feedUrl: `https://sredec-sofia.org/category/${slug}/feed/`, id }));
|
|
15
|
+
exports.FEED_URLS = CATEGORIES.map(({ feedUrl }) => feedUrl);
|
|
16
|
+
const MAX_PAGES_PER_LISTING = 100;
|
|
17
|
+
const REQUEST_DELAY_MS = 2000;
|
|
18
|
+
const MAX_AGE_MS = 90 * 24 * 60 * 60 * 1000;
|
|
19
|
+
const defaults = {
|
|
20
|
+
fetchFeed: rss_1.fetchFeedXml,
|
|
21
|
+
fetchPosts: (...args) => fetch(...args),
|
|
22
|
+
delay: (milliseconds) => new Promise((resolve) => {
|
|
23
|
+
setTimeout(resolve, milliseconds);
|
|
24
|
+
}),
|
|
25
|
+
now: () => new Date(),
|
|
26
|
+
};
|
|
27
|
+
function extractFeedItems(xml) {
|
|
28
|
+
return (0, rss_1.parseRssFeedItems)(xml, { hostname: "sredec-sofia.org" });
|
|
29
|
+
}
|
|
30
|
+
async function readFeedPage(feedUrl, page, readFeed) {
|
|
31
|
+
const url = new URL(feedUrl);
|
|
32
|
+
if (page > 1) {
|
|
33
|
+
url.searchParams.set("paged", String(page));
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const items = extractFeedItems(await readFeed(url.href));
|
|
37
|
+
return { items, hasNext: items.length > 0 };
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof rss_1.RssFeedHttpError) {
|
|
41
|
+
if (page === 1 && error.status === 415) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
if (page > 1 && error.status === 404) {
|
|
45
|
+
return { items: [], hasNext: false };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function discoverCategory({ feedUrl, id }, cutoff, dependencies) {
|
|
52
|
+
let usePostsApi = false;
|
|
53
|
+
async function readPage(page) {
|
|
54
|
+
await dependencies.delay(REQUEST_DELAY_MS);
|
|
55
|
+
if (!usePostsApi) {
|
|
56
|
+
const feedPage = await readFeedPage(feedUrl, page, dependencies.fetchFeed);
|
|
57
|
+
if (feedPage) {
|
|
58
|
+
return feedPage;
|
|
59
|
+
}
|
|
60
|
+
usePostsApi = true;
|
|
61
|
+
await dependencies.delay(REQUEST_DELAY_MS);
|
|
62
|
+
}
|
|
63
|
+
return (0, category_posts_js_1.readCategoryPosts)(id, page, dependencies.fetchPosts);
|
|
64
|
+
}
|
|
65
|
+
const found = new Map();
|
|
66
|
+
for (let page = 1; page <= MAX_PAGES_PER_LISTING; page++) {
|
|
67
|
+
const { items, hasNext } = await readPage(page);
|
|
68
|
+
const recent = items.filter((item) => Date.parse(item.date) > cutoff);
|
|
69
|
+
const newItems = recent.filter((item) => !found.has(item.url));
|
|
70
|
+
for (const item of newItems) {
|
|
71
|
+
found.set(item.url, item);
|
|
72
|
+
}
|
|
73
|
+
if (items.length > 0 && recent.length === items.length && newItems.length === 0) {
|
|
74
|
+
throw new Error(`Sredets pagination repeated a page for ${feedUrl}`);
|
|
75
|
+
}
|
|
76
|
+
if (!hasNext || recent.length < items.length) {
|
|
77
|
+
return [...found.values()];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
throw new Error(`Sredets pagination exceeded ${MAX_PAGES_PER_LISTING} pages for ${feedUrl}`);
|
|
81
|
+
}
|
|
82
|
+
/** Read the complete listing before returning candidates to the platform. */
|
|
83
|
+
async function discoverRecentItems(overrides = {}) {
|
|
84
|
+
const dependencies = { ...defaults, ...overrides };
|
|
85
|
+
const midnight = new Date(dependencies.now());
|
|
86
|
+
midnight.setUTCHours(0, 0, 0, 0);
|
|
87
|
+
const cutoff = midnight.getTime() - MAX_AGE_MS;
|
|
88
|
+
const found = new Map();
|
|
89
|
+
for (const category of CATEGORIES) {
|
|
90
|
+
const items = await discoverCategory(category, cutoff, dependencies);
|
|
91
|
+
for (const item of items) {
|
|
92
|
+
if (!found.has(item.url)) {
|
|
93
|
+
found.set(item.url, item);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return [...found.values()];
|
|
98
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { CreateSource, DataSource } from "@oboapp/source-sdk";
|
|
2
|
+
import { type HybridDependencies } from "@oboapp/source-web-utils";
|
|
3
|
+
import { type DiscoveryDependencies } from "./discovery.js";
|
|
4
|
+
export declare function createSourceWithDependencies(browserDependencies?: HybridDependencies, discoveryDependencies?: Partial<DiscoveryDependencies>): DataSource;
|
|
5
|
+
export declare const createSource: CreateSource;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSource = void 0;
|
|
4
|
+
exports.createSourceWithDependencies = createSourceWithDependencies;
|
|
5
|
+
const source_web_utils_1 = require("@oboapp/source-web-utils");
|
|
6
|
+
const discovery_js_1 = require("./discovery.js");
|
|
7
|
+
function createSourceWithDependencies(browserDependencies, discoveryDependencies = {}) {
|
|
8
|
+
return (0, source_web_utils_1.createHybridSource)({
|
|
9
|
+
discoverItems: () => (0, discovery_js_1.discoverRecentItems)(discoveryDependencies),
|
|
10
|
+
selectors: {
|
|
11
|
+
TITLE: "h1.single-post-title",
|
|
12
|
+
CONTENT: ".single-blog-content.entry",
|
|
13
|
+
},
|
|
14
|
+
unwantedElements: ["script", "style"],
|
|
15
|
+
}, browserDependencies);
|
|
16
|
+
}
|
|
17
|
+
const createSource = () => createSourceWithDependencies();
|
|
18
|
+
exports.createSource = createSource;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.metadata = void 0;
|
|
4
|
+
exports.metadata = {
|
|
5
|
+
id: "sredec-sofia-org",
|
|
6
|
+
url: "https://sredec-sofia.org/",
|
|
7
|
+
name: 'Столична община, Район "Средец"',
|
|
8
|
+
localities: ["bg.sofia"],
|
|
9
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RssFeedItem } from "@oboapp/source-web-utils/rss";
|
|
2
|
+
export interface ListingPage {
|
|
3
|
+
items: RssFeedItem[];
|
|
4
|
+
hasNext: boolean;
|
|
5
|
+
}
|
|
6
|
+
export type FetchPosts = (url: string, options: RequestInit) => Promise<Response>;
|
|
7
|
+
export declare function readCategoryPosts(categoryId: number, page: number, fetchPosts: FetchPosts): Promise<ListingPage>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { decode } from "html-entities";
|
|
2
|
+
const POSTS_ORIGIN = "https://sredec-sofia.org";
|
|
3
|
+
const POSTS_TIMEOUT_MS = 30_000;
|
|
4
|
+
function parsePost(post) {
|
|
5
|
+
if (!post || typeof post !== "object"
|
|
6
|
+
|| !("link" in post) || typeof post.link !== "string"
|
|
7
|
+
|| !("date_gmt" in post) || typeof post.date_gmt !== "string"
|
|
8
|
+
|| !("title" in post)) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
const { title: renderedTitle } = post;
|
|
12
|
+
if (!renderedTitle || typeof renderedTitle !== "object"
|
|
13
|
+
|| !("rendered" in renderedTitle) || typeof renderedTitle.rendered !== "string") {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const title = decode(renderedTitle.rendered).trim();
|
|
17
|
+
const timestamp = Date.parse(`${post.date_gmt}Z`);
|
|
18
|
+
if (!title || Number.isNaN(timestamp) || !URL.canParse(post.link)) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
const url = new URL(post.link);
|
|
22
|
+
if (url.origin !== POSTS_ORIGIN || url.username || url.password) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
return { url: url.href, title, date: new Date(timestamp).toISOString() };
|
|
26
|
+
}
|
|
27
|
+
export async function readCategoryPosts(categoryId, page, fetchPosts) {
|
|
28
|
+
const url = new URL("/wp-json/wp/v2/posts", POSTS_ORIGIN);
|
|
29
|
+
url.search = new URLSearchParams({
|
|
30
|
+
"categories[terms]": String(categoryId),
|
|
31
|
+
"categories[include_children]": "true",
|
|
32
|
+
orderby: "date",
|
|
33
|
+
order: "desc",
|
|
34
|
+
per_page: "10",
|
|
35
|
+
page: String(page),
|
|
36
|
+
_fields: "link,title,date_gmt",
|
|
37
|
+
}).toString();
|
|
38
|
+
const response = await fetchPosts(url.href, {
|
|
39
|
+
headers: {
|
|
40
|
+
"User-Agent": "Mozilla/5.0 (compatible; oboapp-crawler/1.0)",
|
|
41
|
+
Accept: "application/json",
|
|
42
|
+
},
|
|
43
|
+
redirect: "error",
|
|
44
|
+
signal: AbortSignal.timeout(POSTS_TIMEOUT_MS),
|
|
45
|
+
});
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
await response.body?.cancel().catch(() => undefined);
|
|
48
|
+
throw new Error(`Sredets posts API returned HTTP ${response.status} for ${url.href}`);
|
|
49
|
+
}
|
|
50
|
+
const pageCount = response.headers.get("X-WP-TotalPages");
|
|
51
|
+
const posts = await response.json().catch((cause) => {
|
|
52
|
+
throw new Error(`Invalid Sredets posts API JSON for ${url.href}`, { cause });
|
|
53
|
+
});
|
|
54
|
+
if (!Array.isArray(posts) || !pageCount || !/^\d+$/.test(pageCount)) {
|
|
55
|
+
throw new Error(`Invalid Sredets posts API listing for ${url.href}`);
|
|
56
|
+
}
|
|
57
|
+
const hasNext = page < Number(pageCount);
|
|
58
|
+
const items = posts.flatMap((post) => {
|
|
59
|
+
const item = parsePost(post);
|
|
60
|
+
return item ? [item] : [];
|
|
61
|
+
});
|
|
62
|
+
if (items.length === 0 && (posts.length > 0 || hasNext)) {
|
|
63
|
+
throw new Error(`No dated Sredets notices found in ${url.href}`);
|
|
64
|
+
}
|
|
65
|
+
return { items, hasNext };
|
|
66
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { fetchFeedXml, type RssFeedItem } from "@oboapp/source-web-utils/rss";
|
|
2
|
+
import { type FetchPosts } from "./category-posts.js";
|
|
3
|
+
export declare const FEED_URLS: string[];
|
|
4
|
+
export interface DiscoveryDependencies {
|
|
5
|
+
fetchFeed: typeof fetchFeedXml;
|
|
6
|
+
fetchPosts: FetchPosts;
|
|
7
|
+
delay: (milliseconds: number) => Promise<void>;
|
|
8
|
+
now: () => Date;
|
|
9
|
+
}
|
|
10
|
+
export declare function extractFeedItems(xml: string): RssFeedItem[];
|
|
11
|
+
/** Read the complete listing before returning candidates to the platform. */
|
|
12
|
+
export declare function discoverRecentItems(overrides?: Partial<DiscoveryDependencies>): Promise<RssFeedItem[]>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { fetchFeedXml, parseRssFeedItems, RssFeedHttpError, } from "@oboapp/source-web-utils/rss";
|
|
2
|
+
import { readCategoryPosts } from "./category-posts.js";
|
|
3
|
+
const CATEGORIES = [
|
|
4
|
+
{ slug: "публикации/полезна-информация", id: 87 },
|
|
5
|
+
{ slug: "новини", id: 1 },
|
|
6
|
+
{ slug: "публикации/saobshtenia", id: 84 },
|
|
7
|
+
{ slug: "публикации/инвестиционни-намерения", id: 81 },
|
|
8
|
+
{ slug: "обществениобсъждания", id: 89 },
|
|
9
|
+
].map(({ slug, id }) => ({ feedUrl: `https://sredec-sofia.org/category/${slug}/feed/`, id }));
|
|
10
|
+
export const FEED_URLS = CATEGORIES.map(({ feedUrl }) => feedUrl);
|
|
11
|
+
const MAX_PAGES_PER_LISTING = 100;
|
|
12
|
+
const REQUEST_DELAY_MS = 2000;
|
|
13
|
+
const MAX_AGE_MS = 90 * 24 * 60 * 60 * 1000;
|
|
14
|
+
const defaults = {
|
|
15
|
+
fetchFeed: fetchFeedXml,
|
|
16
|
+
fetchPosts: (...args) => fetch(...args),
|
|
17
|
+
delay: (milliseconds) => new Promise((resolve) => {
|
|
18
|
+
setTimeout(resolve, milliseconds);
|
|
19
|
+
}),
|
|
20
|
+
now: () => new Date(),
|
|
21
|
+
};
|
|
22
|
+
export function extractFeedItems(xml) {
|
|
23
|
+
return parseRssFeedItems(xml, { hostname: "sredec-sofia.org" });
|
|
24
|
+
}
|
|
25
|
+
async function readFeedPage(feedUrl, page, readFeed) {
|
|
26
|
+
const url = new URL(feedUrl);
|
|
27
|
+
if (page > 1) {
|
|
28
|
+
url.searchParams.set("paged", String(page));
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const items = extractFeedItems(await readFeed(url.href));
|
|
32
|
+
return { items, hasNext: items.length > 0 };
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (error instanceof RssFeedHttpError) {
|
|
36
|
+
if (page === 1 && error.status === 415) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
if (page > 1 && error.status === 404) {
|
|
40
|
+
return { items: [], hasNext: false };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function discoverCategory({ feedUrl, id }, cutoff, dependencies) {
|
|
47
|
+
let usePostsApi = false;
|
|
48
|
+
async function readPage(page) {
|
|
49
|
+
await dependencies.delay(REQUEST_DELAY_MS);
|
|
50
|
+
if (!usePostsApi) {
|
|
51
|
+
const feedPage = await readFeedPage(feedUrl, page, dependencies.fetchFeed);
|
|
52
|
+
if (feedPage) {
|
|
53
|
+
return feedPage;
|
|
54
|
+
}
|
|
55
|
+
usePostsApi = true;
|
|
56
|
+
await dependencies.delay(REQUEST_DELAY_MS);
|
|
57
|
+
}
|
|
58
|
+
return readCategoryPosts(id, page, dependencies.fetchPosts);
|
|
59
|
+
}
|
|
60
|
+
const found = new Map();
|
|
61
|
+
for (let page = 1; page <= MAX_PAGES_PER_LISTING; page++) {
|
|
62
|
+
const { items, hasNext } = await readPage(page);
|
|
63
|
+
const recent = items.filter((item) => Date.parse(item.date) > cutoff);
|
|
64
|
+
const newItems = recent.filter((item) => !found.has(item.url));
|
|
65
|
+
for (const item of newItems) {
|
|
66
|
+
found.set(item.url, item);
|
|
67
|
+
}
|
|
68
|
+
if (items.length > 0 && recent.length === items.length && newItems.length === 0) {
|
|
69
|
+
throw new Error(`Sredets pagination repeated a page for ${feedUrl}`);
|
|
70
|
+
}
|
|
71
|
+
if (!hasNext || recent.length < items.length) {
|
|
72
|
+
return [...found.values()];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
throw new Error(`Sredets pagination exceeded ${MAX_PAGES_PER_LISTING} pages for ${feedUrl}`);
|
|
76
|
+
}
|
|
77
|
+
/** Read the complete listing before returning candidates to the platform. */
|
|
78
|
+
export async function discoverRecentItems(overrides = {}) {
|
|
79
|
+
const dependencies = { ...defaults, ...overrides };
|
|
80
|
+
const midnight = new Date(dependencies.now());
|
|
81
|
+
midnight.setUTCHours(0, 0, 0, 0);
|
|
82
|
+
const cutoff = midnight.getTime() - MAX_AGE_MS;
|
|
83
|
+
const found = new Map();
|
|
84
|
+
for (const category of CATEGORIES) {
|
|
85
|
+
const items = await discoverCategory(category, cutoff, dependencies);
|
|
86
|
+
for (const item of items) {
|
|
87
|
+
if (!found.has(item.url)) {
|
|
88
|
+
found.set(item.url, item);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return [...found.values()];
|
|
93
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { CreateSource, DataSource } from "@oboapp/source-sdk";
|
|
2
|
+
import { type HybridDependencies } from "@oboapp/source-web-utils";
|
|
3
|
+
import { type DiscoveryDependencies } from "./discovery.js";
|
|
4
|
+
export declare function createSourceWithDependencies(browserDependencies?: HybridDependencies, discoveryDependencies?: Partial<DiscoveryDependencies>): DataSource;
|
|
5
|
+
export declare const createSource: CreateSource;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createHybridSource } from "@oboapp/source-web-utils";
|
|
2
|
+
import { discoverRecentItems } from "./discovery.js";
|
|
3
|
+
export function createSourceWithDependencies(browserDependencies, discoveryDependencies = {}) {
|
|
4
|
+
return createHybridSource({
|
|
5
|
+
discoverItems: () => discoverRecentItems(discoveryDependencies),
|
|
6
|
+
selectors: {
|
|
7
|
+
TITLE: "h1.single-post-title",
|
|
8
|
+
CONTENT: ".single-blog-content.entry",
|
|
9
|
+
},
|
|
10
|
+
unwantedElements: ["script", "style"],
|
|
11
|
+
}, browserDependencies);
|
|
12
|
+
}
|
|
13
|
+
export const createSource = () => createSourceWithDependencies();
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oboapp/source-sredec-sofia-org",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Paginated RSS and webpage source for Sofia district Sredets",
|
|
5
|
+
"main": "./dist/cjs/index.js",
|
|
6
|
+
"license": "Unlicense",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "./dist/cjs/index.d.ts",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/oboapp/sources.git",
|
|
12
|
+
"directory": "packages/source-sredec-sofia-org"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20.3"
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"README.md",
|
|
24
|
+
"CHANGELOG.md"
|
|
25
|
+
],
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"typesVersions": {
|
|
28
|
+
"*": {
|
|
29
|
+
"metadata": [
|
|
30
|
+
"dist/cjs/metadata.d.ts"
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/esm/index.d.ts",
|
|
38
|
+
"default": "./dist/esm/index.js"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./dist/cjs/index.d.ts",
|
|
42
|
+
"default": "./dist/cjs/index.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"./metadata": {
|
|
46
|
+
"import": {
|
|
47
|
+
"types": "./dist/esm/metadata.d.ts",
|
|
48
|
+
"default": "./dist/esm/metadata.js"
|
|
49
|
+
},
|
|
50
|
+
"require": {
|
|
51
|
+
"types": "./dist/cjs/metadata.d.ts",
|
|
52
|
+
"default": "./dist/cjs/metadata.js"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@oboapp/source-sdk": "0.1.1",
|
|
58
|
+
"html-entities": "2.6.0",
|
|
59
|
+
"@oboapp/source-web-utils": "0.5.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"happy-dom": "20.0.11"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"test": "tsc -p test/tsconfig.json && tsx --test test/*.test.ts",
|
|
66
|
+
"build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && node scripts/finalize-build.mjs"
|
|
67
|
+
}
|
|
6
68
|
}
|