Hello everyone;
I spent my entire day building a picture-filled random quote machine.
Initially, it didn’t seem so challenging; I thought I’d do some web scraping to grab the relevant author’s picture and do something unique. Then I realized that using Wikipedia for suitable images was the best approach — until I discovered that Wikipedia had an API. Oh, how I wish I hadn’t found out! That’s when I started grappling with the most challenging and complex API I’ve ever encountered. Fetching the image directly or from its URL seemed impossible, or perhaps I just couldn’t manage it. The documentation wasn’t quite sufficient, or maybe I couldn’t figure out the format to get the desired result from the documentation. At first, I managed to create a very elegant search API endpoint as described in the documentation. But then I gave up and simply inserted the author’s name into the search URLs I found …
const fetchQuoteData = async () => {
const quoteData = randomquoteApi.randomQuote();
setQuote(quoteData);
const url = `https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=${quoteData.author}&gsrlimit=5&prop=pageimages|extracts&exchars=25&exintro&explaintext&exlimit=max&format=json&origin=*`;
I tried to locate the matching page by searching for the author, then I had to retrieve the page image using a separate API call.
The hardest part was finding a single page to match; for some authors, it was impossible. For instance, searching for John F. Kennedy
might return John F. Kennedy Airport...
I tried various approaches, fetching multiple results and selecting the best match. I changed my search method, compared string characters, implemented a scoring system, but it didn’t work. Finally, I discovered a package called “string-similarity” that provided the most consistent results. However, the problem wasn’t over yet; I found that the API’s data mostly returned the ID of the first element in the “pages” property as -1 — sometimes random numbers. Of course, I couldn’t initially realize this; I only discovered it when I was trying to debug why Muhammad Ali’s picture wasn’t showing up… There were many bugs and issues along the way. At one point, I found myself reading the documentation for the HTML anchor element …
Finally, it’s done. Of course, it’s possible to achieve the same task with a much better, more elegant code structure, but this is all I could manage. I was so focused on the JavaScript part that I had no energy left for new design or anything related to CSS. I borrowed the design from a sample project and wrote the CSS in a straightforward manner without much thought.
For now, everything seems to be working okay. Seneca occasionally causes issues, but there’s nothing I can do about that; I’m not a big fan of his philosophy anyway. And also sometimes some author’s image little bit irrelevant… Oh, and sometimes Britney Spears lyrics pop up; I respect those who love her, but let’s face it, they don’t pair well after Einstein . Maybe if I brainstormed a bit, I could impose a ban on certain authors… Anyway, that’s enough for now…
I just wanted to share; of course, I’d greatly appreciate any feedback. I’m particularly curious if there are better solutions for finding images and matching them to authors .
Happy coding, everyone!