argo-search 1.0.1 → 2.9.8
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/LICENSE +201 -21
- package/README.en.md +735 -0
- package/README.es.md +777 -0
- package/README.ja.md +777 -0
- package/README.ko.md +777 -0
- package/README.md +778 -320
- package/SKILL.md +135 -0
- package/assets/readme/hero.svg +121 -0
- package/assets/readme/made-with-beautify.svg +10 -0
- package/assets/readme/proof-routes.svg +73 -0
- package/assets/readme/why-better.svg +104 -0
- package/assets/readme/workflow.svg +83 -0
- package/backends/domain_profiles.json +2356 -96
- package/backends/engine_registry.yaml +3428 -503
- package/backends/query_synonyms_cn.json +35 -0
- package/backends/quota_profiles.json +2488 -160
- package/backends/source_types_cn.json +109 -0
- package/backends/text_lexicon.json +44 -0
- package/bin/argo +772 -0
- package/bin/argo.js +102 -39
- package/config.yaml +5139 -367
- package/cordis.patch.yml +21 -0
- package/data/countries.json +40 -0
- package/data/lang_matrix/lang_capability.json +842 -0
- package/data/lang_matrix/matrix_2026-09-10.json +11921 -0
- package/data/regions_cn.json +1 -0
- package/docs/ADDING_NEW_ENGINE.md +184 -0
- package/docs/ARGO_INTRO.md +124 -0
- package/docs/DATA_SOURCE_GAPS.md +81 -0
- package/docs/DESIGN_LOCAL_DATA_FUSION.md +90 -0
- package/docs/ENGINE_CATALOG.md +575 -0
- package/docs/MCP_SETUP.md +108 -0
- package/docs/PLATFORM_PATHS.md +111 -0
- package/docs/RELEASE_NOTES_v2.8.0.md +71 -0
- package/docs/RELEASE_NOTES_v2.8.2.md +63 -0
- package/docs/RELEASE_NOTES_v2.8.3.md +55 -0
- package/docs/RELEASE_NOTES_v2.8.4.md +79 -0
- package/docs/RELEASE_NOTES_v2.8.5.md +102 -0
- package/docs/RELEASE_NOTES_v2.8.6.md +66 -0
- package/docs/RELEASE_NOTES_v2.8.7.md +65 -0
- package/docs/RELEASE_NOTES_v2.8.8.md +67 -0
- package/docs/RELEASE_NOTES_v2.8.9.md +67 -0
- package/docs/RELEASE_NOTES_v2.9.0.md +64 -0
- package/docs/RELEASE_NOTES_v2.9.8.md +73 -0
- package/docs/SEARCH_ARCHIVE.md +115 -0
- package/docs/assets/architecture.svg +104 -0
- package/docs/assets/hero.svg +98 -0
- package/docs/engines/bangumi.md +27 -0
- package/docs/engines/biorxiv.md +27 -0
- package/docs/engines/clawhub.md +27 -0
- package/docs/engines/crt_sh.md +27 -0
- package/docs/engines/datacite.md +27 -0
- package/docs/engines/deps_dev.md +27 -0
- package/docs/engines/douban_movie.md +27 -0
- package/docs/engines/endoflife.md +27 -0
- package/docs/engines/flk_law.md +27 -0
- package/docs/engines/fxtwitter.md +27 -0
- package/docs/engines/google_news.md +27 -0
- package/docs/engines/gov_regulations.md +27 -0
- package/docs/engines/guqian.md +27 -0
- package/docs/engines/hackernews.md +27 -0
- package/docs/engines/iplant.md +27 -0
- package/docs/engines/keenable.md +27 -0
- package/docs/engines/met_museum.md +27 -0
- package/docs/engines/nasa_images.md +27 -0
- package/docs/engines/netease_music.md +27 -0
- package/docs/engines/npm.md +27 -0
- package/docs/engines/nvd.md +27 -0
- package/docs/engines/openreview.md +27 -0
- package/docs/engines/openstd.md +27 -0
- package/docs/engines/parallel_free.md +27 -0
- package/docs/engines/people_daily.md +27 -0
- package/docs/engines/realtime_index.md +27 -0
- package/docs/engines/sec_edgar.md +27 -0
- package/docs/engines/seltz.md +27 -0
- package/docs/engines/skillsmp.md +27 -0
- package/docs/engines/sspai.md +27 -0
- package/docs/engines/std_samr.md +27 -0
- package/docs/engines/train.md +27 -0
- package/docs/engines/un_comtrade.md +27 -0
- package/docs/engines/wikisource.md +27 -0
- package/docs/engines/zdic.md +27 -0
- package/docs/engines/zenodo.md +27 -0
- package/docs/engines/zhihu_hot_app.md +27 -0
- package/docs//344/270/272/344/273/200/344/271/210/351/200/211/346/213/251argo.md +176 -0
- package/engines/README.md +10 -0
- package/engines/_template_cli.yaml +39 -0
- package/engines/_template_http.yaml +37 -0
- package/engines/plugins/_template_plugin.py +50 -0
- package/engines/specs/_cisa_kev.yaml +54 -0
- package/engines/specs/_osv.yaml +63 -0
- package/engines/specs/aljazeera.yaml +43 -0
- package/engines/specs/antara.yaml +43 -0
- package/engines/specs/artic.yaml +20 -0
- package/engines/specs/aviation_weather.yaml +39 -0
- package/engines/specs/bangumi.yaml +21 -0
- package/engines/specs/biorxiv.yaml +24 -0
- package/engines/specs/carbon_intensity.yaml +32 -0
- package/engines/specs/cfets.yaml +41 -0
- package/engines/specs/cinii.yaml +44 -0
- package/engines/specs/clawhub.yaml +49 -0
- package/engines/specs/cleveland.yaml +19 -0
- package/engines/specs/cn_ai_news.yaml +43 -0
- package/engines/specs/core.yaml +35 -0
- package/engines/specs/crt_sh.yaml +38 -0
- package/engines/specs/ct24.yaml +43 -0
- package/engines/specs/datacite.yaml +27 -0
- package/engines/specs/datagov_uk.yaml +41 -0
- package/engines/specs/ddgs_images.yaml +45 -0
- package/engines/specs/deepseek_search.yaml +74 -0
- package/engines/specs/deezer.yaml +20 -0
- package/engines/specs/defillama.yaml +38 -0
- package/engines/specs/deps_dev.yaml +21 -0
- package/engines/specs/digup.yaml +36 -0
- package/engines/specs/doi.yaml +39 -0
- package/engines/specs/douban_movie.yaml +22 -0
- package/engines/specs/dw_news.yaml +47 -0
- package/engines/specs/egov_law.yaml +21 -0
- package/engines/specs/elpais.yaml +43 -0
- package/engines/specs/endoflife.yaml +21 -0
- package/engines/specs/energy_charts.yaml +21 -0
- package/engines/specs/factcheck_org.yaml +52 -0
- package/engines/specs/faz.yaml +43 -0
- package/engines/specs/federal_register.yaml +57 -0
- package/engines/specs/firecrawl.yaml +53 -0
- package/engines/specs/flk_law.yaml +24 -0
- package/engines/specs/folha.yaml +43 -0
- package/engines/specs/france24.yaml +44 -0
- package/engines/specs/full_fact.yaml +47 -0
- package/engines/specs/fxtwitter.yaml +36 -0
- package/engines/specs/gbfs_nyc.yaml +32 -0
- package/engines/specs/gdacs.yaml +21 -0
- package/engines/specs/gdelt.yaml +64 -0
- package/engines/specs/gleif.yaml +55 -0
- package/engines/specs/google_news.yaml +22 -0
- package/engines/specs/gov_regulations.yaml +38 -0
- package/engines/specs/guardian_rss.yaml +59 -0
- package/engines/specs/hkex.yaml +40 -0
- package/engines/specs/hurriyet.yaml +43 -0
- package/engines/specs/iplant.yaml +25 -0
- package/engines/specs/jikan.yaml +24 -0
- package/engines/specs/jolpica.yaml +20 -0
- package/engines/specs/k10plus.yaml +20 -0
- package/engines/specs/keenable.yaml +44 -0
- package/engines/specs/keenable_free.yaml +36 -0
- package/engines/specs/lefigaro.yaml +43 -0
- package/engines/specs/listenbrainz.yaml +19 -0
- package/engines/specs/matichon.yaml +43 -0
- package/engines/specs/met_museum.yaml +21 -0
- package/engines/specs/nasa_images.yaml +45 -0
- package/engines/specs/netease_music.yaml +38 -0
- package/engines/specs/nhk.yaml +43 -0
- package/engines/specs/nhtsa_vpic.yaml +20 -0
- package/engines/specs/noaa_swpc.yaml +20 -0
- package/engines/specs/nvd.yaml +38 -0
- package/engines/specs/obis.yaml +20 -0
- package/engines/specs/opencitations.yaml +52 -0
- package/engines/specs/openf1.yaml +21 -0
- package/engines/specs/openfoodfacts.yaml +45 -0
- package/engines/specs/openligadb.yaml +20 -0
- package/engines/specs/openreview.yaml +37 -0
- package/engines/specs/openstd.yaml +21 -0
- package/engines/specs/people_daily.yaml +22 -0
- package/engines/specs/realtime_index.yaml +34 -0
- package/engines/specs/ror.yaml +20 -0
- package/engines/specs/satnogs.yaml +20 -0
- package/engines/specs/sec_edgar.yaml +46 -0
- package/engines/specs/shfe.yaml +39 -0
- package/engines/specs/skillsmp.yaml +50 -0
- package/engines/specs/sogou_weixin_browser.yaml +30 -0
- package/engines/specs/soilgrids.yaml +25 -0
- package/engines/specs/sspai.yaml +36 -0
- package/engines/specs/stackexchange.yaml +46 -0
- package/engines/specs/std_samr.yaml +21 -0
- package/engines/specs/tass.yaml +43 -0
- package/engines/specs/tavily_free.yaml +56 -0
- package/engines/specs/tle_mirror.yaml +21 -0
- package/engines/specs/train.yaml +37 -0
- package/engines/specs/tvmaze.yaml +20 -0
- package/engines/specs/twitter_syndication.yaml +31 -0
- package/engines/specs/un_comtrade.yaml +23 -0
- package/engines/specs/unpaywall.yaml +54 -0
- package/engines/specs/vnpress.yaml +43 -0
- package/engines/specs/weather.yaml +32 -0
- package/engines/specs/who_don.yaml +21 -0
- package/engines/specs/who_gho.yaml +20 -0
- package/engines/specs/wikimedia_commons.yaml +34 -0
- package/engines/specs/wikisource.yaml +22 -0
- package/engines/specs/worms.yaml +19 -0
- package/engines/specs/x_trending.yaml +34 -0
- package/engines/specs/yna.yaml +43 -0
- package/engines/specs/ynet.yaml +43 -0
- package/engines/specs/yuanbao.yaml +93 -0
- package/engines/specs/zbmath.yaml +40 -0
- package/engines/specs/zdic.yaml +22 -0
- package/engines/specs/zenodo.yaml +27 -0
- package/engines/specs/zhihu_hot_app.yaml +35 -0
- package/mcp/clients.yaml +106 -0
- package/package.json +55 -12
- package/packages/dsh-plugin/README.md +152 -0
- package/packages/dsh-plugin/cordis.patch.yml +48 -0
- package/packages/dsh-plugin/dsh/client.js +1533 -0
- package/packages/dsh-plugin/dsh/diagnostics.js +54 -0
- package/packages/dsh-plugin/dsh/index.js +527 -0
- package/packages/dsh-plugin/dsh/native-tools.mjs +749 -0
- package/packages/dsh-plugin/dsh/runner.js +666 -0
- package/packages/dsh-plugin/dsh/wide-research.js +878 -0
- package/packages/dsh-plugin/package.json +38 -0
- package/packages/dsh-plugin/scripts/sync-local.sh +70 -0
- package/packages/dsh-plugin/test/client.test.js +406 -0
- package/packages/dsh-plugin/test/config.test.js +30 -0
- package/packages/dsh-plugin/test/diagnostics.test.js +28 -0
- package/packages/dsh-plugin/test/index.test.js +1382 -0
- package/references/query-templates.md +96 -0
- package/references/query-templates.yaml +118 -0
- package/scripts/ab_eval.py +219 -0
- package/scripts/ab_eval_p0p1.py +266 -0
- package/scripts/adaptive.py +294 -70
- package/scripts/answer.py +154 -0
- package/scripts/archive_run.py +591 -0
- package/scripts/argo_engine_registry.py +172 -0
- package/scripts/argo_paths.py +728 -0
- package/scripts/article.py +136 -0
- package/scripts/article_meta.py +131 -0
- package/scripts/arxiv_source.py +201 -0
- package/scripts/badcase_ledger.py +252 -0
- package/scripts/batch_probe.py +241 -0
- package/scripts/benchmark.py +19 -0
- package/scripts/bounded_run.py +144 -0
- package/scripts/browser_auth.py +323 -0
- package/scripts/budget.py +247 -0
- package/scripts/budget_cli.py +93 -0
- package/scripts/build_release_assets.sh +65 -0
- package/scripts/burstiness_detector.py +147 -0
- package/scripts/cache.py +1066 -67
- package/scripts/cache_guard.py +271 -0
- package/scripts/cache_key_vdom.py +56 -0
- package/scripts/cache_similarity.py +170 -0
- package/scripts/caliber_isolation.py +195 -0
- package/scripts/calibrate_lowquality.py +210 -0
- package/scripts/candidate_envelope.py +366 -0
- package/scripts/chrome_cdp.py +871 -0
- package/scripts/ci_drift_gate.py +90 -0
- package/scripts/circuit_breaker.py +389 -0
- package/scripts/citations.py +334 -0
- package/scripts/cite_cli.py +59 -0
- package/scripts/cjk_tokens.py +95 -0
- package/scripts/clarify.py +47 -24
- package/scripts/cli_io.py +240 -0
- package/scripts/config.py +683 -45
- package/scripts/config_cache.py +402 -0
- package/scripts/conn_pool.py +220 -0
- package/scripts/content_boundary.py +46 -0
- package/scripts/content_security.py +685 -0
- package/scripts/content_signals.py +751 -0
- package/scripts/crawl.py +74 -35
- package/scripts/cross_lingual_trace.py +293 -0
- package/scripts/daemon.py +866 -0
- package/scripts/digup_bridge.py +214 -0
- package/scripts/dispatch_result.py +98 -0
- package/scripts/engine_admission.py +366 -0
- package/scripts/engine_dispatch.py +982 -0
- package/scripts/engine_env.py +539 -0
- package/scripts/engine_failure.py +411 -0
- package/scripts/engine_families.py +738 -0
- package/scripts/engine_policy.py +218 -0
- package/scripts/engine_requires.py +190 -0
- package/scripts/engine_status.py +322 -0
- package/scripts/engine_validate.py +638 -0
- package/scripts/engines.py +799 -294
- package/scripts/engines_base.py +1521 -0
- package/scripts/engines_builders.py +310 -0
- package/scripts/engines_builders_batch10.py +204 -0
- package/scripts/engines_builders_batch11.py +174 -0
- package/scripts/engines_builders_batch12.py +103 -0
- package/scripts/engines_builders_batch13.py +340 -0
- package/scripts/engines_builders_batch14.py +656 -0
- package/scripts/engines_builders_batch15.py +249 -0
- package/scripts/engines_builders_batch9.py +1468 -0
- package/scripts/engines_builders_cn.py +2283 -0
- package/scripts/engines_builders_data.py +2923 -0
- package/scripts/engines_builders_data_macro.py +775 -0
- package/scripts/engines_builders_feeds.py +255 -0
- package/scripts/engines_builders_intl.py +1239 -0
- package/scripts/engines_builders_keyless.py +111 -0
- package/scripts/engines_builders_search.py +440 -0
- package/scripts/engines_builders_tech.py +1359 -0
- package/scripts/evidence.py +620 -166
- package/scripts/evidence_loop.py +624 -0
- package/scripts/evidence_tier.py +298 -0
- package/scripts/except_sets.py +25 -0
- package/scripts/extract.py +55 -16
- package/scripts/fact_align.py +207 -0
- package/scripts/fact_crosscheck.py +230 -0
- package/scripts/fetch.py +25 -12
- package/scripts/fetch_browser.py +87 -0
- package/scripts/fetch_jsrun.py +122 -0
- package/scripts/fetch_quality.py +225 -0
- package/scripts/fetch_render_tinyfish.py +117 -0
- package/scripts/fetch_v3.py +1804 -0
- package/scripts/focus_extract.py +350 -0
- package/scripts/fulltext_store.py +169 -0
- package/scripts/fxtwitter_api.py +325 -0
- package/scripts/gen_engine_catalog.py +364 -0
- package/scripts/gen_native_tools.py +91 -0
- package/scripts/gen_routing_snapshot.py +195 -0
- package/scripts/health_check.py +140 -402
- package/scripts/health_probe.py +23 -8
- package/scripts/hot_state.py +134 -0
- package/scripts/http_client.py +1053 -0
- package/scripts/image_ops.py +430 -0
- package/scripts/image_vision/vision_probe.swift +126 -0
- package/scripts/install.ps1 +166 -0
- package/scripts/install.sh +221 -0
- package/scripts/job.py +1186 -0
- package/scripts/jump_resolver.py +235 -0
- package/scripts/lang_capability.py +150 -0
- package/scripts/lang_detect.py +289 -0
- package/scripts/lang_matrix_probe.py +329 -0
- package/scripts/lang_pref.py +311 -0
- package/scripts/link_source.py +340 -0
- package/scripts/local_image.py +970 -0
- package/scripts/local_seek.py +402 -0
- package/scripts/macro_countries.py +97 -0
- package/scripts/matrix_search_eval.py +1187 -0
- package/scripts/mcp_compact.py +234 -0
- package/scripts/mcp_diag.py +8 -3
- package/scripts/mcp_handlers.py +492 -0
- package/scripts/mcp_handlers_surface.py +157 -0
- package/scripts/mcp_launch.sh +124 -0
- package/scripts/mcp_local.py +241 -0
- package/scripts/mcp_server.py +125 -419
- package/scripts/mcp_setup.py +431 -0
- package/scripts/mcp_tools.py +331 -0
- package/scripts/mcp_transport.py +191 -0
- package/scripts/mcp_warm.py +127 -0
- package/scripts/min_version_scan.py +335 -0
- package/scripts/net_proxy.py +258 -0
- package/scripts/network_aware.py +152 -0
- package/scripts/output_transparency.py +200 -0
- package/scripts/paid_policy.py +98 -0
- package/scripts/paper.py +274 -0
- package/scripts/pdf_extract.py +518 -0
- package/scripts/phrase_filter.py +66 -0
- package/scripts/plan.py +422 -0
- package/scripts/postprocess_signals.py +150 -0
- package/scripts/pre-commit +48 -0
- package/scripts/query_enhance.py +157 -0
- package/scripts/query_operators.py +196 -0
- package/scripts/query_rewriter.py +116 -7
- package/scripts/query_signals.py +300 -0
- package/scripts/query_templates.py +167 -0
- package/scripts/query_understanding.py +411 -0
- package/scripts/query_variants.py +298 -0
- package/scripts/quota.py +633 -76
- package/scripts/rank_signals.py +775 -0
- package/scripts/ranking_eval.py +319 -0
- package/scripts/readability_extract.py +433 -0
- package/scripts/recompute.py +415 -0
- package/scripts/recovery.py +616 -0
- package/scripts/recovery_l0.py +160 -0
- package/scripts/recovery_runner.py +150 -0
- package/scripts/redskill/redskill_engine.py +332 -0
- package/scripts/regression_p0p1.py +318 -0
- package/scripts/replay_eval.py +658 -0
- package/scripts/rerank_tfidf.py +225 -0
- package/scripts/research.py +607 -403
- package/scripts/research_cli.py +361 -0
- package/scripts/research_dossier.py +492 -0
- package/scripts/research_expand.py +169 -0
- package/scripts/research_gates.py +272 -0
- package/scripts/research_report.py +209 -0
- package/scripts/research_strategy.py +37 -0
- package/scripts/research_work_packages.py +195 -0
- package/scripts/result_lang.py +311 -0
- package/scripts/retry_policy.py +76 -0
- package/scripts/robots_guard.py +259 -0
- package/scripts/route.py +921 -254
- package/scripts/route_bangs.py +40 -0
- package/scripts/route_cache.py +243 -0
- package/scripts/route_combo.py +917 -0
- package/scripts/route_domains.py +341 -0
- package/scripts/route_lang.py +605 -0
- package/scripts/route_log.py +63 -0
- package/scripts/route_policy.py +250 -0
- package/scripts/rrf_classic.py +66 -0
- package/scripts/screenshot.py +73 -0
- package/scripts/search.py +886 -406
- package/scripts/search_attribution.py +185 -0
- package/scripts/search_benchmark.py +407 -0
- package/scripts/search_cli.py +640 -0
- package/scripts/search_entry.py +395 -0
- package/scripts/search_output.py +690 -0
- package/scripts/search_pipeline.py +926 -0
- package/scripts/search_rank.py +991 -0
- package/scripts/search_types.py +129 -16
- package/scripts/seek_locator.py +68 -0
- package/scripts/semantic_evidence.py +391 -0
- package/scripts/serp_guard.py +336 -0
- package/scripts/serp_kill_log.py +74 -0
- package/scripts/single_flight.py +120 -0
- package/scripts/social_engines/__init__.py +10 -0
- package/scripts/social_engines/bilibili_engine.py +131 -0
- package/scripts/social_engines/hackernews_engine.py +113 -0
- package/scripts/social_engines/reddit_engine.py +172 -0
- package/scripts/social_engines/twitter_engine.py +397 -0
- package/scripts/social_engines/v2ex_engine.py +101 -0
- package/scripts/social_engines/weibo_engine.py +128 -0
- package/scripts/social_engines/xiaohongshu_engine.py +137 -0
- package/scripts/social_engines/zhihu_engine.py +139 -0
- package/scripts/social_research.py +201 -0
- package/scripts/sogou_weixin_browser.py +318 -0
- package/scripts/source_ledger.py +249 -0
- package/scripts/stage_timing.py +58 -0
- package/scripts/stats_cli.py +107 -0
- package/scripts/straggler_policy.py +60 -0
- package/scripts/stylometry_detector.py +231 -0
- package/scripts/sync_backends.py +353 -0
- package/scripts/sync_installs.py +29 -0
- package/scripts/template_cli.py +127 -0
- package/scripts/text_lexicon.py +72 -0
- package/scripts/tfidf_router.py +109 -37
- package/scripts/time_utils.py +233 -0
- package/scripts/topic_research_profiles.py +643 -0
- package/scripts/train.py +737 -0
- package/scripts/tweet.py +596 -0
- package/scripts/url_canon.py +217 -0
- package/scripts/url_optimize.py +56 -0
- package/scripts/url_safety.py +236 -0
- package/scripts/usage_log.py +116 -0
- package/scripts/usage_log_clean.py +113 -0
- package/scripts/v2ex_nodes.py +311 -0
- package/scripts/variant_recall.py +188 -0
- package/scripts/watch.py +205 -0
- package/scripts/wechat_article.py +258 -0
- package/scripts/wide_research.py +447 -0
- package/scripts/wx.py +349 -0
- package/scripts/x_article_fetch.py +141 -0
- package/scripts/x_search.py +234 -0
- package/scripts/x_trending.py +189 -0
- package/scripts/yaml_load.py +67 -0
- package/scripts/zhihu_quota.py +139 -0
- package/sub-skills/ego-search/SKILL.md +253 -0
- package/sub-skills/ego-search/scripts/ego_search.py +800 -0
- package/sub-skills/ego-search/scripts/merge.py +226 -0
- package/sub-skills/ego-search/scripts/quality.py +64 -0
- package/sub-skills/ego-search/scripts/runtime.py +172 -0
- package/sub-skills/ego-search/scripts/safety.py +109 -0
- package/sub-skills/ego-search/scripts/serp_spec.py +109 -0
- package/sub-skills/ego-search/scripts/webbridge_adapter.py +374 -0
- package/sub-skills/js-run/SKILL.md +67 -0
- package/sub-skills/js-run/scripts/jsrun.py +112 -0
- package/sub-skills/js-run/scripts/shim.js +188 -0
- package/sub-skills/local-search/SKILL.md +50 -53
- package/sub-skills/local-search/bm25.py +186 -0
- package/sub-skills/local-search/cli_runner.py +331 -0
- package/sub-skills/local-search/config.yaml +152 -55
- package/sub-skills/local-search/engine_registry.py +91 -26
- package/sub-skills/local-search/{health_check.py → local_health_check.py} +140 -10
- package/sub-skills/local-search/local_search_adapter.py +13 -2
- package/sub-skills/local-search/parse_maps.yaml +40 -42
- package/sub-skills/local-search/parsers.py +328 -0
- package/sub-skills/local-search/search_v3.py +716 -242
- package/sub-skills/local-search/smart_router.py +47 -7
- package/sub-skills/local-seek/SKILL.md +110 -0
- package/sub-skills/local-seek/config/domains.yaml +20 -0
- package/sub-skills/local-seek/scripts/eval_rank.py +216 -0
- package/sub-skills/local-seek/scripts/eval_seek.py +224 -0
- package/sub-skills/local-seek/scripts/seek.py +1465 -0
- package/sub-skills/local-seek/scripts/seek_frecency.py +126 -0
- package/sub-skills/local-seek/scripts/seek_fts.py +430 -0
- package/sub-skills/local-seek/scripts/seek_rank.py +197 -0
package/scripts/wx.py
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""天气查询脚本(免 Key,纯标准库)。
|
|
3
|
+
|
|
4
|
+
能力:
|
|
5
|
+
- 双源并行:wttr.in(信息全:体感/湿度/云量/能见度)+ Open-Meteo
|
|
6
|
+
(结构化:中文 WMO 码 / 降水概率 / 空气质量),快者先返、结果融合去重
|
|
7
|
+
- 地理编码:Open-Meteo Geocoding API(免 Key),中文/拼音/英文城市名 → 坐标,
|
|
8
|
+
让 Open-Meteo 保底在任意地名输入下可用(此前仅坐标输入可保底)
|
|
9
|
+
- 空气质量:Open-Meteo Air Quality API(免 Key),当前 PM2.5/PM10 + 等级
|
|
10
|
+
- 天气描述中文化:wttr.in 英文 desc → 中文(WMO 常见词映射表)
|
|
11
|
+
- 输入:地点名(城市/机场码/邮编)或 "lat,lon",或两个位置参数 lat lon
|
|
12
|
+
|
|
13
|
+
输出:YAML 结果列表(当前天气 1 条 + 未来预报 N 条),供 cli 引擎解析。
|
|
14
|
+
字段:title/url/snippet/published_at。
|
|
15
|
+
|
|
16
|
+
数据源许可:wttr.in(Apache 2.0)、Open-Meteo 数据(CC BY 4.0,需署名)、
|
|
17
|
+
Open-Meteo Geocoding / Air Quality(同 Open-Meteo)。
|
|
18
|
+
|
|
19
|
+
用法:
|
|
20
|
+
python3 scripts/wx.py "上海"
|
|
21
|
+
python3 scripts/wx.py "31.23,121.47" --n 3
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import argparse
|
|
27
|
+
import json
|
|
28
|
+
import re
|
|
29
|
+
import sys
|
|
30
|
+
import urllib.parse as up
|
|
31
|
+
import urllib.request
|
|
32
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
33
|
+
from datetime import date
|
|
34
|
+
from net_proxy import open_url # 出口调度唯一入口(issue #13 同类修复)
|
|
35
|
+
|
|
36
|
+
_WTTR_BASE = "https://wttr.in/"
|
|
37
|
+
_OM_BASE = "https://api.open-meteo.com/v1/forecast"
|
|
38
|
+
_GEO_BASE = "https://geocoding-api.open-meteo.com/v1/search"
|
|
39
|
+
_AQI_BASE = "https://air-quality-api.open-meteo.com/v1/air-quality"
|
|
40
|
+
_HEADERS = {
|
|
41
|
+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
42
|
+
"Accept": "application/json",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
# WMO 天气代码 → 描述(Open-Meteo)
|
|
46
|
+
_WMO = {
|
|
47
|
+
0: "晴", 1: "基本晴", 2: "多云", 3: "阴",
|
|
48
|
+
45: "雾", 48: "雾凇", 51: "毛毛雨", 53: "小雨", 55: "中雨",
|
|
49
|
+
61: "小雨", 63: "中雨", 65: "大雨", 66: "冻雨", 67: "强冻雨",
|
|
50
|
+
71: "小雪", 73: "中雪", 75: "大雪", 77: "霰",
|
|
51
|
+
80: "阵雨", 81: "强阵雨", 82: "暴雨", 85: "阵雪", 86: "强阵雪",
|
|
52
|
+
95: "雷暴", 96: "雷暴伴冰雹", 99: "强雷暴伴冰雹",
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
# wttr.in 英文天气描述 → 中文(WMO 常见词,先转小写精确匹配)
|
|
56
|
+
_WEATHER_EN_ZH = {
|
|
57
|
+
"sunny": "晴", "clear": "晴", "clear sky": "晴",
|
|
58
|
+
"partly cloudy": "多云", "cloudy": "阴", "overcast": "阴",
|
|
59
|
+
"mist": "薄雾", "fog": "雾", "freezing fog": "冻雾", "haze": "霾",
|
|
60
|
+
"light drizzle": "毛毛雨", "drizzle": "毛毛雨", "heavy drizzle": "强毛毛雨",
|
|
61
|
+
"patchy light drizzle": "局部毛毛雨", "patchy light rain": "局部小雨",
|
|
62
|
+
"patchy rain nearby": "局部阵雨", "patchy rain possible": "局部阵雨",
|
|
63
|
+
"light rain": "小雨", "moderate rain": "中雨", "heavy rain": "大雨",
|
|
64
|
+
"light rain shower": "小阵雨", "moderate or heavy rain shower": "强阵雨",
|
|
65
|
+
"torrential rain shower": "暴雨", "moderate or heavy rain with thunder": "雷雨",
|
|
66
|
+
"patchy light rain with thunder": "雷阵雨", "thundery outbreaks possible": "雷阵雨",
|
|
67
|
+
"thunderstorm": "雷暴",
|
|
68
|
+
"light snow": "小雪", "moderate snow": "中雪", "heavy snow": "大雪",
|
|
69
|
+
"patchy snow nearby": "局部降雪", "light snow showers": "小阵雪",
|
|
70
|
+
"moderate or heavy snow showers": "强阵雪", "blowing snow": "吹雪",
|
|
71
|
+
"blizzard": "暴风雪", "patchy light snow with thunder": "雷阵雪",
|
|
72
|
+
"light freezing rain": "冻雨", "moderate or heavy freezing rain": "强冻雨",
|
|
73
|
+
"patchy freezing drizzle nearby": "局部冻毛毛雨", "freezing drizzle": "冻毛毛雨",
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _fetch(url: str, timeout: float = 8) -> str:
|
|
78
|
+
req = urllib.request.Request(url, headers=_HEADERS)
|
|
79
|
+
with open_url(req, timeout=timeout) as resp:
|
|
80
|
+
return resp.read().decode("utf-8", "replace")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
_WEATHER_WORDS = re.compile(
|
|
84
|
+
r"(天气|气温|温度|多少度|预报|怎么样|如何|下雨|降水|雪|晴|今[天日晚]|明[天日晚]|后[天日]|昨[天日]|"
|
|
85
|
+
r"当前|现在|实时|weather|forecast|temperature|today|tonight|tomorrow|now|current)",
|
|
86
|
+
re.IGNORECASE,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _parse_location(args: list[str]) -> str:
|
|
91
|
+
"""把位置参数归一为查询可用的片段。"""
|
|
92
|
+
if len(args) >= 2:
|
|
93
|
+
try:
|
|
94
|
+
lat, lon = float(args[0]), float(args[1])
|
|
95
|
+
return f"{lat},{lon}"
|
|
96
|
+
except ValueError:
|
|
97
|
+
pass
|
|
98
|
+
loc = args[0] if args else "Beijing"
|
|
99
|
+
loc = _WEATHER_WORDS.sub("", loc).strip()
|
|
100
|
+
return loc or "Beijing"
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _zh(desc: str) -> str:
|
|
104
|
+
"""wttr.in 英文天气描述 → 中文;未命中保留原文。"""
|
|
105
|
+
if not desc:
|
|
106
|
+
return ""
|
|
107
|
+
return _WEATHER_EN_ZH.get(desc.strip().lower(), desc)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _wttr(loc: str, query: str | None = None) -> list[dict]:
|
|
111
|
+
"""wttr.in 主源。标题用查询词(用户输入),避免 nearest_area 返回细分区域名。"""
|
|
112
|
+
q = (query or loc).strip() or loc
|
|
113
|
+
url = f"{_WTTR_BASE}{up.quote(loc)}?format=j1"
|
|
114
|
+
data = json.loads(_fetch(url))
|
|
115
|
+
area = (data.get("nearest_area") or [{}])[0]
|
|
116
|
+
country = (area.get("country") or [{}])[0].get("value", "")
|
|
117
|
+
cur = (data.get("current_condition") or [{}])[0]
|
|
118
|
+
desc = ((cur.get("weatherDesc") or [{}])[0].get("value", "") or "")
|
|
119
|
+
|
|
120
|
+
def cond_url():
|
|
121
|
+
return f"{_WTTR_BASE}{up.quote(loc)}"
|
|
122
|
+
|
|
123
|
+
rows = []
|
|
124
|
+
temp_c = cur.get("temp_C")
|
|
125
|
+
if temp_c is not None:
|
|
126
|
+
title = f"{q} 当前 {temp_c}°C {_zh(desc)}".strip()
|
|
127
|
+
parts = []
|
|
128
|
+
if cur.get("FeelsLikeC"):
|
|
129
|
+
parts.append(f"体感 {cur['FeelsLikeC']}°C")
|
|
130
|
+
if cur.get("humidity"):
|
|
131
|
+
parts.append(f"湿度 {cur['humidity']}%")
|
|
132
|
+
if cur.get("windspeedKmph"):
|
|
133
|
+
parts.append(f"风速 {cur['windspeedKmph']}km/h {cur.get('winddir16Point', '')}")
|
|
134
|
+
if cur.get("cloudcover"):
|
|
135
|
+
parts.append(f"云量 {cur['cloudcover']}%")
|
|
136
|
+
if cur.get("visibility"):
|
|
137
|
+
parts.append(f"能见度 {cur['visibility']}km")
|
|
138
|
+
if cur.get("observation_time"):
|
|
139
|
+
parts.append(f"观测 {cur['observation_time']}")
|
|
140
|
+
if country:
|
|
141
|
+
parts.append(country)
|
|
142
|
+
rows.append({
|
|
143
|
+
"title": title,
|
|
144
|
+
"url": cond_url(),
|
|
145
|
+
"snippet": " · ".join(p for p in parts if p),
|
|
146
|
+
"published_at": date.today().isoformat(),
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
for day in data.get("weather") or []:
|
|
150
|
+
d = day.get("date", "")
|
|
151
|
+
if not d:
|
|
152
|
+
continue
|
|
153
|
+
title = f"{q} {d} {day.get('mintempC', '?')}°C~{day.get('maxtempC', '?')}°C"
|
|
154
|
+
noon = next((h for h in (day.get("hourly") or []) if str(h.get("time", "")).startswith(("11", "12"))), None)
|
|
155
|
+
parts = []
|
|
156
|
+
if noon:
|
|
157
|
+
desc2 = ((noon.get("weatherDesc") or [{}])[0].get("value", ""))
|
|
158
|
+
if desc2:
|
|
159
|
+
parts.append(_zh(desc2))
|
|
160
|
+
if noon.get("tempC"):
|
|
161
|
+
parts.append(f"{noon['tempC']}°C")
|
|
162
|
+
if noon.get("humidity"):
|
|
163
|
+
parts.append(f"湿度 {noon['humidity']}%")
|
|
164
|
+
if noon.get("windspeedKmph"):
|
|
165
|
+
parts.append(f"风速 {noon['windspeedKmph']}km/h")
|
|
166
|
+
rows.append({
|
|
167
|
+
"title": title,
|
|
168
|
+
"url": cond_url(),
|
|
169
|
+
"snippet": " · ".join(parts) or day.get("weatherDesc", ""),
|
|
170
|
+
"published_at": d,
|
|
171
|
+
})
|
|
172
|
+
return rows
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _geocode(loc: str) -> dict | None:
|
|
176
|
+
"""Open-Meteo Geocoding:中文/拼音/英文城市名 → {lat, lon, name, country}。"""
|
|
177
|
+
params = {"name": loc, "count": 1, "language": "zh", "format": "json"}
|
|
178
|
+
url = f"{_GEO_BASE}?{up.urlencode(params)}"
|
|
179
|
+
try:
|
|
180
|
+
data = json.loads(_fetch(url, timeout=6))
|
|
181
|
+
except Exception:
|
|
182
|
+
return None
|
|
183
|
+
results = data.get("results") or []
|
|
184
|
+
if not results:
|
|
185
|
+
return None
|
|
186
|
+
r = results[0]
|
|
187
|
+
return {
|
|
188
|
+
"lat": r.get("latitude"), "lon": r.get("longitude"),
|
|
189
|
+
"name": r.get("name") or loc, "country": r.get("country") or "",
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _aqi_level(pm25: float | None) -> str:
|
|
194
|
+
"""PM2.5 日均浓度 → 中国标准等级(ug/m³)。"""
|
|
195
|
+
if pm25 is None:
|
|
196
|
+
return ""
|
|
197
|
+
if pm25 < 35:
|
|
198
|
+
return "优"
|
|
199
|
+
if pm25 < 75:
|
|
200
|
+
return "良"
|
|
201
|
+
if pm25 < 115:
|
|
202
|
+
return "轻度污染"
|
|
203
|
+
if pm25 < 150:
|
|
204
|
+
return "中度污染"
|
|
205
|
+
if pm25 < 250:
|
|
206
|
+
return "重度污染"
|
|
207
|
+
return "严重污染"
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _aqi(lat: float, lon: float) -> str | None:
|
|
211
|
+
"""Open-Meteo Air Quality:当前 PM2.5/PM10 + 等级。失败不阻塞预报。"""
|
|
212
|
+
params = {"latitude": lat, "longitude": lon,
|
|
213
|
+
"current": "pm10,pm2_5", "timezone": "auto"}
|
|
214
|
+
url = f"{_AQI_BASE}?{up.urlencode(params)}"
|
|
215
|
+
try:
|
|
216
|
+
data = json.loads(_fetch(url, timeout=6))
|
|
217
|
+
cur = data.get("current") or {}
|
|
218
|
+
except Exception:
|
|
219
|
+
return None
|
|
220
|
+
pm25 = cur.get("pm2_5")
|
|
221
|
+
pm10 = cur.get("pm10")
|
|
222
|
+
if pm25 is None and pm10 is None:
|
|
223
|
+
return None
|
|
224
|
+
parts = []
|
|
225
|
+
if pm25 is not None:
|
|
226
|
+
parts.append(f"PM2.5 {pm25:.1f}µg/m³")
|
|
227
|
+
if pm10 is not None:
|
|
228
|
+
parts.append(f"PM10 {pm10:.1f}µg/m³")
|
|
229
|
+
level = _aqi_level(pm25)
|
|
230
|
+
if level:
|
|
231
|
+
parts.append(level)
|
|
232
|
+
return " · ".join(parts)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def _open_meteo(loc: str) -> list[dict]:
|
|
236
|
+
"""Open-Meteo 保底源:坐标或地名(自动地理编码)。含降水概率与空气质量。"""
|
|
237
|
+
try:
|
|
238
|
+
lat, lon = (float(x) for x in loc.split(","))
|
|
239
|
+
label = loc
|
|
240
|
+
except (ValueError, AttributeError):
|
|
241
|
+
place = _geocode(loc)
|
|
242
|
+
if not place:
|
|
243
|
+
return []
|
|
244
|
+
lat, lon = place["lat"], place["lon"]
|
|
245
|
+
label = f"{place['name']}{(' ' + place['country']) if place.get('country') else ''}"
|
|
246
|
+
params = {
|
|
247
|
+
"latitude": lat, "longitude": lon, "current_weather": "true",
|
|
248
|
+
"timezone": "auto", "forecast_days": 3,
|
|
249
|
+
"daily": "temperature_2m_max,temperature_2m_min,precipitation_probability_max",
|
|
250
|
+
}
|
|
251
|
+
url = f"{_OM_BASE}?{up.urlencode(params)}"
|
|
252
|
+
# forecast 与 AQI 是同坐标的两个独立端点,geocode 之后并行取——
|
|
253
|
+
# 串行时 geocode→forecast→aqi 三跳各 ~1.4s(经代理实测 4.1s),并行后
|
|
254
|
+
# 墙钟 = geocode + max(forecast, aqi)。_aqi 内部自带容错(失败返回
|
|
255
|
+
# None),fetch 异常照旧冒泡给调用方的 _safe——两条失败语义都不变。
|
|
256
|
+
with ThreadPoolExecutor(max_workers=2) as ex:
|
|
257
|
+
f_om = ex.submit(_fetch, url)
|
|
258
|
+
f_aqi = ex.submit(_aqi, lat, lon)
|
|
259
|
+
data = json.loads(f_om.result())
|
|
260
|
+
aqi = f_aqi.result()
|
|
261
|
+
cur = data.get("current_weather") or {}
|
|
262
|
+
rows = []
|
|
263
|
+
if cur:
|
|
264
|
+
code = _WMO.get(cur.get("weathercode", -1), "未知")
|
|
265
|
+
row = {
|
|
266
|
+
"title": f"{label} 当前 {cur.get('temperature', '?')}°C {code}",
|
|
267
|
+
"url": "https://open-meteo.com/",
|
|
268
|
+
"snippet": f"风速 {cur.get('windspeed', '?')}km/h · 风向 {cur.get('winddirection', '?')}° · 观测 {cur.get('time', '')}",
|
|
269
|
+
"published_at": (cur.get("time") or "")[:10],
|
|
270
|
+
}
|
|
271
|
+
if aqi:
|
|
272
|
+
row["snippet"] += f" · {aqi}"
|
|
273
|
+
rows.append(row)
|
|
274
|
+
daily = data.get("daily") or {}
|
|
275
|
+
times = daily.get("time") or []
|
|
276
|
+
mx = daily.get("temperature_2m_max") or []
|
|
277
|
+
mn = daily.get("temperature_2m_min") or []
|
|
278
|
+
pp = daily.get("precipitation_probability_max") or []
|
|
279
|
+
for i, t in enumerate(times[:2]):
|
|
280
|
+
if i >= len(mx) or i >= len(mn):
|
|
281
|
+
break
|
|
282
|
+
parts = [f"{mn[i]}°C~{mx[i]}°C"]
|
|
283
|
+
if i < len(pp) and pp[i] is not None:
|
|
284
|
+
parts.append(f"降雨概率 {pp[i]}%")
|
|
285
|
+
rows.append({
|
|
286
|
+
"title": f"{label} {t} {' · '.join(parts)}",
|
|
287
|
+
"url": "https://open-meteo.com/",
|
|
288
|
+
"snippet": "Open-Meteo 预报",
|
|
289
|
+
"published_at": t,
|
|
290
|
+
})
|
|
291
|
+
return rows
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _safe(fn, *args, **kwargs):
|
|
295
|
+
"""包装:异常转 (空列表, 错误信息),保证并行线程内不抛。"""
|
|
296
|
+
try:
|
|
297
|
+
return fn(*args, **kwargs), None
|
|
298
|
+
except Exception as e: # noqa: BLE001
|
|
299
|
+
return [], str(e)
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _merge(wttr_rows: list[dict], om_rows: list[dict]) -> list[dict]:
|
|
303
|
+
"""融合去重:当前天气 wttr 优先(信息全),预报按日期 OM 优先(含降水概率)。"""
|
|
304
|
+
cur = None
|
|
305
|
+
if wttr_rows:
|
|
306
|
+
cur = wttr_rows[0]
|
|
307
|
+
elif om_rows:
|
|
308
|
+
cur = om_rows[0]
|
|
309
|
+
by_date: dict[str, dict] = {}
|
|
310
|
+
if om_rows:
|
|
311
|
+
for r in om_rows[1:]:
|
|
312
|
+
by_date.setdefault(r.get("published_at", ""), r)
|
|
313
|
+
if wttr_rows:
|
|
314
|
+
for r in wttr_rows[1:]:
|
|
315
|
+
by_date.setdefault(r.get("published_at", ""), r)
|
|
316
|
+
rows = [cur] if cur else []
|
|
317
|
+
rows += [by_date[d] for d in sorted(d for d in by_date if d)]
|
|
318
|
+
return rows
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def main() -> int:
|
|
322
|
+
parser = argparse.ArgumentParser(description="天气查询(免 Key,wttr.in + Open-Meteo 双源并行)")
|
|
323
|
+
parser.add_argument("loc", nargs="*", help="地点名,或 lat lon 两个位置参数")
|
|
324
|
+
parser.add_argument("--n", type=int, default=3, help="最多返回条数")
|
|
325
|
+
args = parser.parse_args()
|
|
326
|
+
|
|
327
|
+
loc = _parse_location(args.loc)
|
|
328
|
+
with ThreadPoolExecutor(max_workers=2) as ex:
|
|
329
|
+
f_w = ex.submit(_safe, _wttr, loc, loc)
|
|
330
|
+
f_o = ex.submit(_safe, _open_meteo, loc)
|
|
331
|
+
wttr_rows, w_err = f_w.result()
|
|
332
|
+
om_rows, o_err = f_o.result()
|
|
333
|
+
|
|
334
|
+
errors = [e for e in (w_err, o_err) if e]
|
|
335
|
+
rows = _merge(wttr_rows, om_rows)
|
|
336
|
+
if not rows:
|
|
337
|
+
detail = f" ({'; '.join(errors)})" if errors else ""
|
|
338
|
+
print(f"No weather data available.{detail}", file=sys.stderr)
|
|
339
|
+
return 1
|
|
340
|
+
for e in errors:
|
|
341
|
+
print(f"wx: {e}", file=sys.stderr)
|
|
342
|
+
|
|
343
|
+
import yaml
|
|
344
|
+
print(yaml.safe_dump(rows[: max(1, args.n)], allow_unicode=True, sort_keys=False))
|
|
345
|
+
return 0
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
if __name__ == "__main__":
|
|
349
|
+
sys.exit(main())
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""x_article_fetch.py — X Article 正文提取(Draft.js 重建)。
|
|
3
|
+
|
|
4
|
+
X Article 是 X 的长文功能,URL 格式 https://x.com/i/article/{id}。
|
|
5
|
+
FxTwitter API 对 X Article 返回 Draft.js blocks,需重建为 Markdown。
|
|
6
|
+
entityMap 里 MEDIA/MARKDOWN 原子块还原为带内嵌图片 Markdown。
|
|
7
|
+
|
|
8
|
+
为什么单独成模块:Draft.js 重建是纯函数逻辑,与 fetch_v3.py 的降级链
|
|
9
|
+
职责不同。拆出后 fetch_v3.py 只留调用点,祖父文件行数门禁可各自独立管理。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import json
|
|
15
|
+
import os
|
|
16
|
+
import re
|
|
17
|
+
import sys
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
_X_ARTICLE_URL_RE = re.compile(r"x\.com/i/article/(\d+)", re.I)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def is_x_article_url(url: str) -> bool:
|
|
24
|
+
"""URL 是否是 X Article 链接。"""
|
|
25
|
+
return bool(_X_ARTICLE_URL_RE.search(url or ""))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def reconstruct_draftjs(blocks: list[dict], entity_map: dict) -> str:
|
|
29
|
+
"""把 Draft.js blocks 重建为 Markdown 文本。
|
|
30
|
+
|
|
31
|
+
entityMap 里的原子块(IMAGE/VIDEO)还原为  内嵌语法,
|
|
32
|
+
链接实体还原为 [text](url) 语法,普通文本直接保留。
|
|
33
|
+
"""
|
|
34
|
+
if not blocks:
|
|
35
|
+
return ""
|
|
36
|
+
parts: list[str] = []
|
|
37
|
+
for block in blocks:
|
|
38
|
+
text = block.get("text", "") or ""
|
|
39
|
+
block_type = block.get("type", "unstyled")
|
|
40
|
+
if block_type == "atomic":
|
|
41
|
+
entity_ranges = block.get("entityRanges") or []
|
|
42
|
+
for er in entity_ranges:
|
|
43
|
+
key = str(er.get("key", ""))
|
|
44
|
+
entity = entity_map.get(key, {})
|
|
45
|
+
etype = entity.get("type", "")
|
|
46
|
+
data = entity.get("data", {})
|
|
47
|
+
if etype == "IMAGE" and data.get("url"):
|
|
48
|
+
parts.append(f"")
|
|
49
|
+
elif etype == "VIDEO" and data.get("url"):
|
|
50
|
+
parts.append(f"[视频]({data['url']})")
|
|
51
|
+
continue
|
|
52
|
+
if block_type in ("unordered-list-item", "ordered-list-item"):
|
|
53
|
+
parts.append(f"- {text}")
|
|
54
|
+
continue
|
|
55
|
+
if block_type == "header-one":
|
|
56
|
+
parts.append(f"# {text}")
|
|
57
|
+
continue
|
|
58
|
+
if block_type == "header-two":
|
|
59
|
+
parts.append(f"## {text}")
|
|
60
|
+
continue
|
|
61
|
+
if block_type == "header-three":
|
|
62
|
+
parts.append(f"### {text}")
|
|
63
|
+
continue
|
|
64
|
+
if block_type == "blockquote":
|
|
65
|
+
parts.append(f"> {text}")
|
|
66
|
+
continue
|
|
67
|
+
if block_type == "code-block":
|
|
68
|
+
parts.append(f"```\n{text}\n```")
|
|
69
|
+
continue
|
|
70
|
+
entity_ranges = block.get("entityRanges") or []
|
|
71
|
+
if entity_ranges:
|
|
72
|
+
sorted_ranges = sorted(entity_ranges, key=lambda r: r.get("offset", 0))
|
|
73
|
+
last_end = 0
|
|
74
|
+
for er in sorted_ranges:
|
|
75
|
+
offset = er.get("offset", 0)
|
|
76
|
+
length = er.get("length", 0)
|
|
77
|
+
key = str(er.get("key", ""))
|
|
78
|
+
entity = entity_map.get(key, {})
|
|
79
|
+
etype = entity.get("type", "")
|
|
80
|
+
data = entity.get("data", {})
|
|
81
|
+
if offset > last_end:
|
|
82
|
+
parts.append(text[last_end:offset])
|
|
83
|
+
if etype == "LINK" and data.get("url"):
|
|
84
|
+
parts.append(f"[{text[offset:offset+length]}]({data['url']})")
|
|
85
|
+
else:
|
|
86
|
+
parts.append(text[offset:offset+length])
|
|
87
|
+
last_end = offset + length
|
|
88
|
+
if last_end < len(text):
|
|
89
|
+
parts.append(text[last_end:])
|
|
90
|
+
else:
|
|
91
|
+
parts.append(text)
|
|
92
|
+
return "\n\n".join(parts)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def fetch_x_article(url: str, max_chars: int = 8000,
|
|
96
|
+
timeout: float = 15.0) -> dict | None:
|
|
97
|
+
"""通过 FxTwitter API 获取 X Article 的 Draft.js blocks 并重建为 Markdown。
|
|
98
|
+
|
|
99
|
+
成功返回 result dict(含 url/content/title/success/fetch_method),失败返回 None。
|
|
100
|
+
"""
|
|
101
|
+
m = _X_ARTICLE_URL_RE.search(url or "")
|
|
102
|
+
if not m:
|
|
103
|
+
return None
|
|
104
|
+
article_id = m.group(1)
|
|
105
|
+
try:
|
|
106
|
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
107
|
+
from fxtwitter_api import http_get
|
|
108
|
+
api_url = f"https://api.fxtwitter.com/i/article/{article_id}"
|
|
109
|
+
body, _ = http_get(api_url, timeout=timeout)
|
|
110
|
+
data = json.loads(body.decode("utf-8"))
|
|
111
|
+
if not isinstance(data, dict):
|
|
112
|
+
return None
|
|
113
|
+
blocks = data.get("blocks") or []
|
|
114
|
+
entity_map = data.get("entityMap") or {}
|
|
115
|
+
if not blocks:
|
|
116
|
+
return None
|
|
117
|
+
content = reconstruct_draftjs(blocks, entity_map)
|
|
118
|
+
if not content.strip():
|
|
119
|
+
return None
|
|
120
|
+
return {
|
|
121
|
+
"url": url,
|
|
122
|
+
"content": content[:max_chars],
|
|
123
|
+
"title": _extract_title(content),
|
|
124
|
+
"length": min(len(content), max_chars),
|
|
125
|
+
"success": True,
|
|
126
|
+
"error": None,
|
|
127
|
+
"fetch_method": "x_article",
|
|
128
|
+
}
|
|
129
|
+
except Exception:
|
|
130
|
+
return None
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _extract_title(content: str) -> str:
|
|
134
|
+
"""从 Markdown 正文提取标题(第一个 # 标题或前 100 字)。"""
|
|
135
|
+
for line in content.split("\n"):
|
|
136
|
+
line = line.strip()
|
|
137
|
+
if line.startswith("# "):
|
|
138
|
+
return line[2:].strip()[:200]
|
|
139
|
+
if line and not line.startswith("![]"):
|
|
140
|
+
return line[:100]
|
|
141
|
+
return ""
|