“Like” on Twitter Makes Soap Bubbles Blowed

What’s this?

The bubble gun will blow soap bubbles each time your tweet gets like on Twitter.

Materials

  • obniz x1
  • power supply for obniz x1
  • bubble gun x1
  • soapy water x1

Steps

First, apply for a Twitter developer account and be approved in order to use Twitter API.
You can apply from below URL and usually get approved in a few days.
https://developer.twitter.com/

Second, open the battery case and connect electrode plate to the obniz.

obnizbubble gun
0positive
1negative

(Please be careful not to apply excessive voltage.)

Finally, connect the obniz to a power, and then dip bubble gun in soapy water.

Program

Get “consumer_key“, “consumer_secret“, “access_token_key” and access_token_secret from here. Then, save as “secret.json” like below and put the file at the same directory as the program.

{
  "consumer_key"        :"YOUR_CONSUMER_KEY",
  "consumer_secret"     :"YOUR_CONSUMER_SECRET",
  "access_token_key"    :"YOUR_ACCESS_TOKEN_KEY",
  "access_token_secret" :"YOUR_ACCESS_TOKEN_SECRET"
}

This program is written in node.js. Not runnable on browser.

const fs = require("fs");
const Obniz = require("obniz");
const obniz = new Obniz("OBNIZ_ID_HERE");
const Twitter = require("twitter");
const client = new Twitter(JSON.parse(fs.readFileSync("secret.json","utf-8")));
const params = {Name: "YOUR_HANDLE_NAME_ON_TWITTER"};
const ROTATE_TIME = 1500;
const STOP_TIME = 1000;
let isBlowing = false;
let prevLikeCount = 0;
obniz.onconnect = async () => {
  //10秒ごとにループを回し、前ループからいいねが増えたらその分だけシャボン玉を飛ばす
  obniz.repeat(async function(){
    
    if(isBlowing){
      return;
    }
    await client.get('statuses/user_timeline', params, async function(error,tweets,response){
      if (error) {
        console.log("error");
        console.log(error);
        return;
      }
        
      let currentLikeCount = tweets[0].favorite_count;
      console.log("currentLikeCount:"+currentLikeCount);
      
      if(currentLikeCount <= prevLikeCount){
        console.log("not like added");
        prevLikeCount = currentLikeCount;
        return;
      }
        
      let diffLikeCount = currentLikeCount - prevLikeCount;
      
      //likeの増加分だけシャボン玉飛ばす
      await blowBubbles(diffLikeCount);
        
      console.log("like added!");
      console.log("like count:"+currentLikeCount);
        
      prevLikeCount = currentLikeCount;
    });
  }, 10000);
  async function blowBubbles(count){
    isBlowing = true;
    
    for(let i=0; i<count; i++){
      await obniz.io0.output(true);
      await obniz.io1.output(false);
      await obniz.wait(ROTATE_TIME);
      await obniz.io0.output(false);
      await obniz.wait(STOP_TIME);
    }
    
    isBlowing = false;
  }
}