Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// ブクログのアカウント情報
var scriptProperties = PropertiesService.getScriptProperties();
var ACCOUNT = scriptProperties.getProperty('BOOKLOG_ACCOUNT');
var PASSWORD = scriptProperties.getProperty('BOOKLOG_PASSWORD');
function AutoRegistToBooklog() {
// 受信トレイにある注文メールを検索
var query = "from:digital-no-reply@amazon.co.jp in:inbox"
var threads = GmailApp.search(query);
if (threads.length > 0 ) {
var cookies = login();
}
threads.forEach(function(thread) {
var asins = getAsin(thread);
Logger.log("asins: " + asins);
if(typeof asins !== 'undefined') {
input(cookies, asins)
}
// メールを既読&アーカイブ
thread.markRead().moveToArchive();
})
}
function getAsin(thread) {
var body = thread.getMessages()[0].getBody();
var asins = body.match(/dp%2F.{10}/g);
for (var i = 0; i < asins.length; i++){
asins[i] = asins[i].slice(-10)
}
return asins
}
function login(){
var LOGIN_URL = 'https://booklog.jp/login';
var options = {
method : "post",
followRedirects : false,
payload : {
"account" : ACCOUNT,
"password": PASSWORD
}
};
var response = UrlFetchApp.fetch(LOGIN_URL, options);
var headers = response.getAllHeaders();
if ( typeof headers['Set-Cookie'] !== 'undefined' ) {
var cookies = typeof headers['Set-Cookie'] == 'string' ? [ headers['Set-Cookie'] ] : headers['Set-Cookie'];
for (var i = 0; i < cookies.length; i++) {
cookies[i] = cookies[i].split( ';' )[0];
};
}
return cookies;
}
function input(cookies, asins) {
var INPUT_URL = 'http://booklog.jp/input';
var options = {
method : "post",
payload : {
"isbns" : asins.join("\n"),
"category_id" : "0", //カテゴリなし
"status" : "4" //積読
},
headers : {
"Cookie" : cookies.join(';')
}
};
var response = UrlFetchApp.fetch(INPUT_URL, options);
var expression = /.*tc(?:pink|blue) t10M.*/g
var results= response.getContentText().match(expression);
results.forEach(function(result) {
Logger.log("result: " + result.match(/>(.*)</)[1])
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment