-
Notifications
You must be signed in to change notification settings - Fork 0
49 get pitcures for plantcard #57
Merged
+8,014
−116
Merged
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3c3aa29
Add photos from wikimedia
687dbd3
Add plant card image placeholder
15f171b
Style placeholder image, add placeholder text
3be67f2
Style frontpage so everything is visible
3690007
Make infobox smaller
f02b13b
Change from green to pink color on preferences and ino-button
46a3ee9
Bold text on preferences buttton
e406a3d
delete raw data files and the package-lock.json
helenul ccadd57
run lint and prettier
helenul 2b8e109
Merge branch 'main' into 49-get-pitcures-for-plantcard
helenul cc6f755
fix minor typos
helenul 6a2b5e0
Remove raw data and run linting/formatting
0cb2f3f
Fix merge conflict
2473825
Changed so the pictures are base 64 encoded instead of using url
alexajah 30e7c13
loaded all the data with new picture data into the database
alexajah 888ecac
Minor fixes to getting data and the plantcard
alexajah 7ac173c
Merge branch 'main' into 49-get-pitcures-for-plantcard
helenul 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 |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ node_modules/ | |
| dist/ | ||
| dist-ssr/ | ||
| *.local | ||
| .DS_Store | ||
Binary file not shown.
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
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
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,67 @@ | ||
| import { db } from "./db.js"; | ||
| import { getWikimediaImage } from "./wikimedia.js"; | ||
|
|
||
| interface PlantDoc { | ||
| commonName: string; | ||
| scientificName?: string[]; | ||
| imageURL?: string | null; | ||
| } | ||
|
|
||
| const plants = db.collection<PlantDoc>("plants"); | ||
|
|
||
| async function updatePlantImages() { | ||
| const docs = await plants.find({}).toArray(); | ||
|
|
||
| console.log(`Fant ${docs.length} planter`); | ||
|
|
||
| for (const doc of docs) { | ||
| const scientificName = doc.scientificName?.[0]; | ||
|
|
||
| if (!scientificName) { | ||
| console.log(`Mangler scientificName: ${doc.commonName}`); | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| console.log(`Søker etter: ${scientificName}`); | ||
|
|
||
| const imageURL = await getWikimediaImage(scientificName); | ||
|
|
||
| if (!imageURL) { | ||
| console.log(`Fant ikke bilde for ${scientificName}`); | ||
|
|
||
| await plants.updateOne( | ||
| { _id: doc._id }, | ||
| { | ||
| $set: { | ||
| imageURL: null, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| await plants.updateOne( | ||
| { _id: doc._id }, | ||
| { | ||
| $set: { | ||
| imageURL, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| console.log(`✓ Oppdaterte ${doc.commonName}`); | ||
| } catch (error) { | ||
| console.error( | ||
| `Feil ved henting av bilde for ${scientificName}:`, | ||
| error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| console.log("Ferdig!"); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| updatePlantImages(); |
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,51 @@ | ||
| export async function getWikimediaImage(scientificName: string) { | ||
| const params = new URLSearchParams({ | ||
| action: "query", | ||
| generator: "search", | ||
| gsrsearch: scientificName, | ||
| gsrnamespace: "6", | ||
| gsrlimit: "10", | ||
| prop: "imageinfo", | ||
| iiprop: "url|mime|extmetadata", | ||
| format: "json", | ||
| origin: "*", | ||
| }); | ||
|
|
||
| const response = await fetch( | ||
| `https://commons.wikimedia.org/w/api.php?${params}` | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Wikimedia svarte med status ${response.status}` | ||
| ); | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| const pages = data.query?.pages; | ||
|
|
||
| if (!pages) { | ||
| return null; | ||
| } | ||
|
|
||
| const results = Object.values(pages) as any[]; | ||
|
|
||
| const allowedMimeTypes = [ | ||
| "image/jpeg", | ||
| "image/png", | ||
| "image/webp", | ||
| ]; | ||
|
|
||
| const image = results.find((page) => { | ||
| const info = page.imageinfo?.[0]; | ||
|
|
||
| if (!info?.url || !info?.mime) { | ||
| return false; | ||
| } | ||
|
|
||
| return allowedMimeTypes.includes(info.mime); | ||
| }); | ||
|
|
||
| return image?.imageinfo?.[0]?.url ?? null; | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.