obniz developer team
Published

"Like" on Twitter to Blow Soap Bubbles

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

BeginnerFull instructions provided1 hour462
"Like" on Twitter to Blow Soap Bubbles

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1

Story

Read more

Code

Untitled file

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


}

Credits

obniz developer team
80 projects • 37 followers
Development board "obniz" is controlled via the internet.

Comments