google-cse-migrator 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google_cse_migrator-1.0.0/LICENSE +21 -0
- google_cse_migrator-1.0.0/PKG-INFO +168 -0
- google_cse_migrator-1.0.0/README.md +145 -0
- google_cse_migrator-1.0.0/migrate-cse.mjs +159 -0
- google_cse_migrator-1.0.0/migrate_cse.py +190 -0
- google_cse_migrator-1.0.0/package.json +15 -0
- google_cse_migrator-1.0.0/pyproject.toml +42 -0
- google_cse_migrator-1.0.0/test_migrate_cse.py +140 -0
- google_cse_migrator-1.0.0/tests/fixture/.env.example +1 -0
- google_cse_migrator-1.0.0/tests/fixture/node_modules/pkg/index.js +1 -0
- google_cse_migrator-1.0.0/tests/fixture/src/plain.txt +1 -0
- google_cse_migrator-1.0.0/tests/fixture/src/search.js +9 -0
- google_cse_migrator-1.0.0/tests/fixture/src/search.py +18 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SpecStream SAS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: google-cse-migrator
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Find Google Custom Search JSON API calls before the January 1, 2027 shutdown and move them to a drop in endpoint that returns the same JSON.
|
|
5
|
+
Project-URL: Homepage, https://specstream.ai/tools/google-cse-migrator
|
|
6
|
+
Project-URL: Search API, https://specstream.ai/search-api
|
|
7
|
+
Project-URL: Pricing, https://specstream.ai/pricing
|
|
8
|
+
Project-URL: Source, https://github.com/Specstreamai/google-cse-shutdown-migrator
|
|
9
|
+
Project-URL: Issues, https://github.com/Specstreamai/google-cse-shutdown-migrator/issues
|
|
10
|
+
Author-email: SpecStream <support@specstream.ai>
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: codemod,custom-search-json-api,customsearch,google-cse,google-custom-search,migration,programmable-search-engine,search-api,specstream
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Indexing/Search
|
|
20
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
21
|
+
Requires-Python: >=3.8
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# Google Custom Search shutdown migrator
|
|
25
|
+
|
|
26
|
+
[](https://specstream.ai/tools/backlink-checker?domain=specstream.ai)
|
|
27
|
+
|
|
28
|
+
Google ends the Custom Search JSON API for existing customers on **January 1, 2027**. After that date every call to `www.googleapis.com/customsearch/v1` stops answering.
|
|
29
|
+
|
|
30
|
+
This tool finds those calls in your codebase, shows the exact diff, and with `--apply` moves them to [SpecStream](https://specstream.ai/tools/google-cse-migrator), which answers the same request in Google's own response shape. Your parsing code stays as it is: `items[]`, `items[].link`, `title`, `snippet`, `displayLink`, `searchInformation.totalResults` and `queries.nextPage` all come back where your code already reads them.
|
|
31
|
+
|
|
32
|
+
No dependencies and no network calls. Install it from PyPI or npm, or run the single file from a clone.
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pipx install google-cse-migrator # or: pip install google-cse-migrator
|
|
38
|
+
cd your-project
|
|
39
|
+
google-cse-migrator . # dry run: lists every call and prints the diff
|
|
40
|
+
google-cse-migrator . --apply # rewrite the endpoint URLs in place
|
|
41
|
+
google-cse-migrator . --json # JSON report for CI
|
|
42
|
+
|
|
43
|
+
npx @specstream/google-cse-migrator . # the same tool on Node.js 18+, nothing to install
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
PyPI: [google-cse-migrator](https://pypi.org/project/google-cse-migrator/). npm: [@specstream/google-cse-migrator](https://www.npmjs.com/package/@specstream/google-cse-migrator).
|
|
47
|
+
|
|
48
|
+
## Run it from a clone
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
git clone https://github.com/Specstreamai/google-cse-shutdown-migrator
|
|
52
|
+
cd your-project
|
|
53
|
+
|
|
54
|
+
# Dry run: lists every call and prints the diff. Writes nothing.
|
|
55
|
+
python3 ../google-cse-shutdown-migrator/migrate_cse.py .
|
|
56
|
+
|
|
57
|
+
# Same thing in Node.js 18+
|
|
58
|
+
node ../google-cse-shutdown-migrator/migrate-cse.mjs .
|
|
59
|
+
|
|
60
|
+
# Rewrite the endpoint URLs in place
|
|
61
|
+
python3 ../google-cse-shutdown-migrator/migrate_cse.py . --apply
|
|
62
|
+
|
|
63
|
+
# JSON report for CI
|
|
64
|
+
python3 ../google-cse-shutdown-migrator/migrate_cse.py . --json
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Example output:
|
|
68
|
+
|
|
69
|
+
```text
|
|
70
|
+
src/search.py
|
|
71
|
+
L5 [endpoint] https://www.googleapis.com/customsearch/v1
|
|
72
|
+
L6 [endpoint] https://customsearch.googleapis.com/customsearch/v1/siterestrict
|
|
73
|
+
L17 [python-google-api-client] service = build("customsearch", "v1", developerKey=os.environ["CSE_KEY"])
|
|
74
|
+
-> googleapiclient build('customsearch', ...): call the REST endpoint directly, GET https://api.specstream.ai/customsearch/v1?key=SPECSTREAM_KEY&q=..., the response keeps Google's shape (items[], searchInformation, queries.nextPage).
|
|
75
|
+
L18 [cse-list-call] return service.cse().list(q=q, cx=os.environ["CSE_CX"]).execute()
|
|
76
|
+
-> Custom Search client call: same response fields at https://api.specstream.ai/customsearch/v1.
|
|
77
|
+
--- a/src/search.py
|
|
78
|
+
+++ b/src/search.py
|
|
79
|
+
@@ -2,8 +2,8 @@
|
|
80
|
+
import requests
|
|
81
|
+
from googleapiclient.discovery import build
|
|
82
|
+
|
|
83
|
+
-URL = "https://www.googleapis.com/customsearch/v1"
|
|
84
|
+
-SITE_URL = "https://customsearch.googleapis.com/customsearch/v1/siterestrict"
|
|
85
|
+
+URL = "https://api.specstream.ai/customsearch/v1"
|
|
86
|
+
+SITE_URL = "https://api.specstream.ai/customsearch/v1/siterestrict"
|
|
87
|
+
OTHER = "https://www.googleapis.com/youtube/v3/search"
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
4 endpoint URL(s), 3 client library call(s) in 3 file(s).
|
|
92
|
+
Would rewrite 3 file(s). Run again with --apply to write.
|
|
93
|
+
Use a SpecStream API key in key= (or X-Goog-Api-Key, or a Bearer token): a Google key returns 401. cx is accepted and ignored. Get a key with $2 free credit, no card: https://specstream.ai/tools/google-cse-migrator
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## What it changes
|
|
97
|
+
|
|
98
|
+
| Found | What happens |
|
|
99
|
+
| --- | --- |
|
|
100
|
+
| `https://www.googleapis.com/customsearch/v1` | rewritten to `https://api.specstream.ai/customsearch/v1` |
|
|
101
|
+
| `https://customsearch.googleapis.com/customsearch/v1` | rewritten to `https://api.specstream.ai/customsearch/v1` |
|
|
102
|
+
| `.../customsearch/v1/siterestrict` | rewritten, the path is kept |
|
|
103
|
+
| `build("customsearch", "v1", ...)` in google-api-python-client | reported with the change to make |
|
|
104
|
+
| `google.customsearch("v1")` in googleapis for Node.js | reported: pass `rootUrl: "https://api.specstream.ai/"` |
|
|
105
|
+
| `.cse().list(...)` calls | reported |
|
|
106
|
+
| Any other `googleapis.com` URL (YouTube, Maps, a bare base URL) | left alone |
|
|
107
|
+
|
|
108
|
+
Only the host changes, so `--apply` is safe to run twice. It skips `.git`, `node_modules`, virtualenvs, build output and files over 2 MB.
|
|
109
|
+
|
|
110
|
+
## After the rewrite
|
|
111
|
+
|
|
112
|
+
Put a SpecStream API key where the Google key was. The key goes in `key=`, in the `X-Goog-Api-Key` header or as a Bearer token. `cx` is accepted and ignored, so you can leave it in place.
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
curl "https://api.specstream.ai/customsearch/v1?key=$SPECSTREAM_KEY&q=eu+dora+register+of+information&num=10"
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Pricing: $1 per 1,000 calls of up to 10 results. Sign up gives $2 of free credit with no card. Paginate with `start=` exactly as before, and image search works with `searchType=image`.
|
|
119
|
+
|
|
120
|
+
Try a query in the browser first: **https://specstream.ai/tools/google-cse-migrator**
|
|
121
|
+
|
|
122
|
+
The full shutdown timeline and the field by field comparison: **https://specstream.ai/google-custom-search-api-shutdown**
|
|
123
|
+
|
|
124
|
+
## Use it in CI
|
|
125
|
+
|
|
126
|
+
```yaml
|
|
127
|
+
cse-check:
|
|
128
|
+
runs-on: ubuntu-latest
|
|
129
|
+
steps:
|
|
130
|
+
- uses: actions/checkout@v4
|
|
131
|
+
- run: |
|
|
132
|
+
curl -fsSLO https://raw.githubusercontent.com/Specstreamai/google-cse-shutdown-migrator/main/migrate_cse.py
|
|
133
|
+
python3 migrate_cse.py . --json | python3 -c "import json,sys; s=json.load(sys.stdin)['summary']; sys.exit(1 if s['endpoints'] else 0)"
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The job fails while any Google Custom Search endpoint is left in the code.
|
|
137
|
+
|
|
138
|
+
## Links
|
|
139
|
+
|
|
140
|
+
* Drop in endpoint, tried in the browser: [specstream.ai/tools/google-cse-migrator](https://specstream.ai/tools/google-cse-migrator)
|
|
141
|
+
* Search API reference: [specstream.ai/search-api](https://specstream.ai/search-api)
|
|
142
|
+
* Link Graph API (authority, backlinks, anchors): [specstream.ai/backlinks-api](https://specstream.ai/backlinks-api)
|
|
143
|
+
* Pricing: [specstream.ai/pricing](https://specstream.ai/pricing)
|
|
144
|
+
|
|
145
|
+
## Free tools and the MCP server
|
|
146
|
+
|
|
147
|
+
In the browser, no signup:
|
|
148
|
+
|
|
149
|
+
* [Token tax calculator](https://specstream.ai/tools/token-tax-calculator): how many tokens a search reply puts in front of your model, and what that costs. Measured script and results: [web-search-api-benchmarks/token-tax](https://github.com/Specstreamai/web-search-api-benchmarks/tree/main/token-tax).
|
|
150
|
+
* [Backlink checker](https://specstream.ai/tools/backlink-checker): authority, rank, referring domains and anchors for any domain.
|
|
151
|
+
* [DORA subprocessor check](https://specstream.ai/tools/dora-subprocessor-check): a SaaS vendor's subprocessors with their LEIs, as Register of Information rows.
|
|
152
|
+
* [Domain profile, stripe.com](https://specstream.ai/data/domain/stripe.com): the public page for one domain, with its authority, referring domains, rank and top backlinks.
|
|
153
|
+
|
|
154
|
+
For agents:
|
|
155
|
+
|
|
156
|
+
* MCP server: `https://api.specstream.ai/mcp` (Streamable HTTP, Bearer key or OAuth) gives an agent the same calls. Setup for each client: [specstream.ai/docs/mcp](https://specstream.ai/docs/mcp).
|
|
157
|
+
|
|
158
|
+
## Tests
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
python3 -m unittest -v test_migrate_cse
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
The suite checks both hosts, `siterestrict`, that other Google APIs are left alone, that a dry run writes nothing, that a second `--apply` changes nothing, and that the Python and Node.js tools produce the same report and the same files.
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
MIT. Made by [SpecStream](https://specstream.ai), the live web data engine.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Google Custom Search shutdown migrator
|
|
2
|
+
|
|
3
|
+
[](https://specstream.ai/tools/backlink-checker?domain=specstream.ai)
|
|
4
|
+
|
|
5
|
+
Google ends the Custom Search JSON API for existing customers on **January 1, 2027**. After that date every call to `www.googleapis.com/customsearch/v1` stops answering.
|
|
6
|
+
|
|
7
|
+
This tool finds those calls in your codebase, shows the exact diff, and with `--apply` moves them to [SpecStream](https://specstream.ai/tools/google-cse-migrator), which answers the same request in Google's own response shape. Your parsing code stays as it is: `items[]`, `items[].link`, `title`, `snippet`, `displayLink`, `searchInformation.totalResults` and `queries.nextPage` all come back where your code already reads them.
|
|
8
|
+
|
|
9
|
+
No dependencies and no network calls. Install it from PyPI or npm, or run the single file from a clone.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pipx install google-cse-migrator # or: pip install google-cse-migrator
|
|
15
|
+
cd your-project
|
|
16
|
+
google-cse-migrator . # dry run: lists every call and prints the diff
|
|
17
|
+
google-cse-migrator . --apply # rewrite the endpoint URLs in place
|
|
18
|
+
google-cse-migrator . --json # JSON report for CI
|
|
19
|
+
|
|
20
|
+
npx @specstream/google-cse-migrator . # the same tool on Node.js 18+, nothing to install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
PyPI: [google-cse-migrator](https://pypi.org/project/google-cse-migrator/). npm: [@specstream/google-cse-migrator](https://www.npmjs.com/package/@specstream/google-cse-migrator).
|
|
24
|
+
|
|
25
|
+
## Run it from a clone
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
git clone https://github.com/Specstreamai/google-cse-shutdown-migrator
|
|
29
|
+
cd your-project
|
|
30
|
+
|
|
31
|
+
# Dry run: lists every call and prints the diff. Writes nothing.
|
|
32
|
+
python3 ../google-cse-shutdown-migrator/migrate_cse.py .
|
|
33
|
+
|
|
34
|
+
# Same thing in Node.js 18+
|
|
35
|
+
node ../google-cse-shutdown-migrator/migrate-cse.mjs .
|
|
36
|
+
|
|
37
|
+
# Rewrite the endpoint URLs in place
|
|
38
|
+
python3 ../google-cse-shutdown-migrator/migrate_cse.py . --apply
|
|
39
|
+
|
|
40
|
+
# JSON report for CI
|
|
41
|
+
python3 ../google-cse-shutdown-migrator/migrate_cse.py . --json
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Example output:
|
|
45
|
+
|
|
46
|
+
```text
|
|
47
|
+
src/search.py
|
|
48
|
+
L5 [endpoint] https://www.googleapis.com/customsearch/v1
|
|
49
|
+
L6 [endpoint] https://customsearch.googleapis.com/customsearch/v1/siterestrict
|
|
50
|
+
L17 [python-google-api-client] service = build("customsearch", "v1", developerKey=os.environ["CSE_KEY"])
|
|
51
|
+
-> googleapiclient build('customsearch', ...): call the REST endpoint directly, GET https://api.specstream.ai/customsearch/v1?key=SPECSTREAM_KEY&q=..., the response keeps Google's shape (items[], searchInformation, queries.nextPage).
|
|
52
|
+
L18 [cse-list-call] return service.cse().list(q=q, cx=os.environ["CSE_CX"]).execute()
|
|
53
|
+
-> Custom Search client call: same response fields at https://api.specstream.ai/customsearch/v1.
|
|
54
|
+
--- a/src/search.py
|
|
55
|
+
+++ b/src/search.py
|
|
56
|
+
@@ -2,8 +2,8 @@
|
|
57
|
+
import requests
|
|
58
|
+
from googleapiclient.discovery import build
|
|
59
|
+
|
|
60
|
+
-URL = "https://www.googleapis.com/customsearch/v1"
|
|
61
|
+
-SITE_URL = "https://customsearch.googleapis.com/customsearch/v1/siterestrict"
|
|
62
|
+
+URL = "https://api.specstream.ai/customsearch/v1"
|
|
63
|
+
+SITE_URL = "https://api.specstream.ai/customsearch/v1/siterestrict"
|
|
64
|
+
OTHER = "https://www.googleapis.com/youtube/v3/search"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
4 endpoint URL(s), 3 client library call(s) in 3 file(s).
|
|
69
|
+
Would rewrite 3 file(s). Run again with --apply to write.
|
|
70
|
+
Use a SpecStream API key in key= (or X-Goog-Api-Key, or a Bearer token): a Google key returns 401. cx is accepted and ignored. Get a key with $2 free credit, no card: https://specstream.ai/tools/google-cse-migrator
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## What it changes
|
|
74
|
+
|
|
75
|
+
| Found | What happens |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| `https://www.googleapis.com/customsearch/v1` | rewritten to `https://api.specstream.ai/customsearch/v1` |
|
|
78
|
+
| `https://customsearch.googleapis.com/customsearch/v1` | rewritten to `https://api.specstream.ai/customsearch/v1` |
|
|
79
|
+
| `.../customsearch/v1/siterestrict` | rewritten, the path is kept |
|
|
80
|
+
| `build("customsearch", "v1", ...)` in google-api-python-client | reported with the change to make |
|
|
81
|
+
| `google.customsearch("v1")` in googleapis for Node.js | reported: pass `rootUrl: "https://api.specstream.ai/"` |
|
|
82
|
+
| `.cse().list(...)` calls | reported |
|
|
83
|
+
| Any other `googleapis.com` URL (YouTube, Maps, a bare base URL) | left alone |
|
|
84
|
+
|
|
85
|
+
Only the host changes, so `--apply` is safe to run twice. It skips `.git`, `node_modules`, virtualenvs, build output and files over 2 MB.
|
|
86
|
+
|
|
87
|
+
## After the rewrite
|
|
88
|
+
|
|
89
|
+
Put a SpecStream API key where the Google key was. The key goes in `key=`, in the `X-Goog-Api-Key` header or as a Bearer token. `cx` is accepted and ignored, so you can leave it in place.
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
curl "https://api.specstream.ai/customsearch/v1?key=$SPECSTREAM_KEY&q=eu+dora+register+of+information&num=10"
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Pricing: $1 per 1,000 calls of up to 10 results. Sign up gives $2 of free credit with no card. Paginate with `start=` exactly as before, and image search works with `searchType=image`.
|
|
96
|
+
|
|
97
|
+
Try a query in the browser first: **https://specstream.ai/tools/google-cse-migrator**
|
|
98
|
+
|
|
99
|
+
The full shutdown timeline and the field by field comparison: **https://specstream.ai/google-custom-search-api-shutdown**
|
|
100
|
+
|
|
101
|
+
## Use it in CI
|
|
102
|
+
|
|
103
|
+
```yaml
|
|
104
|
+
cse-check:
|
|
105
|
+
runs-on: ubuntu-latest
|
|
106
|
+
steps:
|
|
107
|
+
- uses: actions/checkout@v4
|
|
108
|
+
- run: |
|
|
109
|
+
curl -fsSLO https://raw.githubusercontent.com/Specstreamai/google-cse-shutdown-migrator/main/migrate_cse.py
|
|
110
|
+
python3 migrate_cse.py . --json | python3 -c "import json,sys; s=json.load(sys.stdin)['summary']; sys.exit(1 if s['endpoints'] else 0)"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The job fails while any Google Custom Search endpoint is left in the code.
|
|
114
|
+
|
|
115
|
+
## Links
|
|
116
|
+
|
|
117
|
+
* Drop in endpoint, tried in the browser: [specstream.ai/tools/google-cse-migrator](https://specstream.ai/tools/google-cse-migrator)
|
|
118
|
+
* Search API reference: [specstream.ai/search-api](https://specstream.ai/search-api)
|
|
119
|
+
* Link Graph API (authority, backlinks, anchors): [specstream.ai/backlinks-api](https://specstream.ai/backlinks-api)
|
|
120
|
+
* Pricing: [specstream.ai/pricing](https://specstream.ai/pricing)
|
|
121
|
+
|
|
122
|
+
## Free tools and the MCP server
|
|
123
|
+
|
|
124
|
+
In the browser, no signup:
|
|
125
|
+
|
|
126
|
+
* [Token tax calculator](https://specstream.ai/tools/token-tax-calculator): how many tokens a search reply puts in front of your model, and what that costs. Measured script and results: [web-search-api-benchmarks/token-tax](https://github.com/Specstreamai/web-search-api-benchmarks/tree/main/token-tax).
|
|
127
|
+
* [Backlink checker](https://specstream.ai/tools/backlink-checker): authority, rank, referring domains and anchors for any domain.
|
|
128
|
+
* [DORA subprocessor check](https://specstream.ai/tools/dora-subprocessor-check): a SaaS vendor's subprocessors with their LEIs, as Register of Information rows.
|
|
129
|
+
* [Domain profile, stripe.com](https://specstream.ai/data/domain/stripe.com): the public page for one domain, with its authority, referring domains, rank and top backlinks.
|
|
130
|
+
|
|
131
|
+
For agents:
|
|
132
|
+
|
|
133
|
+
* MCP server: `https://api.specstream.ai/mcp` (Streamable HTTP, Bearer key or OAuth) gives an agent the same calls. Setup for each client: [specstream.ai/docs/mcp](https://specstream.ai/docs/mcp).
|
|
134
|
+
|
|
135
|
+
## Tests
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
python3 -m unittest -v test_migrate_cse
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The suite checks both hosts, `siterestrict`, that other Google APIs are left alone, that a dry run writes nothing, that a second `--apply` changes nothing, and that the Python and Node.js tools produce the same report and the same files.
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
MIT. Made by [SpecStream](https://specstream.ai), the live web data engine.
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Find Google Custom Search JSON API calls in a codebase and move them to SpecStream.
|
|
3
|
+
//
|
|
4
|
+
// Google ends the Custom Search JSON API for existing customers on January 1, 2027.
|
|
5
|
+
// SpecStream answers the same request at https://api.specstream.ai/customsearch/v1 in
|
|
6
|
+
// Google's own response shape (items[], searchInformation, queries.nextPage), so only the
|
|
7
|
+
// host changes.
|
|
8
|
+
//
|
|
9
|
+
// node migrate-cse.mjs [PATH] dry run: report and diff, writes nothing
|
|
10
|
+
// node migrate-cse.mjs [PATH] --apply rewrite endpoint URLs in place
|
|
11
|
+
// node migrate-cse.mjs [PATH] --json machine readable report
|
|
12
|
+
//
|
|
13
|
+
// No dependencies beyond Node.js 18+.
|
|
14
|
+
import fs from "node:fs";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
|
|
18
|
+
export const VERSION = "1.0.0";
|
|
19
|
+
export const NEW_HOST = "https://api.specstream.ai";
|
|
20
|
+
|
|
21
|
+
const ENDPOINT_SRC =
|
|
22
|
+
"(?<scheme>https?://)(?<host>www\\.googleapis\\.com|customsearch\\.googleapis\\.com)" +
|
|
23
|
+
"(?<path>/customsearch/v1(?:/siterestrict)?)\\b";
|
|
24
|
+
|
|
25
|
+
const SDK_PATTERNS = [
|
|
26
|
+
["python-google-api-client", /build\(\s*['"]customsearch['"]/,
|
|
27
|
+
"googleapiclient build('customsearch', ...): call the REST endpoint directly, " +
|
|
28
|
+
"GET https://api.specstream.ai/customsearch/v1?key=SPECSTREAM_KEY&q=..., the response " +
|
|
29
|
+
"keeps Google's shape (items[], searchInformation, queries.nextPage)."],
|
|
30
|
+
["node-googleapis", /\bcustomsearch\(\s*(?:['"]v1['"]|\{)/,
|
|
31
|
+
"googleapis customsearch(...): pass rootUrl 'https://api.specstream.ai/' in the options, " +
|
|
32
|
+
"or call GET https://api.specstream.ai/customsearch/v1 directly."],
|
|
33
|
+
["cse-list-call", /\.cse\(\)\s*\.(?:list|siterestrict)\b|\bcse\.(?:list|siterestrict)\(/,
|
|
34
|
+
"Custom Search client call: same response fields at https://api.specstream.ai/customsearch/v1."],
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
export const KEY_NOTE =
|
|
38
|
+
"Use a SpecStream API key in key= (or X-Goog-Api-Key, or a Bearer token): a Google key " +
|
|
39
|
+
"returns 401. cx is accepted and ignored. Get a key with $2 free credit, no card: " +
|
|
40
|
+
"https://specstream.ai/tools/google-cse-migrator";
|
|
41
|
+
|
|
42
|
+
const SKIP_DIRS = new Set([".git", ".hg", ".svn", "node_modules", ".venv", "venv", "env",
|
|
43
|
+
"__pycache__", "dist", "build", ".next", ".nuxt", "target", "vendor", ".tox", ".mypy_cache",
|
|
44
|
+
".pytest_cache", "site-packages", "bower_components", "coverage"]);
|
|
45
|
+
const TEXT_EXTS = new Set([".py", ".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx", ".go", ".rb",
|
|
46
|
+
".php", ".java", ".kt", ".kts", ".scala", ".cs", ".rs", ".swift", ".dart", ".sh", ".bash",
|
|
47
|
+
".zsh", ".ps1", ".r", ".R", ".jl", ".lua", ".pl", ".ex", ".exs", ".clj", ".vue", ".svelte",
|
|
48
|
+
".astro", ".html", ".htm", ".json", ".yaml", ".yml", ".toml", ".ini", ".cfg", ".conf",
|
|
49
|
+
".env", ".properties", ".md", ".mdx", ".txt", ".ipynb", ".sql", ".tf", ".gs", ".c", ".cc",
|
|
50
|
+
".cpp", ".h", ".hpp", ".m", ".mm", ".groovy", ".gradle"]);
|
|
51
|
+
const TEXT_NAMES = new Set(["Dockerfile", "Makefile", ".env", ".env.example", ".env.local", "Procfile"]);
|
|
52
|
+
const MAX_BYTES = 2_000_000;
|
|
53
|
+
|
|
54
|
+
export function rewriteText(text) {
|
|
55
|
+
return text.replace(new RegExp(ENDPOINT_SRC, "g"), (...args) => NEW_HOST + args.at(-1).path);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function scanText(text) {
|
|
59
|
+
const findings = [];
|
|
60
|
+
text.split(/\r\n|\r|\n/).forEach((line, i) => {
|
|
61
|
+
for (const m of line.matchAll(new RegExp(ENDPOINT_SRC, "g"))) {
|
|
62
|
+
findings.push({ line: i + 1, kind: "endpoint", match: m[0], fix: NEW_HOST + m.groups.path });
|
|
63
|
+
}
|
|
64
|
+
for (const [kind, re, advice] of SDK_PATTERNS) {
|
|
65
|
+
if (re.test(line)) findings.push({ line: i + 1, kind, match: line.trim().slice(0, 160), fix: advice });
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return findings;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isCandidate(p) {
|
|
72
|
+
const name = path.basename(p);
|
|
73
|
+
if (TEXT_NAMES.has(name) || name.startsWith(".env")) return true;
|
|
74
|
+
return TEXT_EXTS.has(path.extname(name));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function* iterFiles(root) {
|
|
78
|
+
if (fs.statSync(root).isFile()) { yield root; return; }
|
|
79
|
+
const entries = fs.readdirSync(root, { withFileTypes: true });
|
|
80
|
+
const dirs = entries.filter((e) => e.isDirectory() && !SKIP_DIRS.has(e.name)).map((e) => e.name).sort();
|
|
81
|
+
const files = entries.filter((e) => e.isFile()).map((e) => e.name).sort();
|
|
82
|
+
for (const f of files) { const p = path.join(root, f); if (isCandidate(p)) yield p; }
|
|
83
|
+
for (const d of dirs) yield* iterFiles(path.join(root, d));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function readText(p) {
|
|
87
|
+
try {
|
|
88
|
+
if (fs.statSync(p).size > MAX_BYTES) return null;
|
|
89
|
+
const buf = fs.readFileSync(p);
|
|
90
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(buf);
|
|
91
|
+
return text;
|
|
92
|
+
} catch { return null; }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function simpleDiff(rel, a, b) {
|
|
96
|
+
const al = a.split("\n"), bl = b.split("\n");
|
|
97
|
+
if (a === b) return "";
|
|
98
|
+
const out = [`--- a/${rel}`, `+++ b/${rel}`];
|
|
99
|
+
for (let i = 0; i < al.length; i++) {
|
|
100
|
+
if (al[i] !== bl[i]) out.push(`@@ line ${i + 1} @@`, `-${al[i]}`, `+${bl[i]}`);
|
|
101
|
+
}
|
|
102
|
+
return out.join("\n") + "\n";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function scan(root, apply = false) {
|
|
106
|
+
const isDir = fs.statSync(root).isDirectory();
|
|
107
|
+
const files = [];
|
|
108
|
+
for (const p of iterFiles(root)) {
|
|
109
|
+
const text = readText(p);
|
|
110
|
+
if (text === null || (!text.includes("customsearch") && !text.includes("cse"))) continue;
|
|
111
|
+
const findings = scanText(text);
|
|
112
|
+
if (!findings.length) continue;
|
|
113
|
+
const newText = rewriteText(text);
|
|
114
|
+
const rel = (isDir ? path.relative(root, p) : path.basename(p)).split(path.sep).join("/");
|
|
115
|
+
if (apply && newText !== text) fs.writeFileSync(p, newText, "utf-8");
|
|
116
|
+
files.push({ path: rel, findings, diff: simpleDiff(rel, text, newText), changed: newText !== text });
|
|
117
|
+
}
|
|
118
|
+
const endpoints = files.flatMap((f) => f.findings).filter((x) => x.kind === "endpoint").length;
|
|
119
|
+
const sdk = files.flatMap((f) => f.findings).filter((x) => x.kind !== "endpoint").length;
|
|
120
|
+
return {
|
|
121
|
+
tool: "google-cse-shutdown-migrator", version: VERSION, applied: apply, files,
|
|
122
|
+
summary: { files: files.length, endpoints, sdk_calls: sdk, files_rewritable: files.filter((f) => f.changed).length },
|
|
123
|
+
key_note: KEY_NOTE,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function render(report) {
|
|
128
|
+
const out = [];
|
|
129
|
+
for (const f of report.files) {
|
|
130
|
+
out.push(f.path);
|
|
131
|
+
for (const x of f.findings) {
|
|
132
|
+
out.push(` L${x.line} [${x.kind}] ${x.match}`);
|
|
133
|
+
if (x.kind !== "endpoint") out.push(` -> ${x.fix}`);
|
|
134
|
+
}
|
|
135
|
+
if (f.diff) out.push(f.diff.replace(/\n$/, ""));
|
|
136
|
+
out.push("");
|
|
137
|
+
}
|
|
138
|
+
const s = report.summary;
|
|
139
|
+
out.push(`${s.endpoints} endpoint URL(s), ${s.sdk_calls} client library call(s) in ${s.files} file(s).`);
|
|
140
|
+
out.push(`${report.applied ? "Rewrote" : "Would rewrite"} ${s.files_rewritable} file(s).` + (report.applied ? "" : " Run again with --apply to write."));
|
|
141
|
+
if (s.files) out.push(report.key_note);
|
|
142
|
+
return out.join("\n") + "\n";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function main(argv) {
|
|
146
|
+
const args = argv.filter((a) => !a.startsWith("--"));
|
|
147
|
+
const flags = new Set(argv.filter((a) => a.startsWith("--")));
|
|
148
|
+
if (flags.has("--version")) { console.log(VERSION); return 0; }
|
|
149
|
+
if (flags.has("--help")) { console.log("usage: migrate-cse.mjs [PATH] [--apply] [--json]"); return 0; }
|
|
150
|
+
const root = args[0] || ".";
|
|
151
|
+
if (!fs.existsSync(root)) { console.error(`No such path: ${root}`); return 2; }
|
|
152
|
+
const report = scan(root, flags.has("--apply"));
|
|
153
|
+
process.stdout.write(flags.has("--json") ? JSON.stringify(report, null, 2) + "\n" : render(report));
|
|
154
|
+
return 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (process.argv[1] && fs.realpathSync(path.resolve(process.argv[1])) === fs.realpathSync(fileURLToPath(import.meta.url))) {
|
|
158
|
+
process.exitCode = main(process.argv.slice(2));
|
|
159
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Find Google Custom Search JSON API calls in a codebase and move them to SpecStream.
|
|
3
|
+
|
|
4
|
+
Google ends the Custom Search JSON API for existing customers on January 1, 2027.
|
|
5
|
+
SpecStream answers the same request at https://api.specstream.ai/customsearch/v1 in
|
|
6
|
+
Google's own response shape (items[], searchInformation, queries.nextPage), so only the
|
|
7
|
+
host changes.
|
|
8
|
+
|
|
9
|
+
Usage:
|
|
10
|
+
python3 migrate_cse.py [PATH] # dry run: report and diff, writes nothing
|
|
11
|
+
python3 migrate_cse.py [PATH] --apply # rewrite endpoint URLs in place
|
|
12
|
+
python3 migrate_cse.py [PATH] --json # machine readable report
|
|
13
|
+
|
|
14
|
+
No dependencies beyond the Python 3.8+ standard library.
|
|
15
|
+
"""
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import argparse
|
|
19
|
+
import difflib
|
|
20
|
+
import json
|
|
21
|
+
import os
|
|
22
|
+
import re
|
|
23
|
+
import sys
|
|
24
|
+
|
|
25
|
+
__version__ = "1.0.0"
|
|
26
|
+
|
|
27
|
+
NEW_HOST = "https://api.specstream.ai"
|
|
28
|
+
|
|
29
|
+
# A Google host and the Custom Search path in the same literal. Only these are rewritten.
|
|
30
|
+
ENDPOINT_RE = re.compile(
|
|
31
|
+
r"(?P<scheme>https?://)(?P<host>www\.googleapis\.com|customsearch\.googleapis\.com)"
|
|
32
|
+
r"(?P<path>/customsearch/v1(?:/siterestrict)?)\b"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
# Calls that use Google's client libraries. Reported with the change to make, never rewritten.
|
|
36
|
+
SDK_PATTERNS = [
|
|
37
|
+
("python-google-api-client",
|
|
38
|
+
re.compile(r"""build\(\s*['"]customsearch['"]"""),
|
|
39
|
+
"googleapiclient build('customsearch', ...): call the REST endpoint directly, "
|
|
40
|
+
"GET https://api.specstream.ai/customsearch/v1?key=SPECSTREAM_KEY&q=..., the response "
|
|
41
|
+
"keeps Google's shape (items[], searchInformation, queries.nextPage)."),
|
|
42
|
+
("node-googleapis",
|
|
43
|
+
re.compile(r"""\bcustomsearch\(\s*(?:['"]v1['"]|\{)"""),
|
|
44
|
+
"googleapis customsearch(...): pass rootUrl 'https://api.specstream.ai/' in the options, "
|
|
45
|
+
"or call GET https://api.specstream.ai/customsearch/v1 directly."),
|
|
46
|
+
("cse-list-call",
|
|
47
|
+
re.compile(r"\.cse\(\)\s*\.(?:list|siterestrict)\b|\bcse\.(?:list|siterestrict)\("),
|
|
48
|
+
"Custom Search client call: same response fields at https://api.specstream.ai/customsearch/v1."),
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
KEY_NOTE = (
|
|
52
|
+
"Use a SpecStream API key in key= (or X-Goog-Api-Key, or a Bearer token): a Google key "
|
|
53
|
+
"returns 401. cx is accepted and ignored. Get a key with $2 free credit, no card: "
|
|
54
|
+
"https://specstream.ai/tools/google-cse-migrator"
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
SKIP_DIRS = {
|
|
58
|
+
".git", ".hg", ".svn", "node_modules", ".venv", "venv", "env", "__pycache__",
|
|
59
|
+
"dist", "build", ".next", ".nuxt", "target", "vendor", ".tox", ".mypy_cache",
|
|
60
|
+
".pytest_cache", "site-packages", "bower_components", "coverage",
|
|
61
|
+
}
|
|
62
|
+
TEXT_EXTS = {
|
|
63
|
+
".py", ".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx", ".go", ".rb", ".php", ".java",
|
|
64
|
+
".kt", ".kts", ".scala", ".cs", ".rs", ".swift", ".dart", ".sh", ".bash", ".zsh",
|
|
65
|
+
".ps1", ".r", ".R", ".jl", ".lua", ".pl", ".ex", ".exs", ".clj", ".vue", ".svelte",
|
|
66
|
+
".astro", ".html", ".htm", ".json", ".yaml", ".yml", ".toml", ".ini", ".cfg",
|
|
67
|
+
".conf", ".env", ".properties", ".md", ".mdx", ".txt", ".ipynb", ".sql", ".tf",
|
|
68
|
+
".gs", ".c", ".cc", ".cpp", ".h", ".hpp", ".m", ".mm", ".groovy", ".gradle",
|
|
69
|
+
}
|
|
70
|
+
TEXT_NAMES = {"Dockerfile", "Makefile", ".env", ".env.example", ".env.local", "Procfile"}
|
|
71
|
+
MAX_BYTES = 2_000_000
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def rewrite_text(text: str) -> str:
|
|
75
|
+
"""Swap Google's Custom Search host for SpecStream's. Idempotent."""
|
|
76
|
+
return ENDPOINT_RE.sub(lambda m: NEW_HOST + m.group("path"), text)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def scan_text(text: str) -> list[dict]:
|
|
80
|
+
"""Return one finding per matching line: endpoint URLs and client library calls."""
|
|
81
|
+
findings = []
|
|
82
|
+
for lineno, line in enumerate(text.splitlines(), 1):
|
|
83
|
+
for m in ENDPOINT_RE.finditer(line):
|
|
84
|
+
findings.append({
|
|
85
|
+
"line": lineno, "kind": "endpoint", "match": m.group(0),
|
|
86
|
+
"fix": NEW_HOST + m.group("path"),
|
|
87
|
+
})
|
|
88
|
+
for kind, pattern, advice in SDK_PATTERNS:
|
|
89
|
+
if pattern.search(line):
|
|
90
|
+
findings.append({"line": lineno, "kind": kind, "match": line.strip()[:160], "fix": advice})
|
|
91
|
+
return findings
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def is_candidate(path: str) -> bool:
|
|
95
|
+
name = os.path.basename(path)
|
|
96
|
+
if name in TEXT_NAMES or name.startswith(".env"):
|
|
97
|
+
return True
|
|
98
|
+
return os.path.splitext(name)[1] in TEXT_EXTS
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def iter_files(root: str):
|
|
102
|
+
if os.path.isfile(root):
|
|
103
|
+
yield root
|
|
104
|
+
return
|
|
105
|
+
for dirpath, dirnames, filenames in os.walk(root):
|
|
106
|
+
dirnames[:] = sorted(d for d in dirnames if d not in SKIP_DIRS)
|
|
107
|
+
for name in sorted(filenames):
|
|
108
|
+
path = os.path.join(dirpath, name)
|
|
109
|
+
if is_candidate(path):
|
|
110
|
+
yield path
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def read_text(path: str):
|
|
114
|
+
try:
|
|
115
|
+
if os.path.getsize(path) > MAX_BYTES:
|
|
116
|
+
return None
|
|
117
|
+
with open(path, "r", encoding="utf-8", newline="") as fh:
|
|
118
|
+
return fh.read()
|
|
119
|
+
except (OSError, UnicodeDecodeError):
|
|
120
|
+
return None
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def scan(root: str, apply: bool = False) -> dict:
|
|
124
|
+
files = []
|
|
125
|
+
for path in iter_files(root):
|
|
126
|
+
text = read_text(path)
|
|
127
|
+
if text is None or ("customsearch" not in text and "cse" not in text):
|
|
128
|
+
continue
|
|
129
|
+
findings = scan_text(text)
|
|
130
|
+
if not findings:
|
|
131
|
+
continue
|
|
132
|
+
new_text = rewrite_text(text)
|
|
133
|
+
rel = os.path.relpath(path, root) if os.path.isdir(root) else os.path.basename(path)
|
|
134
|
+
rel = rel.replace(os.sep, "/")
|
|
135
|
+
diff = "".join(difflib.unified_diff(
|
|
136
|
+
text.splitlines(True), new_text.splitlines(True),
|
|
137
|
+
fromfile="a/" + rel, tofile="b/" + rel,
|
|
138
|
+
))
|
|
139
|
+
if apply and new_text != text:
|
|
140
|
+
with open(path, "w", encoding="utf-8", newline="") as fh:
|
|
141
|
+
fh.write(new_text)
|
|
142
|
+
files.append({"path": rel, "findings": findings, "diff": diff, "changed": new_text != text})
|
|
143
|
+
endpoints = sum(1 for f in files for x in f["findings"] if x["kind"] == "endpoint")
|
|
144
|
+
sdk = sum(1 for f in files for x in f["findings"] if x["kind"] != "endpoint")
|
|
145
|
+
return {
|
|
146
|
+
"tool": "google-cse-shutdown-migrator", "version": __version__,
|
|
147
|
+
"applied": apply, "files": files,
|
|
148
|
+
"summary": {"files": len(files), "endpoints": endpoints, "sdk_calls": sdk,
|
|
149
|
+
"files_rewritable": sum(1 for f in files if f["changed"])},
|
|
150
|
+
"key_note": KEY_NOTE,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def render(report: dict) -> str:
|
|
155
|
+
out = []
|
|
156
|
+
for f in report["files"]:
|
|
157
|
+
out.append(f["path"])
|
|
158
|
+
for x in f["findings"]:
|
|
159
|
+
out.append(f" L{x['line']} [{x['kind']}] {x['match']}")
|
|
160
|
+
if x["kind"] != "endpoint":
|
|
161
|
+
out.append(f" -> {x['fix']}")
|
|
162
|
+
if f["diff"]:
|
|
163
|
+
out.append(f["diff"].rstrip("\n"))
|
|
164
|
+
out.append("")
|
|
165
|
+
s = report["summary"]
|
|
166
|
+
verb = "Rewrote" if report["applied"] else "Would rewrite"
|
|
167
|
+
out.append(f"{s['endpoints']} endpoint URL(s), {s['sdk_calls']} client library call(s) in {s['files']} file(s).")
|
|
168
|
+
out.append(f"{verb} {s['files_rewritable']} file(s)." + ("" if report["applied"] else " Run again with --apply to write."))
|
|
169
|
+
if s["files"]:
|
|
170
|
+
out.append(report["key_note"])
|
|
171
|
+
return "\n".join(out) + "\n"
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def main(argv=None) -> int:
|
|
175
|
+
p = argparse.ArgumentParser(description="Move Google Custom Search JSON API calls to SpecStream before January 1, 2027.")
|
|
176
|
+
p.add_argument("path", nargs="?", default=".")
|
|
177
|
+
p.add_argument("--apply", action="store_true", help="rewrite endpoint URLs in place")
|
|
178
|
+
p.add_argument("--json", action="store_true", help="print the report as JSON")
|
|
179
|
+
p.add_argument("--version", action="version", version=__version__)
|
|
180
|
+
a = p.parse_args(argv)
|
|
181
|
+
if not os.path.exists(a.path):
|
|
182
|
+
print(f"No such path: {a.path}", file=sys.stderr)
|
|
183
|
+
return 2
|
|
184
|
+
report = scan(a.path, apply=a.apply)
|
|
185
|
+
sys.stdout.write(json.dumps(report, indent=2) + "\n" if a.json else render(report))
|
|
186
|
+
return 0
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
if __name__ == "__main__":
|
|
190
|
+
sys.exit(main())
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@specstream/google-cse-migrator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Find Google Custom Search JSON API calls before the January 1, 2027 shutdown and move them to a drop in endpoint that returns the same JSON.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": { "google-cse-migrator": "migrate-cse.mjs" },
|
|
7
|
+
"files": ["migrate-cse.mjs", "README.md", "LICENSE"],
|
|
8
|
+
"engines": { "node": ">=18" },
|
|
9
|
+
"keywords": ["google-custom-search", "customsearch", "google-cse", "custom-search-json-api", "programmable-search-engine", "googleapis", "search-api", "migration", "codemod", "specstream"],
|
|
10
|
+
"homepage": "https://specstream.ai/tools/google-cse-migrator",
|
|
11
|
+
"repository": { "type": "git", "url": "git+https://github.com/Specstreamai/google-cse-shutdown-migrator.git" },
|
|
12
|
+
"bugs": { "url": "https://github.com/Specstreamai/google-cse-shutdown-migrator/issues" },
|
|
13
|
+
"author": "SpecStream <support@specstream.ai> (https://specstream.ai)",
|
|
14
|
+
"license": "MIT"
|
|
15
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.21"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "google-cse-migrator"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Find Google Custom Search JSON API calls before the January 1, 2027 shutdown and move them to a drop in endpoint that returns the same JSON."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.8"
|
|
13
|
+
authors = [{ name = "SpecStream", email = "support@specstream.ai" }]
|
|
14
|
+
keywords = ["google-custom-search", "customsearch", "google-cse", "custom-search-json-api", "programmable-search-engine", "search-api", "migration", "codemod", "specstream"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 5 - Production/Stable",
|
|
17
|
+
"Environment :: Console",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Topic :: Internet :: WWW/HTTP :: Indexing/Search",
|
|
22
|
+
"Topic :: Software Development :: Code Generators",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://specstream.ai/tools/google-cse-migrator"
|
|
27
|
+
"Search API" = "https://specstream.ai/search-api"
|
|
28
|
+
Pricing = "https://specstream.ai/pricing"
|
|
29
|
+
Source = "https://github.com/Specstreamai/google-cse-shutdown-migrator"
|
|
30
|
+
Issues = "https://github.com/Specstreamai/google-cse-shutdown-migrator/issues"
|
|
31
|
+
|
|
32
|
+
[project.scripts]
|
|
33
|
+
google-cse-migrator = "google_cse_migrator.migrate_cse:main"
|
|
34
|
+
|
|
35
|
+
[tool.hatch.build.targets.wheel]
|
|
36
|
+
bypass-selection = true
|
|
37
|
+
|
|
38
|
+
[tool.hatch.build.targets.wheel.force-include]
|
|
39
|
+
"migrate_cse.py" = "google_cse_migrator/migrate_cse.py"
|
|
40
|
+
|
|
41
|
+
[tool.hatch.build.targets.sdist]
|
|
42
|
+
include = ["migrate_cse.py", "migrate-cse.mjs", "test_migrate_cse.py", "tests", "README.md", "LICENSE", "package.json"]
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Tests for migrate_cse.py. Run: python3 -m unittest -v test_migrate_cse"""
|
|
2
|
+
import io
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import shutil
|
|
6
|
+
import subprocess
|
|
7
|
+
import tempfile
|
|
8
|
+
import unittest
|
|
9
|
+
from contextlib import redirect_stdout
|
|
10
|
+
|
|
11
|
+
import migrate_cse as m
|
|
12
|
+
|
|
13
|
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
14
|
+
FIXTURE = os.path.join(HERE, "tests", "fixture")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def tree_snapshot(root):
|
|
18
|
+
snap = {}
|
|
19
|
+
for dirpath, _, files in os.walk(root):
|
|
20
|
+
for name in files:
|
|
21
|
+
p = os.path.join(dirpath, name)
|
|
22
|
+
with open(p, "rb") as fh:
|
|
23
|
+
snap[os.path.relpath(p, root)] = fh.read()
|
|
24
|
+
return snap
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class RewriteTests(unittest.TestCase):
|
|
28
|
+
def test_both_google_hosts_are_rewritten(self):
|
|
29
|
+
self.assertEqual(m.rewrite_text("https://www.googleapis.com/customsearch/v1?q=a"),
|
|
30
|
+
"https://api.specstream.ai/customsearch/v1?q=a")
|
|
31
|
+
self.assertEqual(m.rewrite_text("https://customsearch.googleapis.com/customsearch/v1"),
|
|
32
|
+
"https://api.specstream.ai/customsearch/v1")
|
|
33
|
+
|
|
34
|
+
def test_siterestrict_path_is_kept(self):
|
|
35
|
+
self.assertEqual(m.rewrite_text("'https://www.googleapis.com/customsearch/v1/siterestrict'"),
|
|
36
|
+
"'https://api.specstream.ai/customsearch/v1/siterestrict'")
|
|
37
|
+
|
|
38
|
+
def test_other_google_apis_are_left_alone(self):
|
|
39
|
+
for s in ("https://www.googleapis.com/youtube/v3/search",
|
|
40
|
+
"https://www.googleapis.com",
|
|
41
|
+
"https://www.googleapis.com/customsearch/v10"):
|
|
42
|
+
self.assertEqual(m.rewrite_text(s), s)
|
|
43
|
+
|
|
44
|
+
def test_rewrite_is_idempotent(self):
|
|
45
|
+
once = m.rewrite_text("x = 'https://www.googleapis.com/customsearch/v1'")
|
|
46
|
+
self.assertEqual(m.rewrite_text(once), once)
|
|
47
|
+
|
|
48
|
+
def test_sdk_calls_are_reported_not_rewritten(self):
|
|
49
|
+
line = 'service = build("customsearch", "v1", developerKey=KEY)'
|
|
50
|
+
kinds = {f["kind"] for f in m.scan_text(line)}
|
|
51
|
+
self.assertIn("python-google-api-client", kinds)
|
|
52
|
+
self.assertEqual(m.rewrite_text(line), line)
|
|
53
|
+
self.assertIn("node-googleapis", {f["kind"] for f in m.scan_text('google.customsearch("v1")')})
|
|
54
|
+
self.assertIn("cse-list-call", {f["kind"] for f in m.scan_text("s.cse().list(q=q).execute()")})
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class ScanTests(unittest.TestCase):
|
|
58
|
+
def setUp(self):
|
|
59
|
+
self.tmp = tempfile.mkdtemp()
|
|
60
|
+
self.root = os.path.join(self.tmp, "repo")
|
|
61
|
+
shutil.copytree(FIXTURE, self.root)
|
|
62
|
+
os.makedirs(os.path.join(self.root, ".git"))
|
|
63
|
+
with open(os.path.join(self.root, ".git", "config.txt"), "w") as fh:
|
|
64
|
+
fh.write("url = https://www.googleapis.com/customsearch/v1\n")
|
|
65
|
+
|
|
66
|
+
def tearDown(self):
|
|
67
|
+
shutil.rmtree(self.tmp)
|
|
68
|
+
|
|
69
|
+
def test_dry_run_writes_nothing(self):
|
|
70
|
+
before = tree_snapshot(self.root)
|
|
71
|
+
report = m.scan(self.root)
|
|
72
|
+
self.assertEqual(tree_snapshot(self.root), before)
|
|
73
|
+
self.assertFalse(report["applied"])
|
|
74
|
+
self.assertEqual(report["summary"]["files_rewritable"], 3)
|
|
75
|
+
|
|
76
|
+
def test_skips_git_and_node_modules(self):
|
|
77
|
+
paths = {f["path"] for f in m.scan(self.root)["files"]}
|
|
78
|
+
self.assertEqual(paths, {".env.example", "src/search.js", "src/search.py"})
|
|
79
|
+
|
|
80
|
+
def test_counts(self):
|
|
81
|
+
s = m.scan(self.root)["summary"]
|
|
82
|
+
self.assertEqual(s["endpoints"], 4)
|
|
83
|
+
self.assertEqual(s["sdk_calls"], 3)
|
|
84
|
+
|
|
85
|
+
def test_apply_rewrites_then_second_run_changes_nothing(self):
|
|
86
|
+
m.scan(self.root, apply=True)
|
|
87
|
+
with open(os.path.join(self.root, "src", "search.py")) as fh:
|
|
88
|
+
text = fh.read()
|
|
89
|
+
self.assertIn('URL = "https://api.specstream.ai/customsearch/v1"', text)
|
|
90
|
+
self.assertIn("https://api.specstream.ai/customsearch/v1/siterestrict", text)
|
|
91
|
+
self.assertIn("https://www.googleapis.com/youtube/v3/search", text)
|
|
92
|
+
self.assertIn('data["searchInformation"]["totalResults"]', text)
|
|
93
|
+
with open(os.path.join(self.root, "node_modules", "pkg", "index.js")) as fh:
|
|
94
|
+
self.assertIn("www.googleapis.com/customsearch", fh.read())
|
|
95
|
+
after_first = tree_snapshot(self.root)
|
|
96
|
+
second = m.scan(self.root, apply=True)
|
|
97
|
+
self.assertEqual(tree_snapshot(self.root), after_first)
|
|
98
|
+
self.assertEqual(second["summary"]["files_rewritable"], 0)
|
|
99
|
+
|
|
100
|
+
def test_cli_dry_run_output(self):
|
|
101
|
+
buf = io.StringIO()
|
|
102
|
+
with redirect_stdout(buf):
|
|
103
|
+
code = m.main([self.root])
|
|
104
|
+
self.assertEqual(code, 0)
|
|
105
|
+
out = buf.getvalue()
|
|
106
|
+
self.assertIn("+URL = \"https://api.specstream.ai/customsearch/v1\"", out)
|
|
107
|
+
self.assertIn("Would rewrite 3 file(s).", out)
|
|
108
|
+
self.assertIn("cx is accepted and ignored", out)
|
|
109
|
+
|
|
110
|
+
def test_missing_path(self):
|
|
111
|
+
self.assertEqual(m.main([os.path.join(self.tmp, "nope")]), 2)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@unittest.skipUnless(shutil.which("node"), "node not installed")
|
|
115
|
+
class NodeParityTests(unittest.TestCase):
|
|
116
|
+
def test_node_and_python_agree(self):
|
|
117
|
+
py = m.scan(FIXTURE)
|
|
118
|
+
out = subprocess.run(["node", os.path.join(HERE, "migrate-cse.mjs"), FIXTURE, "--json"],
|
|
119
|
+
check=True, capture_output=True, text=True).stdout
|
|
120
|
+
js = json.loads(out)
|
|
121
|
+
strip = lambda r: [{k: v for k, v in f.items() if k != "diff"} for f in r["files"]]
|
|
122
|
+
self.assertEqual(strip(js), strip(py))
|
|
123
|
+
self.assertEqual(js["summary"], py["summary"])
|
|
124
|
+
|
|
125
|
+
def test_node_apply_matches_python_apply(self):
|
|
126
|
+
tmp = tempfile.mkdtemp()
|
|
127
|
+
try:
|
|
128
|
+
a, b = os.path.join(tmp, "a"), os.path.join(tmp, "b")
|
|
129
|
+
shutil.copytree(FIXTURE, a)
|
|
130
|
+
shutil.copytree(FIXTURE, b)
|
|
131
|
+
m.scan(a, apply=True)
|
|
132
|
+
subprocess.run(["node", os.path.join(HERE, "migrate-cse.mjs"), b, "--apply"],
|
|
133
|
+
check=True, capture_output=True)
|
|
134
|
+
self.assertEqual(tree_snapshot(a), tree_snapshot(b))
|
|
135
|
+
finally:
|
|
136
|
+
shutil.rmtree(tmp)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
if __name__ == "__main__":
|
|
140
|
+
unittest.main()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
GOOGLE_CSE_ENDPOINT=https://www.googleapis.com/customsearch/v1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = "https://www.googleapis.com/customsearch/v1";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nothing to see here
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const { google } = require("googleapis");
|
|
2
|
+
const customsearch = google.customsearch("v1");
|
|
3
|
+
|
|
4
|
+
async function search(q) {
|
|
5
|
+
const res = await fetch(`https://www.googleapis.com/customsearch/v1?key=${process.env.KEY}&cx=${process.env.CX}&q=${encodeURIComponent(q)}`);
|
|
6
|
+
const data = await res.json();
|
|
7
|
+
return { items: data.items ?? [], next: data.queries?.nextPage?.[0]?.startIndex };
|
|
8
|
+
}
|
|
9
|
+
module.exports = { search };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import requests
|
|
3
|
+
from googleapiclient.discovery import build
|
|
4
|
+
|
|
5
|
+
URL = "https://www.googleapis.com/customsearch/v1"
|
|
6
|
+
SITE_URL = "https://customsearch.googleapis.com/customsearch/v1/siterestrict"
|
|
7
|
+
OTHER = "https://www.googleapis.com/youtube/v3/search"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def search(q):
|
|
11
|
+
r = requests.get(URL, params={"key": os.environ["CSE_KEY"], "cx": os.environ["CSE_CX"], "q": q})
|
|
12
|
+
data = r.json()
|
|
13
|
+
return [i["link"] for i in data.get("items", [])], data["searchInformation"]["totalResults"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def search_sdk(q):
|
|
17
|
+
service = build("customsearch", "v1", developerKey=os.environ["CSE_KEY"])
|
|
18
|
+
return service.cse().list(q=q, cx=os.environ["CSE_CX"]).execute()
|