Skip to content

Commit

Permalink
Merge pull request #4 from TUS-OSK/gif
Browse files Browse the repository at this point in the history
slackで写真ではなくgifを返すように
  • Loading branch information
ilim0t authored Mar 2, 2019
2 parents 906ff1c + 05547a6 commit 34f1a73
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 13 deletions.
13 changes: 9 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
"use strict";

const photoapi = require("./libs/photoAPI");
const {capture} = require("./libs/caputure");
const capture = require("./libs/caputure");
const slack = require("./slack");
const utils = require("./libs/utils");

const main = async () => {
//写真撮影
const cap = new capture.Capture(0);
cap.setIntervalCapture();

//環境設定
const {client_id, client_secret, slack_token} = process.env;
const oAuth2Client = await photoapi.getOAuthToken(client_id, client_secret);
const slackBot = new slack.Slack(slack_token);

//アルバム, 共有のの設定
//アルバム, 共有の設定
const albumTitle = "bushitsuchan_album";
const albums = await photoapi.getAlbumList(oAuth2Client);
let album = albums.filter(album => album.title === albumTitle)[0];
Expand All @@ -20,9 +24,10 @@ const main = async () => {
await photoapi.shareAlbum(oAuth2Client, album.id)
}

//slackbotの返信メッセージの設定
slackBot.getReplyText = async () => {
const photo = await capture(0, ".png");
const uploadToken = await photoapi.uploadPhoto(oAuth2Client, photo, Date().toLocaleString());
const buf = cap.generateGif();
const uploadToken = await photoapi.uploadPhoto(oAuth2Client, buf, Date().toLocaleString());
const {mediaItem} = await photoapi.createAlbumMediaItem(oAuth2Client, album.id, uploadToken, "");
const {baseUrl} = await photoapi.getMediaItem(oAuth2Client, mediaItem.id);
return await utils.getShortURL(baseUrl);
Expand Down
53 changes: 48 additions & 5 deletions libs/caputure.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
"use strict";

const cv = require('opencv4nodejs');
const GIFEncoder = require('gifencoder');

module.exports.capture = async (devicePort = 0, ext = ".png") => {
const cap = new cv.VideoCapture(devicePort);
const frame = cap.read();
return cv.imencode(ext, frame);
};

module.exports.Capture = class Capture {
constructor(devicePort = 0) {
this.cap = new cv.VideoCapture(devicePort);
this.photoArray = [];
}

capture() {
return new Promise(resolve => {
const image = this.cap.read();
resolve(image);
})
};

setIntervalCapture(ms = 10 * 1000, limit = 6) {
setInterval(() => {
if (this.photoArray.length >= limit) {
this.photoArray.shift();
}
this.capture()
.then(photo => this.photoArray.push(photo));
}, ms);
}

static encode(image, ext) {
return cv.imencode(ext, image);
};

generateGif() {
if (this.photoArray.length === 0) {
throw new Error("");
}
const [height, width] = this.photoArray[0].sizes;

const encoder = new GIFEncoder(width, height);
encoder.start();
encoder.setRepeat(0);
encoder.setDelay(200);
encoder.setQuality(10);
for (let img of this.photoArray) {
encoder.addFrame(img.cvtColor(cv.COLOR_BGR2RGBA).getData());
}
encoder.finish();
return encoder.out.getData();
}

};
6 changes: 3 additions & 3 deletions libs/photoAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ module.exports.getAlbumList = async oAuth2Client => {
/**
* 画像のバイナリデータを送信します
* @param oAuth2Client
* @param photo
* @param buf
* @param {string} filename
* @returns {Promise<string>} uploadToken
*/
module.exports.uploadPhoto = async (oAuth2Client, photo, filename) => {
module.exports.uploadPhoto = async (oAuth2Client, buf, filename) => {
const accessToken = await oAuth2Client.getAccessToken();
const url = "https://photoslibrary.googleapis.com/v1/uploads";
const headers = {
Expand All @@ -102,7 +102,7 @@ module.exports.uploadPhoto = async (oAuth2Client, photo, filename) => {
return rpap(url, {
method: "POST",
headers: headers,
body: photo
body: buf
})
};

Expand Down
2 changes: 1 addition & 1 deletion libs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports.getLocalIps = () => {
}
}
if (ipList.length === 0) {
throw Error("IP Adressが見つかりません");
throw new Error("IP Adressが見つかりません");
}
return ipList;
};
Loading

0 comments on commit 34f1a73

Please sign in to comment.