LoginSignup
5
5

More than 5 years have passed since last update.

「いいね」で飛ぶシャボン玉

Posted at

これは何?

Twitterでツイートがいいねされるたび、バブルガンからシャボン玉が発射されます。

Screen Shot 2019-04-19 at 20.13.40.png

用意するもの

  • obniz
  • obnizの電源
  • バブルガン
  • 石鹸水

手順

まず、ツイートのいいね数を取得するために、Twitter Developerに登録します。
以下のURLより申請することができ、内容に問題なければ数日で承認されます。(2019/04/05現在)
https://developer.twitter.com/

次に、バブルガンの電池ボックスの蓋を開け、プラス極側を0番に、マイナス極側を1番にobnizに繋ぎます。
(一般的なアルカリ電池の電圧は1つにつき1.5Vですが、obnizから加えられる電圧は3Vまたは5Vです。バブルガンの仕様の電池数に対応する電圧を超えて印可しないようにご注意ください。)

最後に、obnizに電源を繋ぎ、バブルガンに石鹸水をつけます。
下記のプログラムでは、自分の一番最新のツイートにいいねをされるたびにバブルガンからシャボン玉が発射されます。

プログラムについて

nodejs (async/awaitが使えるバージョン)でプログラムしています。

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 processingTime = 0;
let startTime;

obniz.onconnect = async () => {

    //10秒ごとにループを回し、前ループからいいねが増えたらその分だけシャボン玉を飛ばす
    //初期値を-1に設定し、サーバ立ち上げ時に既にされているいいね分のシャボン玉を飛ばす
    let prevLikeCount = -1;

    obniz.repeat(async function(){

        await client.get('statuses/user_timeline', params, async function(error,tweets,response){
            //obniz.debugprint = true;
            if (error) {
                console.log("error");
                console.log(error);
                return;
            }

            let nowLikeCount = tweets[0].favorite_count;
            console.log("nowLikeCount:"+nowLikeCount);

            if(nowLikeCount > prevLikeCount){

                processingTime = nowLikeCount * (ROTATE_TIME + STOP_TIME);

                //ここは1周目のときに通る
                if(prevLikeCount === -1){
                    startTime = new Date().getTime();
                    console.log("first like count:"+nowLikeCount)

                    //サーバ立ち上げた時にすでにあるlike分シャボン玉飛ばす
                    for(let i=0; i<nowLikeCount; 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);    
                    }

                    processingTime = 0;
                    prevLikeCount = nowLikeCount;
                    return;
                }

                //前のループの処理時間中だったらループを抜ける
                let nowTime = new Date().getTime();
                if(nowTime - (startTime + processingTime) < 0){
                    prevLikeCount = nowLikeCount;
                    return;
                }

                let diffLikeCount = nowLikeCount - prevLikeCount;
                startTime = new Date().getTime();

                console.log("like added!");
                console.log("like count:"+nowLikeCount);

                processingTime = diffLikeCount * (ROTATE_TIME + STOP_TIME);

                //likeの増加分だけシャボン玉飛ばす
                for(let i=0; i<diffLikeCount; 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);
                }

                processingTime = 0;
                prevLikeCount = nowLikeCount;

            }else{
                console.log("not like added");
                prevLikeCount = nowLikeCount;
            }

        });

    }, 10000);


}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5