fix: fallback to English for invalid Wikipedia language codes#474
Open
lennney wants to merge 2 commits into
Open
fix: fallback to English for invalid Wikipedia language codes#474lennney wants to merge 2 commits into
lennney wants to merge 2 commits into
Conversation
Adds `--json` flag to all search commands (text, images, videos, news, books) and extract command. When used, results are output as formatted JSON to stdout, making it easier for coding agents and scripts to parse. Previously, the only way to get JSON output was via `-o` which writes to a file. The new `--json` flag outputs directly to stdout without requiring a file path. Closes deedy5#432
When region='wt-wt' (DuckDuckGo's worldwide sentinel), the Wikipedia engine extracted lang='wt' which is not a valid ISO 639 language code, causing ConnectionError on https://wt.wikipedia.org. Add _INVALID_LANG_CODES frozenset containing 'wt' and fall back to 'en' when the extracted language code is in this set. Fixes deedy5#417
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR improves CLI output flexibility and hardens Wikipedia engine behavior for certain DuckDuckGo region codes.
Changes:
- Add
--jsonflag to multiple CLI commands to print results as JSON to stdout. - Add a Wikipedia language fallback to English when the derived language code is not valid (e.g.,
wt).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| ddgs/engines/wikipedia.py | Adds a guard to map invalid DDG region-derived “lang” codes to en for Wikipedia API calls. |
| ddgs/cli.py | Introduces a --json flag across several commands (and adjusts output branching) to emit JSON on stdout. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+566
to
+576
| @click.option("-nc", "--no-color", is_flag=True, default=False, help="disable color output") | ||
| @click.option("--json", "as_json", is_flag=True, default=False, help="output results as JSON to stdout") | ||
| def extract( | ||
| url: str, | ||
| fmt: str, | ||
| output: str | None, | ||
| proxy: str | None, | ||
| *, | ||
| verify: bool, | ||
| no_color: bool, | ||
| as_json: bool, |
Comment on lines
+264
to
267
| if as_json: | ||
| click.echo(json.dumps(data, ensure_ascii=False, indent=2)) | ||
| elif not output and not download: | ||
| _print_data(data, no_color=no_color) |
Comment on lines
+377
to
380
| if as_json: | ||
| click.echo(json.dumps(data, ensure_ascii=False, indent=2)) | ||
| elif not output and not download: | ||
| _print_data(data, no_color=no_color) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #417
When region=wt-wt (DuckDuckGo worldwide sentinel), the Wikipedia engine extracts lang=wt which is not a valid ISO 639 language code. This causes a ConnectionError on every search because wt.wikipedia.org does not exist.
Changes
Testing
Verified with unit tests and integration test: