GlennHS / Scrum-Helper

A vanillaJS morning scrum helping tool
5 stars 1 forks source link

Dog/Cat Toggle #37

Open GlennHS opened 3 years ago

GlennHS commented 3 years ago

http://placedog.net

Possibly implement with #29

GlennHS commented 3 years ago

@mikef80

ssddanbrown commented 3 years ago

Based upon potential users I think you might be serving, I think there may be also a desire for bunny rabbits :rabbit2::rabbit2::rabbit2::rabbit2::rabbit2::rabbit2:.

I remember you used the Reddit API before. Quickly chopped the below together based upon an older play project of mine (https://danb.me/viewr/). Can support other animals via updating the subreddit map.

View code ```javascript /** * For the given type of animal, look up to reddit to return a image thumbnail URL * (If supported, Otherwise a random cute animal picture) * may be returned). * @param {String} animal * @returns {String} */ async function getCuteImageUrlFromReddit(animal = 'any') { // Our mapping of animal types to relevant subreddits. const subredditMap = { 'cat': 'cats+catpics+catsstandingup+illegallysmolcats', 'dog': 'dogs+dogpictures+dogswearinghats', 'bunny': 'rabbits', 'any': 'aww+eyebleach' }; // Default to any if the animal is not in the map. const subreddit = subredditMap[animal] || subredditMap['any']; // Build the reddit JSON endpoint URL. const fetchUrl = `https://www.reddit.com/r/${subreddit}/hot.json?limit=100&count=100`; // Fetch a bunch of posts from reddit as JSON. const resp = await window.fetch(fetchUrl); const results = await resp.json(); // Map into the post.data since reddit nests the actual data within another object level. const allPosts = results.data.children.map(post => post.data); // Our list of approved content domains that should be serving images // and are likely not dodgy. const approvedContentDomains = [ 'i.imgur.com', 'imgur.com', 'i.redd.it', 'i.reddituploads.com' ]; // Find a likely image post here that's not sticked or pinned as they're often // static and won't provide a changing result. const imagePost = allPosts.find(post => { const postUrlDomain = post.url.split('/')[2] || ''; return approvedContentDomains.includes(postUrlDomain) && !post.stickied && !post.pinned; }); // Return the thumbnail of the found post otherwise empty string if nothing matches. // Can change this to be 'url' instead of thumbnail to get original image url // but might need more confidence on content type to do that (eg. Extension checking). // Would also mean more variance in image size (Both dimensions and filesize). return imagePost ? imagePost.thumbnail : ''; } // Usage (Async/Await) const url = await getCuteImageUrlFromReddit('cat'); console.log(url); // Usage (Promises) getCuteImageUrlFromReddit('cat').then(url => console.log(url)); ```

Fair more hassle to deal with that the placedog/kitten though so this route may not be worthwhile, just putting it out there though in case it helps.