Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
休日だけhueの操作を変えるGAS(作りかけ)
var account = {
'email': '',
'password': ''
};
var bridgeid = '';
var initializeParameter = {
'devicetype' : 'GAS',
'username' : 'newdeveloper'
};
if (!this.console) {
var console = {
log : function () {
Logger.log.apply(Logger, arguments);
}
}
}
function isHoliday() {
var day = (new Date('2014-3-22')).getDay();
if (day === 0 || day === 6) {
return true;
}
var cal = CalendarApp.getCalendarById("ja.japanese#holiday@group.v.calendar.google.com");
if (cal.getEventsForDay(new Date()).length) {
return true;
}
return false;
}
function getPlaySession (headers, callback) {
Object.keys(headers).filter(function (key) {
return key.replace(/\W/g, '').match(/SetCookie/i);
}).map(function (key) {
return headers[key].filter(function (cookie) {
return cookie.match(/PLAY_SESSION/i);
}).map(function (sessions) {
return sessions.split(/;/).filter(function (session) {
return session.match(/PLAY_SESSION/i);
}).map(function (session) {
return session.match(/^\w+="(.+?)"$/).pop();
});
});
}).forEach(callback);
}
function WakeUp () {
getAccessToken(account, bridgeid, function (accessToken, sessionId) {
sendCommand(accessToken, sessionId, bridgeid, {
'url' : '/api/0/api',
'method' : 'POST',
'body' : initializeParameter
}, function (response) {
sendCommand(accessToken, sessionId, bridgeid, {
'url' : '/api/0/lights/3/state',
'method' : 'PUT',
'body' : {
'on' : false
}
}, function (response) {
console.log(response);
});
});
});
}
function getStatus (accessToken, bridgeid, callback) {
var token = encodeURIComponent(accessToken);
var bridge = encodeURIComponent(bridgeid);
var response = UrlFetchApp.fetch('https://www.meethue.com/api/getbridge?token='+token+'&bridgeid='+bridge, {
'contentType' : 'application/x-www-form-urlencoded'
});
callback(response);
}
function sendCommand (accessToken, sessionId, bridgeid, command, callback) {
var token = encodeURIComponent(accessToken);
var response = UrlFetchApp.fetch('https://www.meethue.com/en-US/user/sendMessageToBridge?token='+token, {
'contentType' : 'application/x-www-form-urlencoded',
'method' : 'post',
'headers' : {
'cookie' : 'PLAY_SESSION="'+sessionId+'";'
},
'payload' : 'clipmessage=' + JSON.stringify({
'bridgeId' : bridgeid,
'clipCommand' : command
})
});
callback(response);
}
function getSessionId (account, bridgeid, callback) {
var response = UrlFetchApp.fetch('http://www.meethue.com/en-US/api/gettoken?devicename=iPhone+5&appid=hueapp&deviceid=' + bridgeid);
var headers = response.getAllHeaders();
getPlaySession(headers, function (sessionId) {
var response = UrlFetchApp.fetch('https://www.meethue.com//en-US/api/getaccesstokengivepermission', {
'method' : 'post',
'payload' : account,
'headers' : {
'cookie' : 'PLAY_SESSION="'+sessionId+'";'
}
});
var headers = response.getAllHeaders();
getPlaySession(headers, function (sessionId) {
callback(sessionId);
});
});
}
function getAccessToken (account, bridgeid, callback) {
var response = UrlFetchApp.fetch('http://www.meethue.com/en-US/api/gettoken?devicename=iPhone+5&appid=hueapp&deviceid=' + bridgeid);
var headers = response.getAllHeaders();
getPlaySession(headers, function (sessionId) {
var response = UrlFetchApp.fetch('https://www.meethue.com//en-US/api/getaccesstokengivepermission', {
'method' : 'post',
'payload' : account,
'headers' : {
'cookie' : 'PLAY_SESSION="'+sessionId+'";'
}
});
var headers = response.getAllHeaders();
getPlaySession(headers, function (sessionId) {
var response = UrlFetchApp.fetch('https://www.meethue.com/en-US/api/getaccesstokenpost', {
'headers' : {
'cookie' : 'PLAY_SESSION="'+sessionId+'";'
}
});
var match = (response + '').match(/phhueapp:\/\/sdk\/login\/(.+?)"/);
var accessToken = match.pop();
callback(accessToken, sessionId);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment