Skip to content

Instantly share code, notes, and snippets.

@mtmtcode
Created April 2, 2020 07:24
Show Gist options
  • Save mtmtcode/0bd676a9a657971f0337327dbb52d8ae to your computer and use it in GitHub Desktop.
Save mtmtcode/0bd676a9a657971f0337327dbb52d8ae to your computer and use it in GitHub Desktop.
テレビを起動してNHKをつけるやつ
function doGet() {
const token = PropertiesService.getScriptProperties().getProperty("REMO_API_TOKEN");
if (!token) {
throw new Error('REMO_API_TOKEN must be set as script property');
}
Logger.log("=== begin ===");
postTv(token, "power"); // 電源ON
sleep(20000);
postTv(token, "home"); // メニュー表示
sleep(20000);
postTv(token, "right"); // 「メディア」選択
sleep(800);
postTv(token, "down");
sleep(350);
postTv(token, "ok"); // 「ビデオ」を選択して決定(初期状態で「フォト」が選択されている想定)
sleep(3000);
postTv(token, "ok"); // 「bdrec」で決定
sleep(15000);
postTv(token, "ok"); // 「VIDEO」で決定
sleep(2000);
// 「ライブチューナー」を選択して決定
postTv(token, "down");
sleep(350);
postTv(token, "down");
sleep(350);
postTv(token, "ok");
sleep(2000);
// 「地上デジタル」で決定
postTv(token, "ok");
sleep(5000);
// 「NHK総合1・札幌(032)を選択して決定
// 選択がうまくいかないことがあるので1回多く移動しとく
postTv(token, "down");
sleep(350);
postTv(token, "down");
sleep(350);
postTv(token, "down");
sleep(350);
postTv(token, "down");
sleep(350);
postTv(token, "down");
sleep(350);
postTv(token, "down");
sleep(350);
postTv(token, "down");
sleep(350);
postTv(token, "ok");
return ContentService.createTextOutput('OK');
}
function sleep(ms: number) {
Utilities.sleep(ms);
}
function postTv(token: string, button_name: string): void {
const applianceID = PropertiesService.getScriptProperties().getProperty("REMO_APPLIANCE_ID");
if (!applianceID) {
throw new Error('REMO_APPLIANCE_ID must be set as script property');
}
const options: GoogleAppsScript.URL_Fetch.URLFetchRequestOptions = {
method: "post",
headers: {
"Authorization": "Bearer " + token,
"Accept": "application/json"
},
payload: {"button": button_name}
};
Logger.log(`Sending ${button_name}`);
var res = UrlFetchApp.fetch(`https://api.nature.global/1/appliances/${applianceID}/tv`, options);
Logger.log(`${res.getResponseCode()} ${res.getContentText()}`);
var headers = res.getHeaders();
Logger.log(`x-rate-limit-remaining:${headers['x-rate-limit-remaining']} x-rate-limit-reset:${new Date(headers['x-rate-limit-reset']*1000)}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment