Last active
July 29, 2020 15:26
-
-
Save UhRhythm/5e208cce89f3e845ba942f5f081fc7f9 to your computer and use it in GitHub Desktop.
Spobniz.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
require('dotenv').config(); | |
const request = require('request'); | |
//同期処理のため | |
const sync_request = require('sync-request'); | |
// obniz認証情報 | |
const obniz_id = process.env.OBNIZ_ID; | |
const Obniz = require('obniz'); | |
const obniz = new Obniz(obniz_id); | |
// spotify認証情報(.envから読み込む) | |
const access_token = process.env.ACCESS_TOKEN; | |
const client_id = process.env.CLIENT_ID; // Your client id | |
const client_secret = process.env.CLIENT_SECRET; // Your secret | |
const redirect_uri = process.env.REDIRECT_URI; // Your redirect urico | |
//日本語対応 | |
const { createCanvas } = require('canvas'); | |
// ヘッダー情報 | |
let headers = { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${access_token}` | |
}; | |
obniz.onconnect = async function () { | |
// RGB LEDを呼び出す | |
var rgbled = obniz.wired('WS2811', { gnd: 0, vcc: 1, din: 2 }); | |
var state = await obniz.switch.getWait(); | |
// スイッチの反応を常時監視 | |
obniz.switch.onchange = function (state) { | |
var result = getTrackInfo(); | |
if (state === 'push') { | |
var is_playing = result['is_playing']; | |
// 再生 | |
if (!is_playing) { | |
playTrack(); | |
// 停止 | |
} else { | |
pauseTrack(); | |
} | |
// ディスプレイ処理 | |
obniz.display.clear(); | |
const r = random(0, 255); | |
const g = random(0, 255); | |
const b = random(0, 255); | |
console.log(r, g, b); | |
rgbled.rgb(r, g, b); | |
} else if (state == 'none') { | |
//曲情報を表示 | |
var info = getDispInfo(result); | |
obniz.display.clear(); | |
} else if (state === 'right') { | |
// 早送り | |
console.log('skip'); | |
rgbled.rgb(0, 255, 0); | |
skipTrack(); | |
obniz.display.clear(); | |
obniz.display.print('skip'); | |
} else if (state === 'left') { | |
rgbled.rgb(255, 0, 255); | |
backTrack(); | |
obniz.display.clear(); | |
obniz.display.print('back'); | |
} | |
} | |
setInterval(async function () { | |
var result = getTrackInfo(); | |
if (state == 'none' && result['is_playing']) { | |
rgbled.rgb(random(0, 255), random(0, 255), random(0, 255)); | |
} else { | |
rgbled.rgb(0, 0, 0); | |
} | |
}, 1200); | |
setInterval(async function () { | |
if (state == 'none') { | |
var result = getTrackInfo(); | |
var info = getDispInfo(result); | |
console.log(info); | |
const canvas = createCanvas(128, 64); | |
const ctx = canvas.getContext('2d'); | |
ctx.fillStyle = "white"; | |
ctx.font = "15px Avenir"; | |
ctx.fillText(info, 0, 40); | |
obniz.display.draw(ctx); | |
} | |
}, 3000); | |
} | |
// RGBをランダムで | |
var random = function (min, max) { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive | |
} | |
// 再生 | |
function playTrack() { | |
console.log("play func"); | |
var options = { | |
url: 'https://api.spotify.com/v1/me/player/play', | |
method: 'PUT', | |
headers: headers, | |
}; | |
function callback(error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log("success"); | |
return body; | |
} | |
} | |
request(options, callback); | |
} | |
// 巻き戻し | |
function backTrack() { | |
console.log("back func"); | |
var options = { | |
url: 'https://api.spotify.com/v1/me/player/previous', | |
method: 'POST', | |
headers: headers | |
}; | |
function callback(error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log(body); | |
} | |
} | |
request(options, callback); | |
} | |
// 早送り | |
function skipTrack() { | |
console.log("skip func"); | |
var options = { | |
url: 'https://api.spotify.com/v1/me/player/next', | |
method: 'POST', | |
headers: headers | |
}; | |
function callback(error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log(body); | |
} | |
} | |
request(options, callback); | |
} | |
// 再生中の曲を取得 | |
function getTrackInfo() { | |
var response = sync_request( | |
'GET', | |
'https://api.spotify.com/v1/me/player/currently-playing', { | |
headers: headers, | |
}); | |
return JSON.parse(response.getBody('utf8')); | |
} | |
// ディスプレイ表示用の曲情報 | |
function getDispInfo(data) { | |
var title = data["item"]["name"]; | |
var name = data["item"]["artists"]["0"]["name"]; | |
return title + "\n" + name; | |
} | |
// 停止 | |
function pauseTrack() { | |
console.log("stop func"); | |
var options = { | |
url: 'https://api.spotify.com/v1/me/player/pause', | |
method: 'PUT', | |
headers: headers | |
}; | |
function callback(error, response, body) { | |
if (!error && response.statusCode == 200) { | |
console.log(body); | |
} | |
} | |
request(options, callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment