balsimpson
10/19/2017 - 12:57 AM

flashBriefing

var num = context.global.getRandom;

var feed = global.get('feed');

var thoughts = 'Here are a few shower thoughts: ';
var learned = 'Today I learned ';
var prompts = 'In Writing Prompts: ';

var _feed = [];

var _num = num(0, feed.length);

for (var i = 0; i < feed.length; i++) {
    
    if ( feed[_num].subreddit == "Showerthoughts" && !feed[_num].seen ) {
        if (thoughts.length < 800) {
            thoughts += feed[_num].title + '\r\n ';
            feed[_num].seen = true;
        }
    } else {
            _num = num(0, feed.length);
        }
    
    if (feed[_num].subreddit == "todayilearned" && !feed[_num].seen) {
        if (feed[_num].title.substr(0, 8) == 'TIL that') {
            feed[_num].title = feed[_num].title.substr(9, feed[_num].title.length);        
        } else if (feed[_num].title.substr(0, 3) == 'TIL') {
            feed[_num].title = feed[_num].title.substr(4, feed[_num].title.length);        
        }
        
        if (learned.length < 800) {
            learned += feed[_num].title + '\r\n ';
            feed[_num].seen = true;
        }
        
    } else {
            _num = num(0, feed.length);
        } 
    
    if (feed[_num].subreddit == "WritingPrompts" && !feed[_num].seen) {
        if (feed[_num].title.substr(0, 4) == '[WP]') {
            feed[_num].title = feed[_num].title.substr(5, feed[_num].title.length);        
        } else if (feed[_num].title.substr(0, 4) == '[EU]') {
            feed[_num].title = feed[_num].title.substr(5, feed[_num].title.length);        
        }
        
        if (prompts.length < 800) {
            prompts += feed[_num].title + '\r\n ';
            feed[_num].seen = true;
        }
    } else {
            _num = num(0, feed.length);
        }
        
}
    
    var thoughts_data = {
          uid: num(200, 300) * 23,
          updateDate : new Date(),
          titleText : "Showerthoughts",
          mainText : thoughts,
          redirectionUrl : "www.google.com"
    };
    _feed.push(thoughts_data);
    
    var learned_data = {
          uid: num(300, 400) * 23,
          updateDate : new Date(),
          titleText : "todayilearned",
          mainText : learned,
          redirectionUrl : "www.google.com"
    };
    _feed.push(learned_data);
    
    var prompts_data = {
          uid: num(400, 500) * 23,
          updateDate : new Date(),
          titleText : "Showerthoughts",
          mainText : prompts,
          redirectionUrl : "www.google.com"
    };
    _feed.push(prompts_data);

global.set('feed', feed);
msg.payload = _feed;
return msg;