dvargas92495 / SmartBlocks

Useful examples from developer community for Roam42 SmartBlocks
147 stars 7 forks source link

Random Line From Shakespeare #86

Open shabegom opened 3 years ago

shabegom commented 3 years ago

✂️ Copy of your #42SmartBlock from Roam

📋 Describe the SmartBlock

Fetches a random line from a random Shakespeare play

✅ Describe any prerequisites or dependencies that are required for this SmartBlock

I was surprised that there wasn't an existing method online to do this, so I wrote my own. It is essentially trying to guess a URL from Folger's Shakespeare database using a recursive function. Response time can be a bit slow if it doesn't get a hit in the first few tries. Please be patient

const getQuote = () => {
    let number = randomInteger(1000).toString().padStart(4, '0')
    let play = plays[randomInteger(plays.length-1)]
  return axios.get(`https://folgerdigitaltexts.org/${play.code}/segment/sp-${number}`)
    .then(response => {
      return {quote: turndownService.turndown(response.data), play: play.name}
  })
  .catch(err => {
    return getQuote()
  })

}

📷 Screenshot of your #42SmartBlock workflow/template from Roam

💡 Additional Info

You can see the full code in glitch. It's a very basic Express App: https://glitch.com/~shakespeare-quote

GitMurf commented 3 years ago

Here is a version with a few tweaks I made :)



image

image

4sRf4vNfTI