Skip to content

Commit

Permalink
Merge pull request #2 from TUS-OSK/slackbot
Browse files Browse the repository at this point in the history
Slackbot
  • Loading branch information
ilim0t authored Feb 28, 2019
2 parents d40cf14 + 5b4edea commit 68fa0d0
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 119 deletions.
56 changes: 18 additions & 38 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,34 @@
const photoapi = require("./photoAPI");
const {capture} = require("./caputure");
const slack = require("./slack");
const utils = require("./utils");

const main = async () => {
//認証鍵の設定
const {client_id, client_secret} = process.env;
if (!client_id || !client_secret) {
console.log("READMEに従ってGoogle Photos APIsの認証鍵を設定してください");
process.exit(1)
}
//環境設定
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];

if (album === undefined) {
let album = albums.filter(album => album.title === albumTitle)[0];
if (!album) {
album = await photoapi.createAlbum(oAuth2Client, albumTitle);
await photoapi.shareAlbum(oAuth2Client, album.id)
}

//定期的に撮影した写真の共有リンクをslackbotで送信
//https://developers.google.com/photos/library/guides/api-limits-quotas に抵触しないように!!
/**
* 何msに一回実行するか あまり小さくしすぎるとエラーが発生します
* @type {number}
*/
const interval = 10 * 1000;
if (60 * 60 * 24 * 1000 / 10000 * 3 > interval) {
console.log(`注意: 1日あたり${(60 * 60 * 24 * 3 / interval * 1000).toLocaleString()}回PhotoAPIを叩く設定で,1日の上限10,000回を越してしまいます`)
}
setInterval(async () => {
const url = await capture(oAuth2Client, album).catch(e => {
console.error(e.name);
if (e.name === "StatusCodeError") {
console.error(JSON.parse(e.error).error.message);
return
}
console.error(e.message)
// console.error(e)
});
if (!url) {
return
}
const shortURL = await photoapi.getShortURL(url);

// ここをカスタマイズしてください
slack.send(shortURL)
}, interval)
slackBot.getReplyText = async () => {
const photo = await capture(0, ".png");
const uploadToken = await photoapi.uploadPhoto(oAuth2Client, photo, Date().toLocaleString());
const {mediaItem} = await photoapi.createAlbumMediaItem(oAuth2Client, album.id, uploadToken, "");
const {baseUrl} = await photoapi.getMediaItem(oAuth2Client, mediaItem.id);
const shortUrl = await utils.getShortURL(baseUrl);
return shortUrl;
};

//slackbotの開始
slackBot.start();
};


Expand Down
48 changes: 6 additions & 42 deletions caputure.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
"use strict";

const photoapi = require("./photoAPI");
const NodeWebcam = require('node-webcam');
const slack = require("./slack");
const cv = require('opencv4nodejs');


const Webcam = NodeWebcam.create({
width: 1280,
height: 720,
quality: 100,

delay: 0,
saveShots: true,
device: false,
callbackReturn: "buffer",
verbose: false
});

/**
* 写真を撮影しGooglePhotoへとアップロード,その共有リンクを取得します
* @param {oAuth2Client} oAuth2Client - photoapi.getOAuthToken関数で取得します
* @param {Object} album
* @returns {Promise<void>}
*/
module.exports.capture = async (oAuth2Client, album) => {
const photo = await new Promise((resolve, reject) => {
Webcam.capture("capture", (err, photo) => {
if (err) {
reject(err)
}
resolve(photo)
})
}).catch(e => {
console.error(e);
console.error(
"READMEに従ってカメラを使えるようにしてください\n" +
"また,OSやセキュリティソフトでカメラへのアクセスをブロックしている可能性もあります 解除してください\n");
process.exit(1)
});
const uploadToken = await photoapi.uploadPhoto(oAuth2Client, photo, Date().toLocaleString());
const {mediaItem} = await photoapi.createAlbumMediaItem(oAuth2Client, album.id, uploadToken, "");
const {baseUrl} = await photoapi.getMediaItem(oAuth2Client, mediaItem.id);
return baseUrl
};
module.exports.capture = async (devicePort = 0, ext = ".png") => {
const cap = new cv.VideoCapture(devicePort);
const frame = cap.read();
return cv.imencode(ext, frame);
};
Loading

0 comments on commit 68fa0d0

Please sign in to comment.