api-everything 0.5.0__py3-none-any.whl

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.
Files changed (281) hide show
  1. api_everything/__init__.py +3 -0
  2. api_everything/__main__.py +8 -0
  3. api_everything/check.py +168 -0
  4. api_everything/chrome_cmd.py +131 -0
  5. api_everything/cli.py +803 -0
  6. api_everything/config.py +321 -0
  7. api_everything/delivery.py +518 -0
  8. api_everything/delivery_git.py +94 -0
  9. api_everything/driver/__init__.py +37 -0
  10. api_everything/driver/attach.py +95 -0
  11. api_everything/driver/base.py +80 -0
  12. api_everything/driver/chrome.py +127 -0
  13. api_everything/driver/profile.py +84 -0
  14. api_everything/engine.py +381 -0
  15. api_everything/errors.py +23 -0
  16. api_everything/hooks.py +52 -0
  17. api_everything/login.py +95 -0
  18. api_everything/mcp_bridge.py +249 -0
  19. api_everything/metrics.py +87 -0
  20. api_everything/mock_site/__init__.py +5 -0
  21. api_everything/mock_site/__main__.py +18 -0
  22. api_everything/mock_site/app.py +262 -0
  23. api_everything/pipeline.py +934 -0
  24. api_everything/pool.py +553 -0
  25. api_everything/ratelimit.py +67 -0
  26. api_everything/recipe/__init__.py +6 -0
  27. api_everything/recipe/actions.py +56 -0
  28. api_everything/recipe/jsonpath.py +22 -0
  29. api_everything/recipe/loader.py +142 -0
  30. api_everything/recipe/params.py +120 -0
  31. api_everything/recipe/schema.py +235 -0
  32. api_everything/recipes/alignmentforum/feed.yaml +15 -0
  33. api_everything/recipes/alpine_docs/search.yaml +32 -0
  34. api_everything/recipes/angular_docs/search.yaml +32 -0
  35. api_everything/recipes/anthropic_status/summary.yaml +17 -0
  36. api_everything/recipes/apis_guru/providers.yaml +15 -0
  37. api_everything/recipes/arbeitnow/jobs.yaml +20 -0
  38. api_everything/recipes/arin_rdap/ip.yaml +24 -0
  39. api_everything/recipes/artifacthub/browse.yaml +22 -0
  40. api_everything/recipes/artifacthub/search.yaml +24 -0
  41. api_everything/recipes/astro_docs/search.yaml +32 -0
  42. api_everything/recipes/atlassian_status/summary.yaml +17 -0
  43. api_everything/recipes/babel_docs/search.yaml +32 -0
  44. api_everything/recipes/binance/price.yaml +24 -0
  45. api_everything/recipes/bugzilla/bugs.yaml +24 -0
  46. api_everything/recipes/bundlephobia/size.yaml +24 -0
  47. api_everything/recipes/caddy_docs/search.yaml +32 -0
  48. api_everything/recipes/caniuse/search.yaml +24 -0
  49. api_everything/recipes/capacitor_docs/search.yaml +32 -0
  50. api_everything/recipes/cdnjs/library.yaml +22 -0
  51. api_everything/recipes/cdnjs/search.yaml +27 -0
  52. api_everything/recipes/certspotter/issuances.yaml +27 -0
  53. api_everything/recipes/cheerio_docs/search.yaml +32 -0
  54. api_everything/recipes/circleci_status/summary.yaml +17 -0
  55. api_everything/recipes/cisa_kev/catalog.yaml +17 -0
  56. api_everything/recipes/civitai/models.yaml +26 -0
  57. api_everything/recipes/cloudflare_status/summary.yaml +17 -0
  58. api_everything/recipes/cncf_landscape/projects.yaml +20 -0
  59. api_everything/recipes/cncf_landscape/ui.yaml +18 -0
  60. api_everything/recipes/coinbase/rates.yaml +24 -0
  61. api_everything/recipes/coingecko/trending.yaml +17 -0
  62. api_everything/recipes/conda_forge/package.yaml +25 -0
  63. api_everything/recipes/crates/categories.yaml +15 -0
  64. api_everything/recipes/crates/downloads.yaml +22 -0
  65. api_everything/recipes/crates/keywords.yaml +15 -0
  66. api_everything/recipes/crates/reverse_deps.yaml +22 -0
  67. api_everything/recipes/crates/search.yaml +24 -0
  68. api_everything/recipes/crates/summary.yaml +15 -0
  69. api_everything/recipes/crates/user.yaml +22 -0
  70. api_everything/recipes/crates/versions.yaml +22 -0
  71. api_everything/recipes/crossref/works.yaml +24 -0
  72. api_everything/recipes/cve_org/record.yaml +24 -0
  73. api_everything/recipes/debian_sources/search.yaml +24 -0
  74. api_everything/recipes/deps_dev/npm_spa.yaml +22 -0
  75. api_everything/recipes/deps_dev/package.yaml +23 -0
  76. api_everything/recipes/devdocs/index.yaml +18 -0
  77. api_everything/recipes/devto/articles.yaml +26 -0
  78. api_everything/recipes/devto/latest.yaml +18 -0
  79. api_everything/recipes/devto/tags.yaml +18 -0
  80. api_everything/recipes/devto/top.yaml +18 -0
  81. api_everything/recipes/digitalocean_status/summary.yaml +17 -0
  82. api_everything/recipes/digitalocean_status/ui.yaml +15 -0
  83. api_everything/recipes/discord_status/summary.yaml +17 -0
  84. api_everything/recipes/dns_google/resolve.yaml +24 -0
  85. api_everything/recipes/drizzle_docs/search.yaml +32 -0
  86. api_everything/recipes/echojs/latest.yaml +17 -0
  87. api_everything/recipes/ecosystems/package.yaml +25 -0
  88. api_everything/recipes/endoflife/product.yaml +24 -0
  89. api_everything/recipes/epss/score.yaml +24 -0
  90. api_everything/recipes/exchangerate/latest.yaml +24 -0
  91. api_everything/recipes/firefox_addons/search.yaml +26 -0
  92. api_everything/recipes/fly_status/summary.yaml +17 -0
  93. api_everything/recipes/formik_docs/search.yaml +32 -0
  94. api_everything/recipes/frankfurter/rates.yaml +24 -0
  95. api_everything/recipes/gcp_status/incidents.yaml +20 -0
  96. api_everything/recipes/github/explore.yaml +35 -0
  97. api_everything/recipes/github/gists.yaml +18 -0
  98. api_everything/recipes/github/issues.yaml +30 -0
  99. api_everything/recipes/github/license.yaml +23 -0
  100. api_everything/recipes/github/org.yaml +22 -0
  101. api_everything/recipes/github/releases.yaml +30 -0
  102. api_everything/recipes/github/repo.yaml +28 -0
  103. api_everything/recipes/github/user.yaml +22 -0
  104. api_everything/recipes/github_advisories/list.yaml +20 -0
  105. api_everything/recipes/github_status/summary.yaml +17 -0
  106. api_everything/recipes/github_status/ui.yaml +15 -0
  107. api_everything/recipes/gitlab/issues.yaml +24 -0
  108. api_everything/recipes/gitlab/repo.yaml +21 -0
  109. api_everything/recipes/gitlab/search.yaml +26 -0
  110. api_everything/recipes/go_proxy/latest.yaml +24 -0
  111. api_everything/recipes/grafana_status/summary.yaml +17 -0
  112. api_everything/recipes/hackage/versions.yaml +24 -0
  113. api_everything/recipes/hackernews/ask.yaml +18 -0
  114. api_everything/recipes/hackernews/front_page.yaml +18 -0
  115. api_everything/recipes/hackernews/item.yaml +21 -0
  116. api_everything/recipes/hackernews/jobs.yaml +18 -0
  117. api_everything/recipes/hackernews/latest.yaml +25 -0
  118. api_everything/recipes/hackernews/search.yaml +34 -0
  119. api_everything/recipes/hackernews/show.yaml +18 -0
  120. api_everything/recipes/hashicorp_releases/list.yaml +27 -0
  121. api_everything/recipes/hashicorp_status/summary.yaml +17 -0
  122. api_everything/recipes/helm_docs/search.yaml +32 -0
  123. api_everything/recipes/heroku_status/current.yaml +17 -0
  124. api_everything/recipes/hexpm/search.yaml +27 -0
  125. api_everything/recipes/hf_papers/daily.yaml +20 -0
  126. api_everything/recipes/himalayas/jobs.yaml +17 -0
  127. api_everything/recipes/homebrew/cask.yaml +22 -0
  128. api_everything/recipes/homebrew/formula.yaml +26 -0
  129. api_everything/recipes/hono_docs/search.yaml +32 -0
  130. api_everything/recipes/httpbin/json.yaml +17 -0
  131. api_everything/recipes/httpbin/uuid.yaml +15 -0
  132. api_everything/recipes/huggingface/datasets.yaml +25 -0
  133. api_everything/recipes/huggingface/model_info.yaml +27 -0
  134. api_everything/recipes/huggingface/models.yaml +28 -0
  135. api_everything/recipes/huggingface/spaces.yaml +25 -0
  136. api_everything/recipes/inspirehep/papers.yaml +24 -0
  137. api_everything/recipes/ionic_docs/search.yaml +32 -0
  138. api_everything/recipes/ipify/ipv4.yaml +17 -0
  139. api_everything/recipes/ipwho/lookup.yaml +24 -0
  140. api_everything/recipes/jest_docs/search.yaml +32 -0
  141. api_everything/recipes/jsdelivr/gh.yaml +27 -0
  142. api_everything/recipes/jsdelivr/npm.yaml +24 -0
  143. api_everything/recipes/jsr/search.yaml +27 -0
  144. api_everything/recipes/launchdarkly_status/summary.yaml +17 -0
  145. api_everything/recipes/lemmy/hot.yaml +17 -0
  146. api_everything/recipes/lesswrong/feed.yaml +17 -0
  147. api_everything/recipes/llamaindex_docs/search.yaml +32 -0
  148. api_everything/recipes/lobsters/hottest.yaml +20 -0
  149. api_everything/recipes/luma/category.yaml +22 -0
  150. api_everything/recipes/luma/events.yaml +17 -0
  151. api_everything/recipes/mastodon/explore.yaml +26 -0
  152. api_everything/recipes/mastodon/tag.yaml +23 -0
  153. api_everything/recipes/mastodon/trends_tags.yaml +18 -0
  154. api_everything/recipes/mdn/search.yaml +24 -0
  155. api_everything/recipes/metacpan/search.yaml +24 -0
  156. api_everything/recipes/microlink/preview.yaml +24 -0
  157. api_everything/recipes/mikro_orm_docs/search.yaml +32 -0
  158. api_everything/recipes/mock/items.yaml +35 -0
  159. api_everything/recipes/mongodb_status/summary.yaml +17 -0
  160. api_everything/recipes/nager_date/holidays.yaml +32 -0
  161. api_everything/recipes/neon_docs/search.yaml +32 -0
  162. api_everything/recipes/nestjs_docs/search.yaml +32 -0
  163. api_everything/recipes/netlify_status/summary.yaml +17 -0
  164. api_everything/recipes/nextjs_docs/search.yaml +32 -0
  165. api_everything/recipes/nodejs_dist/releases.yaml +17 -0
  166. api_everything/recipes/npm/dist_tags.yaml +22 -0
  167. api_everything/recipes/npm/package.yaml +25 -0
  168. api_everything/recipes/npm/search.yaml +25 -0
  169. api_everything/recipes/npm_downloads/weekly.yaml +24 -0
  170. api_everything/recipes/npm_status/summary.yaml +17 -0
  171. api_everything/recipes/npmtrends/compare.yaml +22 -0
  172. api_everything/recipes/nuget/search.yaml +27 -0
  173. api_everything/recipes/nuxt_docs/search.yaml +32 -0
  174. api_everything/recipes/nvd/cve.yaml +22 -0
  175. api_everything/recipes/nvd/cves.yaml +24 -0
  176. api_everything/recipes/open_elevation/lookup.yaml +29 -0
  177. api_everything/recipes/openai_status/summary.yaml +17 -0
  178. api_everything/recipes/opencollective/search.yaml +22 -0
  179. api_everything/recipes/openfda/events.yaml +17 -0
  180. api_everything/recipes/openlibrary/search.yaml +24 -0
  181. api_everything/recipes/openmeteo/forecast.yaml +17 -0
  182. api_everything/recipes/openrouter/models.yaml +20 -0
  183. api_everything/recipes/openstreetmap/geocode.yaml +27 -0
  184. api_everything/recipes/openstreetmap/reverse.yaml +27 -0
  185. api_everything/recipes/osv/vuln.yaml +24 -0
  186. api_everything/recipes/packagist/search.yaml +27 -0
  187. api_everything/recipes/peps/list.yaml +17 -0
  188. api_everything/recipes/pinia_docs/search.yaml +32 -0
  189. api_everything/recipes/pkg_go_dev/browse.yaml +15 -0
  190. api_everything/recipes/pkg_go_dev/package.yaml +25 -0
  191. api_everything/recipes/playwright_docs/search.yaml +32 -0
  192. api_everything/recipes/pnpm_docs/search.yaml +32 -0
  193. api_everything/recipes/pokeapi/pokemon.yaml +24 -0
  194. api_everything/recipes/posthog_docs/search.yaml +32 -0
  195. api_everything/recipes/postman_echo/get.yaml +24 -0
  196. api_everything/recipes/postman_status/summary.yaml +17 -0
  197. api_everything/recipes/preact_docs/search.yaml +32 -0
  198. api_everything/recipes/pubdev/search.yaml +24 -0
  199. api_everything/recipes/pubmed/search.yaml +24 -0
  200. api_everything/recipes/puppeteer_docs/search.yaml +32 -0
  201. api_everything/recipes/pypi/package.yaml +24 -0
  202. api_everything/recipes/pypi/version.yaml +27 -0
  203. api_everything/recipes/quay/search.yaml +27 -0
  204. api_everything/recipes/quora/answers.yaml +46 -0
  205. api_everything/recipes/qwik_docs/search.yaml +32 -0
  206. api_everything/recipes/ray_docs/search.yaml +32 -0
  207. api_everything/recipes/rdap/domain.yaml +24 -0
  208. api_everything/recipes/react_docs/search.yaml +32 -0
  209. api_everything/recipes/react_hook_form_docs/search.yaml +32 -0
  210. api_everything/recipes/reactrouter_docs/search.yaml +32 -0
  211. api_everything/recipes/reddit/search.yaml +48 -0
  212. api_everything/recipes/reddit_status/summary.yaml +17 -0
  213. api_everything/recipes/remoteok/jobs.yaml +17 -0
  214. api_everything/recipes/render_status/summary.yaml +17 -0
  215. api_everything/recipes/repology/project.yaml +24 -0
  216. api_everything/recipes/ripe_stat/prefix.yaml +24 -0
  217. api_everything/recipes/rollup_docs/search.yaml +32 -0
  218. api_everything/recipes/rsbuild_docs/search.yaml +32 -0
  219. api_everything/recipes/rubygems/gem.yaml +22 -0
  220. api_everything/recipes/rubygems/search.yaml +27 -0
  221. api_everything/recipes/sendgrid_status/summary.yaml +17 -0
  222. api_everything/recipes/sequelize_docs/search.yaml +32 -0
  223. api_everything/recipes/shodan_internetdb/host.yaml +24 -0
  224. api_everything/recipes/shopify_status/summary.yaml +17 -0
  225. api_everything/recipes/sigstore_rekor/log.yaml +17 -0
  226. api_everything/recipes/slack_status/current.yaml +17 -0
  227. api_everything/recipes/spdx/licenses.yaml +17 -0
  228. api_everything/recipes/stackexchange/sites.yaml +20 -0
  229. api_everything/recipes/stackoverflow/answers.yaml +15 -0
  230. api_everything/recipes/stackoverflow/questions.yaml +18 -0
  231. api_everything/recipes/stackoverflow/search.yaml +27 -0
  232. api_everything/recipes/stackoverflow/tags.yaml +18 -0
  233. api_everything/recipes/strapi_docs/search.yaml +32 -0
  234. api_everything/recipes/strawberry_docs/search.yaml +32 -0
  235. api_everything/recipes/stripe_status/current.yaml +17 -0
  236. api_everything/recipes/substack/explore.yaml +22 -0
  237. api_everything/recipes/substack/search.yaml +31 -0
  238. api_everything/recipes/sunrise_sunset/times.yaml +29 -0
  239. api_everything/recipes/supabase_status/summary.yaml +17 -0
  240. api_everything/recipes/swr_docs/search.yaml +32 -0
  241. api_everything/recipes/tanstack_docs/search.yaml +32 -0
  242. api_everything/recipes/tc39/proposals.yaml +20 -0
  243. api_everything/recipes/techempower/results.yaml +15 -0
  244. api_everything/recipes/temporal_docs/search.yaml +32 -0
  245. api_everything/recipes/terraform_registry/browse.yaml +25 -0
  246. api_everything/recipes/terraform_registry/search.yaml +24 -0
  247. api_everything/recipes/testing_library_docs/search.yaml +32 -0
  248. api_everything/recipes/tvmaze/shows.yaml +24 -0
  249. api_everything/recipes/twilio_status/summary.yaml +17 -0
  250. api_everything/recipes/upstash_docs/search.yaml +32 -0
  251. api_everything/recipes/valibot_docs/search.yaml +32 -0
  252. api_everything/recipes/vercel_status/summary.yaml +17 -0
  253. api_everything/recipes/vercel_status/ui.yaml +15 -0
  254. api_everything/recipes/vue_docs/search.yaml +32 -0
  255. api_everything/recipes/w3c/specs.yaml +17 -0
  256. api_everything/recipes/wayback/available.yaml +24 -0
  257. api_everything/recipes/webdriverio_docs/search.yaml +32 -0
  258. api_everything/recipes/webpack_docs/search.yaml +32 -0
  259. api_everything/recipes/wikidata/entity.yaml +22 -0
  260. api_everything/recipes/wikidata/search.yaml +24 -0
  261. api_everything/recipes/wikimedia_commons/search.yaml +25 -0
  262. api_everything/recipes/wikipedia/search.yaml +25 -0
  263. api_everything/recipes/wikipedia/summary.yaml +33 -0
  264. api_everything/recipes/wordpress_plugins/search.yaml +24 -0
  265. api_everything/recipes/wordpress_themes/search.yaml +24 -0
  266. api_everything/recipes/worldbank/indicator.yaml +17 -0
  267. api_everything/recipes/zenodo/records.yaml +24 -0
  268. api_everything/recipes/zippopotam/in.yaml +22 -0
  269. api_everything/recipes/zippopotam/lookup.yaml +29 -0
  270. api_everything/recipes/zoom_status/summary.yaml +17 -0
  271. api_everything/record.py +327 -0
  272. api_everything/record_session.py +364 -0
  273. api_everything/repair.py +717 -0
  274. api_everything/repair_ai.py +194 -0
  275. api_everything/runlog.py +61 -0
  276. api_everything/server.py +323 -0
  277. api_everything-0.5.0.dist-info/METADATA +363 -0
  278. api_everything-0.5.0.dist-info/RECORD +281 -0
  279. api_everything-0.5.0.dist-info/WHEEL +4 -0
  280. api_everything-0.5.0.dist-info/entry_points.txt +2 -0
  281. api_everything-0.5.0.dist-info/licenses/LICENSE +202 -0
@@ -0,0 +1,3 @@
1
+ """Api-everything: serve a website's own internal JSON as an API."""
2
+
3
+ __version__ = "0.5.0"
@@ -0,0 +1,8 @@
1
+ """`python -m api_everything` runs the same command line as `api-everything`."""
2
+
3
+ import sys
4
+
5
+ from api_everything.cli import main
6
+
7
+ if __name__ == "__main__":
8
+ sys.exit(main())
@@ -0,0 +1,168 @@
1
+ """Live checks: run a recipe with its sample parameters and test the result.
2
+
3
+ A check passes only if the run succeeds AND every assertion in the recipe's `check:` block
4
+ holds, so a site that still answers 200 but changed its data shape is caught. Checks run one
5
+ after another, never in parallel, to stay polite to the sites.
6
+
7
+ If a daemon is already running, checks go through it (so they share its Chrome and its profile
8
+ locks, queue and rate limiter). Otherwise a private pool is started just for the checks.
9
+ Live checks are never run by hosted CI: they run on your own machine, by hand or on a schedule.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import os
15
+ import subprocess
16
+ import time
17
+ from collections.abc import Awaitable, Callable, Iterable
18
+ from dataclasses import dataclass
19
+ from urllib.parse import urlsplit
20
+
21
+ import httpx
22
+
23
+ from api_everything.config import Config
24
+ from api_everything.engine import RunResult
25
+ from api_everything.errors import RunError
26
+ from api_everything.hooks import load_hooks
27
+ from api_everything.pool import Pool
28
+ from api_everything.recipe import Recipe
29
+ from api_everything.recipe.loader import LoadedRecipes
30
+
31
+ Runner = Callable[[Recipe], Awaitable[RunResult]]
32
+ _LOCAL_HOSTS = {"127.0.0.1", "localhost", "::1"}
33
+
34
+
35
+ class CheckSelectionError(Exception):
36
+ """The user asked for recipes that do not exist, or for nothing at all."""
37
+
38
+
39
+ @dataclass
40
+ class CheckResult:
41
+ recipe: str
42
+ ok: bool
43
+ detail: str
44
+ duration_ms: int
45
+
46
+
47
+ def is_local(recipe: Recipe) -> bool:
48
+ """True for recipes that point at this machine, such as the mock-site demo."""
49
+ return (urlsplit(recipe.url).hostname or "") in _LOCAL_HOSTS
50
+
51
+
52
+ def select_recipes(
53
+ loaded: LoadedRecipes,
54
+ names: Iterable[str],
55
+ *,
56
+ everything: bool,
57
+ include_local: bool,
58
+ include_experimental: bool = False,
59
+ ) -> list[Recipe]:
60
+ names = list(names)
61
+ if names and everything:
62
+ raise CheckSelectionError("give recipe names or --all, not both")
63
+ if not names and not everything:
64
+ raise CheckSelectionError("name the recipes to check, or use --all")
65
+ if names:
66
+ unknown = [name for name in names if name not in loaded.recipes]
67
+ if unknown:
68
+ raise CheckSelectionError(f"unknown recipe(s): {', '.join(unknown)}")
69
+ return [loaded.recipes[name] for name in names]
70
+ chosen = [loaded.recipes[name] for name in sorted(loaded.recipes)]
71
+ if not include_local:
72
+ chosen = [r for r in chosen if not is_local(r)]
73
+ if not include_experimental:
74
+ chosen = [r for r in chosen if r.status != "experimental"]
75
+ return chosen
76
+
77
+
78
+ async def check_recipes(recipes: Iterable[Recipe], run: Runner) -> list[CheckResult]:
79
+ results: list[CheckResult] = []
80
+ for recipe in recipes:
81
+ started = time.perf_counter()
82
+ try:
83
+ result = await run(recipe)
84
+ except RunError as err:
85
+ ok, detail = False, f"{err.code}: {err.message}"
86
+ else:
87
+ failures = [
88
+ problem
89
+ for expectation in recipe.check.expect
90
+ for problem in expectation.failures(result.data)
91
+ ]
92
+ ok, detail = not failures, "; ".join(failures) or "ok"
93
+ results.append(
94
+ CheckResult(recipe.name, ok, detail, int((time.perf_counter() - started) * 1000))
95
+ )
96
+ return results
97
+
98
+
99
+ def daemon_base_url(config: Config) -> str:
100
+ """Where a running daemon would be listening, as a client should address it."""
101
+ host = config.server.host
102
+ if host in ("0.0.0.0", "::", ""):
103
+ host = "127.0.0.1"
104
+ if ":" in host: # a bare IPv6 literal needs brackets in a URL
105
+ host = f"[{host}]"
106
+ return f"http://{host}:{config.server.port}"
107
+
108
+
109
+ async def daemon_is_running(client: httpx.AsyncClient, base_url: str) -> bool:
110
+ try:
111
+ response = await client.get(f"{base_url}/healthz", timeout=1.0)
112
+ except httpx.HTTPError:
113
+ return False
114
+ return response.status_code == 200 and response.json().get("status") == "ok"
115
+
116
+
117
+ def daemon_runner(client: httpx.AsyncClient, base_url: str, token: str | None) -> Runner:
118
+ """A runner that asks a running daemon, so checks share its browser and limits."""
119
+ headers = {"Authorization": f"Bearer {token}"} if token else {}
120
+
121
+ async def run(recipe: Recipe) -> RunResult:
122
+ try:
123
+ response = await client.get(
124
+ f"{base_url}/v1/run/{recipe.name}",
125
+ params=recipe.check.params,
126
+ headers=headers,
127
+ timeout=120.0,
128
+ )
129
+ except httpx.HTTPError as exc:
130
+ raise RunError(
131
+ "daemon_unreachable", f"could not reach the daemon: {type(exc).__name__}"
132
+ ) from exc
133
+ body = response.json()
134
+ if response.status_code != 200:
135
+ error = body.get("error", {})
136
+ raise RunError(
137
+ error.get("code", "daemon_error"),
138
+ error.get("message", f"the daemon answered {response.status_code}"),
139
+ http_status=response.status_code,
140
+ meta=body.get("meta"),
141
+ )
142
+ return RunResult(data=body["data"], meta=body.get("meta", {}))
143
+
144
+ return run
145
+
146
+
147
+ async def run_locally(config: Config, loaded: LoadedRecipes, recipes: list[Recipe]):
148
+ """Check with a private pool (no daemon running): start Chrome, run, shut it down."""
149
+ pool = Pool(config)
150
+ hooks = load_hooks(config.hooks_dir, loaded.recipes.values())
151
+ await pool.start()
152
+ try:
153
+
154
+ async def run(recipe: Recipe) -> RunResult:
155
+ return await pool.run(recipe, recipe.check.params, hook=hooks.get(recipe.name))
156
+
157
+ return await check_recipes(recipes, run)
158
+ finally:
159
+ await pool.stop()
160
+
161
+
162
+ def notify(command: str, failed: list[str]) -> int:
163
+ """Run the operator's own notification command, telling it which recipes failed."""
164
+ env = dict(os.environ, API_EVERYTHING_FAILED_RECIPES=",".join(failed))
165
+ try:
166
+ return subprocess.run(command, shell=True, env=env, timeout=30, check=False).returncode
167
+ except subprocess.TimeoutExpired:
168
+ return 124
@@ -0,0 +1,131 @@
1
+ """`api-everything chrome <name>`: start the Chrome that an attached profile connects to.
2
+
3
+ It opens a normal visible Chrome on the profile's own folder, listening for the DevTools
4
+ protocol on this machine only, at the port in the profile's `attach` address. You sign in by
5
+ hand and leave it open; the daemon attaches to it. The daemon never sees your credentials.
6
+
7
+ Chrome 136 and later ignores remote debugging on the default profile, so this Chrome always
8
+ uses a dedicated folder. That is also why it cannot be your everyday Chrome.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import asyncio
14
+ import socket
15
+ import subprocess
16
+ from pathlib import Path
17
+
18
+ import httpx
19
+
20
+ from api_everything.config import Config, ConfigError
21
+ from api_everything.login import profile_lock_pid
22
+
23
+ CHROME_PATHS = [
24
+ "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
25
+ "/usr/bin/google-chrome",
26
+ "/usr/bin/google-chrome-stable",
27
+ ]
28
+ START_TIMEOUT_SECONDS = 20.0
29
+
30
+
31
+ class ChromeCommandError(Exception):
32
+ """Chrome cannot be started for this profile. The message says what to do."""
33
+
34
+
35
+ def find_chrome() -> str | None:
36
+ for path in CHROME_PATHS:
37
+ if Path(path).exists():
38
+ return path
39
+ return None
40
+
41
+
42
+ def chrome_args(chrome: str, directory: Path, port: int, *, headless: bool = False) -> list[str]:
43
+ args = [
44
+ chrome,
45
+ f"--remote-debugging-port={port}",
46
+ "--remote-debugging-address=127.0.0.1", # this machine only, never the network
47
+ f"--user-data-dir={directory}",
48
+ "--no-first-run",
49
+ "--no-default-browser-check",
50
+ ]
51
+ if headless:
52
+ args.append("--headless=new")
53
+ return args
54
+
55
+
56
+ async def cdp_answering(url: str) -> bool:
57
+ """True when a Chrome is answering the DevTools protocol at this address."""
58
+ try:
59
+ async with httpx.AsyncClient() as client:
60
+ resp = await client.get(f"{url}/json/version", timeout=1.5)
61
+ return resp.status_code == 200 and "webSocketDebuggerUrl" in resp.json()
62
+ except (httpx.HTTPError, ValueError):
63
+ return False
64
+
65
+
66
+ def _port_taken(port: int) -> bool:
67
+ with socket.socket() as sock:
68
+ return sock.connect_ex(("127.0.0.1", port)) == 0
69
+
70
+
71
+ async def start_chrome(
72
+ config: Config,
73
+ name: str,
74
+ *,
75
+ headless: bool = False,
76
+ chrome_path: str | None = None,
77
+ say=print,
78
+ ) -> Path:
79
+ """Start Chrome for an attached profile and return once it answers. Leaves it running."""
80
+ try:
81
+ attach = config.profile_attach(name)
82
+ directory = config.profile_dir(name)
83
+ except ConfigError as exc:
84
+ raise ChromeCommandError(str(exc)) from exc
85
+ if attach is None:
86
+ raise ChromeCommandError(
87
+ f"profile {name!r} is not set up to attach. Add it to your config file:\n"
88
+ f" profiles:\n {name}:\n attach: http://127.0.0.1:9222"
89
+ )
90
+ if await cdp_answering(attach.url):
91
+ say(f"Chrome for profile {name!r} is already running at {attach.url}.")
92
+ return directory
93
+
94
+ holder = profile_lock_pid(directory)
95
+ if holder is not None:
96
+ raise ChromeCommandError(
97
+ f"profile {name!r} ({directory}) is open in a Chrome (pid {holder}) that was not "
98
+ "started with remote debugging. Close that window, then run this again."
99
+ )
100
+ if _port_taken(attach.port):
101
+ raise ChromeCommandError(
102
+ f"port {attach.port} is already in use by another program. Choose another port "
103
+ f"in the attach address of profile {name!r}."
104
+ )
105
+ chrome = chrome_path or find_chrome()
106
+ if chrome is None:
107
+ raise ChromeCommandError(
108
+ "Google Chrome was not found. Install it, then run this again."
109
+ )
110
+
111
+ directory.mkdir(parents=True, exist_ok=True)
112
+ process = subprocess.Popen( # noqa: S603 - a fixed argument list, no shell
113
+ chrome_args(chrome, directory, attach.port, headless=headless),
114
+ stdout=subprocess.DEVNULL,
115
+ stderr=subprocess.DEVNULL,
116
+ start_new_session=True, # outlives this command, so you can close the terminal
117
+ )
118
+ loop = asyncio.get_running_loop()
119
+ deadline = loop.time() + START_TIMEOUT_SECONDS
120
+ while loop.time() < deadline:
121
+ if process.poll() is not None:
122
+ raise ChromeCommandError(
123
+ f"Chrome exited straight away (code {process.returncode}). Is another Chrome "
124
+ "using this profile?"
125
+ )
126
+ if await cdp_answering(attach.url):
127
+ say(f"Chrome is running for profile {name!r} at {attach.url} ({directory}).")
128
+ return directory
129
+ await asyncio.sleep(0.3)
130
+ process.terminate()
131
+ raise ChromeCommandError(f"Chrome did not start answering at {attach.url} in time.")