dvargas92495 / SmartBlocks

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

Google Trends SmartBlock #202

Open mlava opened 3 years ago

mlava commented 3 years ago

✂️ Copy of your #42SmartBlock from Roam

Google Trends SmartBlock.zip

📋 Describe the SmartBlock

SmartBlock to retrieve Daily Trends or Category Realtime trends from Google Trends.

Part of a set of SmartBlocks written to fulfil a bounty requested by Ekim Nazim Kaya: https://roamresearch.com/#/app/roambounties/page/01-09-2021

Hacker News - Google Trends - Product Hunt - Reddit - Twitter Trending

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

Roam42.

Requires creation of a node.js server. I did this on heroku: https://devcenter.heroku.com/articles/getting-started-with-nodejs

The address of the node.js API server needs to be supplied in the configuration settings.

Included CSS to allow horizontal/grid display of results sub-blocks. Also includes a button for placement in Daily Notes templates.

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

image

Each item has sub-blocks containing a summary, representative image and link to a news source.

💡 Additional Info

Content of app.js file in root folder of nodejs server:

const googleTrends = require('./node_modules/google-trends-api');
const express = require('express');
var cors = require('cors');
const app = express();
var port = process.env.PORT || 3000;
app.use(cors());

var corsOptions = {
  origin: 'https://roamresearch.com',
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}

app.get('/', cors(corsOptions), (req, res) => {

    var mode = req.query.mode;

    if (req.query.country) {
    var country = req.query.country;
    } else {
        console.log("Please supply a Country! Defaulting to US.");
        var country = "US";
    }

    if (mode == "DT") {
        googleTrends.dailyTrends({
          geo: country,
        }, function(err, results) {
          if (err) {
            console.log(err);
          }else{
            res.json(results);
          }
        });
    } else if (mode == "cat") {
        if (req.query.category) {
            var cat = req.query.category;
        } else {
            var cat = "all";
        }
        googleTrends.realTimeTrends({
        geo: country,
        category: cat,
        }, function(err, results) {
            if (err) {
                console.log(err);
            } else {

            } 
        });
    }
})

app.listen(port, () => {
  console.log(`Listening at http://localhost:${port}`)
})