-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from TUS-OSK/photoapi
Photoapi
- Loading branch information
Showing
9 changed files
with
1,140 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
node_modules | ||
capture.jpg | ||
.envrc | ||
token.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,33 @@ | ||
# bushitsuchan-PC | ||
OSKの部室の様子を様子をオンラインで確認できるプロジェクト 部室ちゃん. | ||
その部室に置いてあるPC側で動かすプログラム. | ||
その部室に置いてあるPC側で動かすプログラム. | ||
|
||
## Google Photos APIs を使えようにする | ||
1. [Photos Library API](https://console.developers.google.com/apis/library/photoslibrary.googleapis.com)を有効にする. | ||
1. [認証情報](https://console.developers.google.com/apis/credentials)でOAuth クライアント IDを作成する (アプリケーションの種類 は その他) | ||
1. 作成した`クライアント ID`と`クライアント シークレット`を`.envrc`に以下のように保存する | ||
|
||
```shell | ||
export client_id="477...oav.apps.googleusercontent.com" | ||
export client_secret="yEP..." | ||
``` | ||
4. `direnv`の有効化方法に従う` | ||
Macでbashなら | ||
```shell | ||
direnv allow | ||
``` | ||
|
||
## カメラを使えるようにする | ||
Macなら | ||
```shell | ||
brew install imagesnap | ||
``` | ||
Ubuntuは | ||
```shell | ||
sudo apt-get install fswebcam | ||
``` | ||
|
||
## Usage | ||
1. `npm install` | ||
1. `npm start` | ||
を実行 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,59 @@ | ||
"use strict"; | ||
|
||
const photoapi = require("./photoAPI"); | ||
const {capture} = require("./caputure"); | ||
const slack = require("./slack"); | ||
|
||
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 oAuth2Client = await photoapi.getOAuthToken(client_id, client_secret); | ||
|
||
//共有するためアルバムを指定 | ||
const albumTitle = "bushitsuchan_album"; | ||
const albums = await photoapi.getAlbumList(oAuth2Client); | ||
let album = albums.filter((album) => album.title === albumTitle)[0]; | ||
|
||
if (album === undefined) { | ||
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) | ||
}; | ||
|
||
|
||
if (require.main === module) { | ||
main().catch(console.error) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use strict"; | ||
|
||
const photoapi = require("./photoAPI"); | ||
const NodeWebcam = require('node-webcam'); | ||
const slack = require("./slack"); | ||
|
||
|
||
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 | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"web": { | ||
"client_id": "", | ||
"client_secret": "" | ||
} | ||
} |
Oops, something went wrong.