Created
April 21, 2017 16:03
-
-
Save Mossuru777/02834dff14939c337e24a03586351926 to your computer and use it in GitHub Desktop.
ChinachuとMirakurunが何も使われていないかどうかを返す
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
{ | |
"chinachu": { | |
"base_url": "http://<chinachu-address:port>/" | |
}, | |
"mirakurun": { | |
"base_url": "http://<mirakurun-address:port>/" | |
} | |
} |
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
const http = require("http"); | |
const https = require("https"); | |
const url = require("url"); | |
const config = require("./config.json"); | |
function isMirakurunTunerUsed() { | |
return new Promise(function (resolve, reject) { | |
const api_url = url.parse(url.resolve(config.mirakurun.base_url, "api/Status")); | |
const http_or_https = api_url.protocol === "https:" ? https : http; | |
http_or_https.get( | |
api_url, | |
function (res) { | |
if (res.statusCode !== 200) { | |
console.error("Mirakurun API (Status) HTTPError: " + res.status); | |
reject(); | |
} | |
res.setEncoding("utf8"); | |
let body = ""; | |
res.on("data", function (chunk) { | |
body += chunk; | |
}); | |
res.on("end", function () { | |
const json = JSON.parse(body); | |
const result = json.streamCount.tunerDevice > 0; // Mirakurunのチューナーを使用しているストリームがあるかどうか | |
return resolve(result); | |
}); | |
} | |
).on("error", function (e) { | |
console.error("Mirakurun API (Status) AccessError: " + e.message); | |
reject(); | |
}); | |
}); | |
} | |
function isThereUserConnectedToChinachuWUI() { | |
return new Promise(function (resolve, reject) { | |
const api_url = url.parse(url.resolve(config.chinachu.base_url, "api/status.json")); | |
const http_or_https = api_url.protocol === "https:" ? https : http; | |
http_or_https.get( | |
api_url, | |
function (res) { | |
if (res.statusCode !== 200) { | |
console.error("Chinachu API (status.json) HTTPError: " + res.status); | |
reject(); | |
} | |
res.setEncoding("utf8"); | |
let body = ""; | |
res.on("data", function (chunk) { | |
body += chunk; | |
}); | |
res.on("end", function () { | |
const json = JSON.parse(body); | |
const result = json.connectedCount > 0; // Chinachu WUIにアクセス中のユーザーがいるかどうか | |
return resolve(result); | |
}); | |
} | |
).on("error", function (e) { | |
console.error("Chinachu API (status.json) AccessError: " + e.message); | |
reject(); | |
}); | |
}); | |
} | |
function hasScheduledRecordingSoon() { | |
return new Promise(function (resolve, reject) { | |
const api_url = url.parse(url.resolve(config.chinachu.base_url, "api/reserves.json")); | |
const http_or_https = api_url.protocol === "https:" ? https : http; | |
http_or_https.get( | |
api_url, | |
function (res) { | |
if (res.statusCode !== 200) { | |
console.error("Chinachu API (reserves.json) HTTPError: " + res.status); | |
reject(); | |
} | |
res.setEncoding("utf8"); | |
let body = ""; | |
res.on("data", function (chunk) { | |
body += chunk; | |
}); | |
res.on("end", function () { | |
const json = JSON.parse(body); | |
const current_time = new Date().getTime(); | |
const result = json.length > 0 && json[0].start - 1800000 <= current_time; // 録画予約があり、直近の録画予約開始時刻が30分以内かどうか | |
return resolve(result); | |
}); | |
} | |
).on("error", function (e) { | |
console.error("Chinachu API (reserves.json) AccessError: " + e.message); | |
reject(); | |
}); | |
}); | |
} | |
// main | |
const tasks = [isMirakurunTunerUsed(), isThereUserConnectedToChinachuWUI(), hasScheduledRecordingSoon()]; | |
Promise.all(tasks).then(function (results) { | |
let exit_code = 0; | |
for (let i = 0; i < tasks.length; i++) { | |
if (results[i]) { | |
// チェック対象の3関数のうち1つでもTrueが返ってきたときは | |
// ハイバネートできないので、exit_codeに加算 | |
exit_code += 1 << i; | |
} | |
} | |
process.exit(exit_code); | |
}).catch(function () { | |
// 何らかのエラーが起きてるよ | |
process.exit(255); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment