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,95 @@
1
+ """A driver that attaches to a Chrome you started yourself, over the DevTools protocol.
2
+
3
+ You start that Chrome with `api-everything chrome <name>` (a normal, visible Chrome on its own
4
+ profile folder, listening on this machine only), sign in by hand, and leave it open. The daemon
5
+ connects to it, gives each run its own tab in the profile's single shared context, and when it
6
+ stops it only disconnects: your Chrome and its tabs stay open.
7
+
8
+ This is not a way to drive your everyday Chrome: Chrome 136 and later ignores remote debugging
9
+ on its default profile, so the Chrome you attach to is always a separate, dedicated one.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import contextlib
15
+ from collections.abc import AsyncIterator
16
+ from contextlib import asynccontextmanager
17
+
18
+ from playwright.async_api import Browser, BrowserContext, Playwright, async_playwright
19
+ from playwright.async_api import Error as PlaywrightError
20
+
21
+ from api_everything.driver.base import BrowserDriver, Lease
22
+
23
+
24
+ class BrowserNotRunning(Exception):
25
+ """No Chrome is answering at the attach address. `str(exc)` says how to start one."""
26
+
27
+
28
+ class ChromeAttachDriver(BrowserDriver):
29
+ def __init__(self, url: str, profile: str) -> None:
30
+ self._url = url
31
+ self._profile = profile
32
+ self._playwright: Playwright | None = None
33
+ self._browser: Browser | None = None
34
+ self._context: BrowserContext | None = None
35
+
36
+ async def start(self) -> None:
37
+ self._playwright = await async_playwright().start()
38
+ try:
39
+ self._browser = await self._playwright.chromium.connect_over_cdp(
40
+ self._url, timeout=5_000
41
+ )
42
+ # The profile's own context: its cookies are the login the user made by hand.
43
+ if not self._browser.contexts:
44
+ raise BrowserNotRunning(self._not_running("it has no browser context"))
45
+ self._context = self._browser.contexts[0]
46
+ except BrowserNotRunning:
47
+ await self._abandon()
48
+ raise
49
+ except Exception as exc:
50
+ await self._abandon()
51
+ if isinstance(exc, PlaywrightError):
52
+ raise BrowserNotRunning(self._not_running("nothing answered there")) from exc
53
+ raise
54
+
55
+ def _not_running(self, why: str) -> str:
56
+ return (
57
+ f"no Chrome is attached for profile {self._profile!r} at {self._url} ({why}). "
58
+ f"Start it with `api-everything chrome {self._profile}` and leave it open"
59
+ )
60
+
61
+ async def _abandon(self) -> None:
62
+ playwright, self._playwright = self._playwright, None
63
+ self._browser = self._context = None
64
+ if playwright is not None:
65
+ await playwright.stop()
66
+
67
+ async def stop(self) -> None:
68
+ """Disconnect. Never closes the user's Chrome or the tabs it already had."""
69
+ browser, playwright = self._browser, self._playwright
70
+ self._browser = self._playwright = self._context = None
71
+ try:
72
+ if browser is not None:
73
+ with contextlib.suppress(PlaywrightError):
74
+ await browser.close() # for an attached browser this only disconnects
75
+ finally:
76
+ if playwright is not None:
77
+ await playwright.stop()
78
+
79
+ def is_alive(self) -> bool:
80
+ return self._browser is not None and self._browser.is_connected()
81
+
82
+ @asynccontextmanager
83
+ async def lease_context(self) -> AsyncIterator[BrowserContext]:
84
+ if self._context is None:
85
+ raise RuntimeError("driver is not started: call start() or use 'async with'")
86
+ yield self._context # shared on purpose; never closed per run
87
+
88
+ async def open_lease(self) -> Lease:
89
+ if self._context is None:
90
+ raise RuntimeError("driver is not started: call start() or use 'async with'")
91
+ return Lease(self._context, await self._context.new_page())
92
+
93
+ async def close_lease(self, lease: Lease) -> None:
94
+ with contextlib.suppress(PlaywrightError): # only the run's own tab, never the user's
95
+ await lease.page.close()
@@ -0,0 +1,80 @@
1
+ """The driver interface: a source of browsers.
2
+
3
+ A driver owns a browser process and hands out Playwright contexts. The real-Chrome drivers
4
+ ship in v0.1; stealth Chromium and CDP-attach drivers arrive in v0.2 behind this same
5
+ interface, so the pool and engine never care which one is in use.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from abc import ABC, abstractmethod
11
+ from collections.abc import AsyncIterator
12
+ from contextlib import AbstractAsyncContextManager, asynccontextmanager
13
+ from dataclasses import dataclass
14
+ from types import TracebackType
15
+
16
+ from playwright.async_api import BrowserContext, Page
17
+
18
+
19
+ @dataclass
20
+ class Lease:
21
+ """What one run uses: a browser context and a page (tab) already open in it.
22
+
23
+ Opening a page costs far more than navigating one (hundreds of milliseconds against tens),
24
+ so the pool opens leases ahead of time and hands them out ready to use.
25
+ """
26
+
27
+ context: BrowserContext
28
+ page: Page
29
+
30
+
31
+ class BrowserDriver(ABC):
32
+ @abstractmethod
33
+ async def start(self) -> None:
34
+ """Launch the browser. Call once before handing out contexts."""
35
+
36
+ @abstractmethod
37
+ async def stop(self) -> None:
38
+ """Close the browser and release everything it holds."""
39
+
40
+ @abstractmethod
41
+ def lease_context(self) -> AbstractAsyncContextManager[BrowserContext]:
42
+ """The context one run uses, released when the block ends.
43
+
44
+ A driver that gives each run its own isolated context closes it afterwards. A driver
45
+ built on one persistent profile hands out its single shared context and leaves it
46
+ open, so the run only owns the tab it opens in it.
47
+ """
48
+
49
+ @abstractmethod
50
+ async def open_lease(self) -> Lease:
51
+ """Open a lease now: a context and a blank page, ready for one run."""
52
+
53
+ @abstractmethod
54
+ async def close_lease(self, lease: Lease) -> None:
55
+ """Release a lease. Never raises: a browser that is already gone is fine."""
56
+
57
+ @asynccontextmanager
58
+ async def lease(self) -> AsyncIterator[Lease]:
59
+ """Open a lease for one run and release it afterwards."""
60
+ lease = await self.open_lease()
61
+ try:
62
+ yield lease
63
+ finally:
64
+ await self.close_lease(lease)
65
+
66
+ def is_alive(self) -> bool:
67
+ """False once the browser has died (crashed, killed or closed under us)."""
68
+ return True
69
+
70
+ async def __aenter__(self) -> BrowserDriver:
71
+ await self.start()
72
+ return self
73
+
74
+ async def __aexit__(
75
+ self,
76
+ exc_type: type[BaseException] | None,
77
+ exc: BaseException | None,
78
+ tb: TracebackType | None,
79
+ ) -> None:
80
+ await self.stop()
@@ -0,0 +1,127 @@
1
+ """The shared-lane driver: one non-persistent browser, a fresh isolated context per run.
2
+
3
+ Two flavours share this class and differ only in which browser they launch:
4
+
5
+ - `ChromeDriver` launches the user's installed Google Chrome (Playwright `channel="chrome"`),
6
+ in Chrome's new headless mode. It needs no extra download when Chrome is installed.
7
+ - `ChromiumDriver` launches Playwright's own managed Chromium (the headless shell). It needs
8
+ one download (`playwright install --only-shell chromium`) but not Google Chrome, which is
9
+ what a Linux server or CI machine usually has.
10
+
11
+ Every run gets its own isolated context (separate cookies and storage) from one shared
12
+ browser process. Neither flavour hides that it is automated.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import contextlib
18
+ from collections.abc import AsyncIterator
19
+ from contextlib import asynccontextmanager
20
+
21
+ from playwright.async_api import (
22
+ Browser,
23
+ BrowserContext,
24
+ Playwright,
25
+ async_playwright,
26
+ )
27
+ from playwright.async_api import Error as PlaywrightError
28
+
29
+ from api_everything.driver.base import BrowserDriver, Lease
30
+
31
+ CHROMIUM_INSTALL_COMMAND = "uv run playwright install --only-shell chromium"
32
+
33
+ # Playwright reports a missing browser as a plain Error. These phrases are how it words it:
34
+ # "Executable doesn't exist at ..." for its own downloads, "Chromium distribution 'chrome' is
35
+ # not found at ..." for an installed channel such as Google Chrome.
36
+ _MISSING_MARKERS = ("Executable doesn't exist", "is not found at")
37
+
38
+
39
+ class BrowserNotInstalled(Exception):
40
+ """The browser to launch is not on this machine. `hint` says exactly how to get it.
41
+
42
+ Retrying cannot help, so callers fail fast instead of backing off and trying again.
43
+ """
44
+
45
+ def __init__(self, browser: str, hint: str) -> None:
46
+ super().__init__(f"{browser} is not installed. {hint}")
47
+ self.browser = browser
48
+ self.hint = hint
49
+
50
+
51
+ def is_missing_browser(exc: BaseException) -> bool:
52
+ """True when a Playwright launch error means the browser binary is not there."""
53
+ return isinstance(exc, PlaywrightError) and any(m in str(exc) for m in _MISSING_MARKERS)
54
+
55
+
56
+ class ChromeDriver(BrowserDriver):
57
+ # `None` means Playwright's own Chromium; a name such as "chrome" means an installed browser.
58
+ channel: str | None = "chrome"
59
+ browser_name = "Google Chrome"
60
+ install_hint = (
61
+ "Install Google Chrome, or set `browser: chromium` in the config to use the managed "
62
+ f"Chromium instead (then run `{CHROMIUM_INSTALL_COMMAND}`)."
63
+ )
64
+
65
+ def __init__(self, *, headless: bool = True) -> None:
66
+ self._headless = headless
67
+ self._playwright: Playwright | None = None
68
+ self._browser: Browser | None = None
69
+
70
+ async def start(self) -> None:
71
+ self._playwright = await async_playwright().start()
72
+ try:
73
+ self._browser = await self._playwright.chromium.launch(
74
+ channel=self.channel, headless=self._headless
75
+ )
76
+ except Exception as exc:
77
+ await self._playwright.stop()
78
+ self._playwright = None
79
+ if is_missing_browser(exc):
80
+ raise BrowserNotInstalled(self.browser_name, self.install_hint) from exc
81
+ raise
82
+
83
+ async def stop(self) -> None:
84
+ browser, playwright = self._browser, self._playwright
85
+ self._browser = self._playwright = None
86
+ try:
87
+ if browser is not None:
88
+ await browser.close()
89
+ finally:
90
+ if playwright is not None:
91
+ await playwright.stop()
92
+
93
+ def is_alive(self) -> bool:
94
+ return self._browser is not None and self._browser.is_connected()
95
+
96
+ @asynccontextmanager
97
+ async def lease_context(self) -> AsyncIterator[BrowserContext]:
98
+ if self._browser is None:
99
+ raise RuntimeError("driver is not started: call start() or use 'async with'")
100
+ context = await self._browser.new_context()
101
+ try:
102
+ yield context
103
+ finally:
104
+ await context.close()
105
+
106
+ async def open_lease(self) -> Lease:
107
+ if self._browser is None:
108
+ raise RuntimeError("driver is not started: call start() or use 'async with'")
109
+ context = await self._browser.new_context()
110
+ try:
111
+ return Lease(context, await context.new_page())
112
+ except BaseException:
113
+ with contextlib.suppress(PlaywrightError):
114
+ await context.close()
115
+ raise
116
+
117
+ async def close_lease(self, lease: Lease) -> None:
118
+ with contextlib.suppress(PlaywrightError): # the browser may already be gone
119
+ await lease.context.close()
120
+
121
+
122
+ class ChromiumDriver(ChromeDriver):
123
+ """Playwright's managed Chromium (headless shell) instead of installed Google Chrome."""
124
+
125
+ channel = None
126
+ browser_name = "Playwright's managed Chromium"
127
+ install_hint = f"Run `{CHROMIUM_INSTALL_COMMAND}` once, then start again."
@@ -0,0 +1,84 @@
1
+ """A real-Chrome driver on one persistent profile, for `session: profile:<name>` recipes.
2
+
3
+ The profile directory holds a login the operator made by hand. Chrome allows only one
4
+ process per profile directory, and a persistent profile has exactly one browser context, so
5
+ every run shares that context and just opens its own tab. The pool therefore keeps a profile's
6
+ concurrency low (one at a time by default) and gives each profile its own queue.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import asyncio
12
+ import contextlib
13
+ from collections.abc import AsyncIterator
14
+ from contextlib import asynccontextmanager
15
+ from pathlib import Path
16
+
17
+ from playwright.async_api import BrowserContext, Playwright, async_playwright
18
+ from playwright.async_api import Error as PlaywrightError
19
+
20
+ from api_everything.driver.base import BrowserDriver, Lease
21
+
22
+
23
+ class ChromeProfileDriver(BrowserDriver):
24
+ def __init__(self, user_data_dir: Path, *, headless: bool = True) -> None:
25
+ self._dir = user_data_dir
26
+ self._headless = headless
27
+ self._playwright: Playwright | None = None
28
+ self._context: BrowserContext | None = None
29
+ self._alive = False
30
+ self._closed = asyncio.Event()
31
+
32
+ async def start(self) -> None:
33
+ self._dir.mkdir(parents=True, exist_ok=True)
34
+ self._playwright = await async_playwright().start()
35
+ try:
36
+ self._context = await self._playwright.chromium.launch_persistent_context(
37
+ str(self._dir), channel="chrome", headless=self._headless
38
+ )
39
+ except Exception:
40
+ await self._playwright.stop()
41
+ self._playwright = None
42
+ raise
43
+ self._alive = True
44
+ # Playwright closes a persistent context when its browser exits for any reason.
45
+ self._context.on("close", lambda _context: self._mark_dead())
46
+
47
+ def _mark_dead(self) -> None:
48
+ self._alive = False
49
+ self._closed.set()
50
+
51
+ async def wait_closed(self) -> None:
52
+ """Return once the browser has closed (the user closed the window, or it crashed)."""
53
+ await self._closed.wait()
54
+
55
+ async def stop(self) -> None:
56
+ context, playwright = self._context, self._playwright
57
+ self._context = self._playwright = None
58
+ self._alive = False
59
+ try:
60
+ if context is not None:
61
+ with contextlib.suppress(Exception): # it may already be gone
62
+ await context.close()
63
+ finally:
64
+ if playwright is not None:
65
+ await playwright.stop()
66
+
67
+ def is_alive(self) -> bool:
68
+ return self._alive
69
+
70
+ @asynccontextmanager
71
+ async def lease_context(self) -> AsyncIterator[BrowserContext]:
72
+ if self._context is None:
73
+ raise RuntimeError("driver is not started: call start() or use 'async with'")
74
+ yield self._context # shared on purpose; never closed per run
75
+
76
+ async def open_lease(self) -> Lease:
77
+ if self._context is None:
78
+ raise RuntimeError("driver is not started: call start() or use 'async with'")
79
+ # The context is the profile's single shared one; the run owns only its tab.
80
+ return Lease(self._context, await self._context.new_page())
81
+
82
+ async def close_lease(self, lease: Lease) -> None:
83
+ with contextlib.suppress(PlaywrightError):
84
+ await lease.page.close()