"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.

Material

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

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.

positive : obniz io0
negative : obniz io1

(Please be careful not to apply excessive voltage.)

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

About program

This program is nodejs. Not runnable on Browser.

Program

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);


}