-
Notifications
You must be signed in to change notification settings - Fork 356
Added Wikipedia extension (fixed) #1941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
samuellouf
wants to merge
22
commits into
TurboWarp:master
Choose a base branch
from
samuellouf:Wikipedia
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8dedb63
Created Wikipedia.js
samuellouf 8a05d2c
Update extensions.json
samuellouf fb4fcf7
Update Wikipedia.js
samuellouf 56d38c1
Update Wikipedia.js
samuellouf 40f625b
Merge branch 'Wikipedia' of https://github.com/samuellouf/extensions …
samuellouf 186fd98
Corrected my errors + Added Functionnalities
samuellouf c72fe96
Made lint and prettier fix it
samuellouf 94660d8
Small changes
samuellouf eaee56e
Merge branch 'TurboWarp:master' into Wikipedia
samuellouf cd8d9e0
Added Translation to menus
samuellouf f377481
Merge branch 'TurboWarp:master' into Wikipedia
samuellouf 55322d8
Merge branch 'TurboWarp:master' into Wikipedia
samuellouf dfc3eac
Update Wikipedia.js to fix incorrect pluralization
PPPDUD 1937918
Merge branch 'TurboWarp:master' into Wikipedia
samuellouf c18e5f9
Update extensions/SamuelLouf/Wikipedia.js
samuellouf 946f5e9
Update extensions/SamuelLouf/Wikipedia.js
samuellouf 9426420
Update extensions/SamuelLouf/Wikipedia.js
samuellouf 4f61f99
Made a few changes
samuellouf 1b43637
Merge branch 'TurboWarp:master' into Wikipedia
samuellouf 2ff0ad6
Removed double "encodeURIComponent"
samuellouf 08ad352
Fixed some things
samuellouf 0d2fd50
[Automated] Format code
DangoCat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,298 @@ | ||
| // Name: Wikipedia | ||
| // ID: samuelloufwikipedia | ||
| // Description: Fetch the content of Wikipedia pages. | ||
| // By: SamuelLouf <https://scratch.mit.edu/users/samuellouf/> | ||
| // License: MPL-2.0 | ||
|
|
||
| (function (Scratch) { | ||
| "use strict"; | ||
|
|
||
| const icon = | ||
| "data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIxOTkuMjYzMzkiIGhlaWdodD0iMTk5LjA5MTgzIiB2aWV3Qm94PSIwLDAsMTk5LjI2MzM5LDE5OS4wOTE4MyI+PGcgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTE0OS4zNzY4MywtNjguNDQwOTUpIj48ZyBzdHJva2U9Im5vbmUiIHN0cm9rZS1taXRlcmxpbWl0PSIxMCI+PHBhdGggZD0iTTE0OS4zNzY4MywyNjcuNTMyNzhjMCwwIDAsLTI3Ljg4Nzk3IDAsLTU0LjgzNjE5YzAsLTE0LjQ3MzQzIDM1LjI3MzgsMjkuNDg0NzUgMzUuNjM5ODUsLTMzLjc2NDA3YzAuNDY2MjksLTgwLjU2OTEgLTM1LjYzOTg1LC0yMi41MDAzNiAtMzUuNjM5ODUsLTMxLjg4ODI5YzAsLTE1LjE2MzIgMCwtMjguNTEyOSAwLC0yOC41MTI5YzAsMCA0Ny41MjAxNSwwIDc4LjE4MTU1LDBjNi41NTQxNiwwIC01My4zMTY1LC00My41NTA0MyAyNC41ODY3NywtNDkuNDIxODdjOTguMzA3NTEsLTcuNDA5MjggMjMuMTQzN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English"), | ||
| value: "en", | ||
| }, | ||
| { | ||
| text: Scratch.translate("French"), | ||
| value: "fr", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Spanish"), | ||
| value: "es", | ||
| }, | ||
| { | ||
| text: Scratch.translate("German"), | ||
| value: "de", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Italian"), | ||
| value: "it", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Portuguese"), | ||
| value: "pt", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Russian"), | ||
| value: "ru", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Japanese"), | ||
| value: "ja", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Chinese"), | ||
| value: "zh", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Arabic"), | ||
| value: "ar", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Polish"), | ||
| value: "pl", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Egyptian"), | ||
| value: "arz", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Dutch"), | ||
| value: "nl", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Vietnamese"), | ||
| value: "vi", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Ukrainian"), | ||
| value: "uk", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Swedish"), | ||
| value: "sv", | ||
| }, | ||
| { | ||
| text: Scratch.translate("Vietnamese"), | ||
| value: "vi", | ||
| }, | ||
| ]; | ||
|
|
||
| const hasOwn = (obj, property) => | ||
| Object.prototype.hasOwnProperty.call(obj, property); | ||
|
|
||
| function json_array_filter(key, json) { | ||
| json = JSON.stringify(json); | ||
| try { | ||
| json = JSON.parse(json); | ||
| return json.map((x) => { | ||
| if (hasOwn(x, key)) { | ||
| return x[key]; | ||
| } | ||
| return null; | ||
| }); | ||
| } catch (e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function fetchWikipedia(language, name) { | ||
| return Scratch.fetch( | ||
| `https://${language}.wikipedia.org/w/api.php?action=query&prop=extracts&exlimit=1&titles=${encodeURIComponent(name)}&explaintext=1&exsectionformat=plain&format=json&origin=*` | ||
| ) | ||
| .then((response) => { | ||
| if (response.ok == true) { | ||
| return response.json(); | ||
| } else { | ||
| return ""; | ||
| } | ||
| }) | ||
| .catch((error) => { | ||
| return ""; | ||
| }); | ||
| } | ||
|
|
||
| class Wikipedia { | ||
| constructor() { | ||
| this.wikipediaLanguages = "en"; | ||
| } | ||
|
|
||
| getInfo() { | ||
| return { | ||
| id: "samuelloufwikipedia", | ||
| name: Scratch.translate("Wikipedia"), | ||
| color1: "#bcbcbc", | ||
| menuIconURI: icon, | ||
| blocks: [ | ||
| { | ||
| opcode: "fetchFirstParagraph", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("first paragraph on [NAME]'s article"), | ||
| disableMonitor: true, | ||
| arguments: { | ||
| NAME: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "Scratch (programming language)", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "fetchShortSentence", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("short sentence about [NAME]"), | ||
| disableMonitor: true, | ||
| arguments: { | ||
| NAME: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: Scratch.translate( | ||
| "Scratch (programming language)" | ||
| ), | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "getPageURL", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("URL of page [NAME]"), | ||
| disableMonitor: true, | ||
| arguments: { | ||
| NAME: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: Scratch.translate( | ||
| "Scratch (programming language)" | ||
| ), | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "doesPageExists", | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: Scratch.translate("does page [NAME] exist?"), | ||
| disableMonitor: true, | ||
| arguments: { | ||
| NAME: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: Scratch.translate("Unexistant page"), | ||
| }, | ||
| }, | ||
| }, | ||
| "---", | ||
| { | ||
| opcode: "addLanguage", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: Scratch.translate( | ||
| "add the language [text] with the id [value]" | ||
| ), | ||
| arguments: { | ||
| text: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: Scratch.translate("Esperanto"), | ||
| }, | ||
| value: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: "eo", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "selectLanguage", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: Scratch.translate("switch wikipedia to [LANGUAGE]"), | ||
| arguments: { | ||
| LANGUAGE: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| menu: "languages", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "getLanguage", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("Wikipedia's language"), | ||
| }, | ||
| ], | ||
| menus: { | ||
| languages: { | ||
| acceptReporters: false, | ||
| items: languages_list, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| async fetchFirstParagraph(args) { | ||
| var data = await fetchWikipedia( | ||
| this.wikipediaLanguages, | ||
| encodeURIComponent(args.NAME) | ||
| ); | ||
| if (data == "") return ""; | ||
| const pageId = Object.keys(data.query.pages)[0]; | ||
| let extract = data.query.pages[pageId].extract; | ||
| extract = extract.replace(/\s{2,}/g, " "); | ||
| var paragraph = extract.split("\n")[0]; | ||
| var split_paragraph = paragraph.split("."); | ||
| var result = ""; | ||
| for (var i in split_paragraph) { | ||
| // @ts-ignore | ||
| if (i != split_paragraph.length - 1) { | ||
| result = result + split_paragraph[i] + "."; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| async fetchShortSentence(args) { | ||
| var data = await fetchWikipedia( | ||
| this.wikipediaLanguages, | ||
| encodeURIComponent(args.NAME) | ||
| ); | ||
| if (data == "") return ""; | ||
| const pageId = Object.keys(data.query.pages)[0]; | ||
| let extract = data.query.pages[pageId].extract; | ||
| extract = extract.replace(/\s{2,}/g, " "); | ||
| return extract.split(".").slice(0, 2).join(".") + "."; | ||
| } | ||
|
|
||
| getPageURL(args) { | ||
| return `https://${this.wikipediaLanguages}.wikipedia.org/wiki/${args.NAME.replace(/\s/g, "_")}`; | ||
| } | ||
|
|
||
| async doesPageExists(args) { | ||
| var data = await fetchWikipedia( | ||
| this.wikipediaLanguages, | ||
| encodeURIComponent(args.NAME) | ||
| ); | ||
| if (data == "") return ""; | ||
| return !!data.query.pages[0]; | ||
| } | ||
|
|
||
| // --- | ||
| // --- | ||
|
|
||
| addLanguage(args) { | ||
| languages_list.push(args); | ||
| Scratch.vm.extensionManager.refreshBlocks(); | ||
| } | ||
|
|
||
| selectLanguage(args) { | ||
| this.wikipediaLanguages = args.LANGUAGE; | ||
| } | ||
|
|
||
| getLanguage(args) { | ||
| try { | ||
| return json_array_filter("text", languages_list)[ | ||
| json_array_filter("value", languages_list).indexOf( | ||
| this.wikipediaLanguages | ||
| ) | ||
| ]; | ||
| } catch { | ||
| return ""; | ||
| } | ||
| } | ||
| } | ||
| Scratch.extensions.register(new Wikipedia()); | ||
| // @ts-ignore | ||
| })(Scratch); | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap this in Scratch.canFetch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it uses scratch.fetch already
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused? I thought we were supposed to wrap even non-native Scratch.fetch with Scratch.canFetch anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I knew for sure regular
fetchwas supposed to be wrappedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which existing extensions wrap Scratch.fetch in canFetch?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, maybe I just misunderstood
Lily's skins does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it probably shouldn't
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scratch.fetch is literally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay