Skip to content

Instantly share code, notes, and snippets.

@hushin
Created November 19, 2019 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hushin/bba403bb59e8124882c757fefeb7c16c to your computer and use it in GitHub Desktop.
Save hushin/bba403bb59e8124882c757fefeb7c16c to your computer and use it in GitHub Desktop.
// http://tateren.hateblo.jp/entry/2016/10/03/025425 を参考にしています
// ブクログのアカウント情報
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();
}
Logger.log("cookies: " + cookies);
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.filter(function (x, i, self) {
return self.indexOf(x) === i;
});
}
function login(){
var LOGIN_URL = 'https://booklog.jp/login';
var options = {
method : "post",
followRedirects : false,
headers: {
referer: 'https://booklog.jp/login'
},
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 = 'https://booklog.jp/input';
var options = {
method : "post",
payload : {
"isbns" : asins.join("\n"),
"category_id" : "0", //カテゴリなし
"status" : "4", //積読
"tags": ""
},
headers : {
"referer": 'https://booklog.jp/input',
"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