Skip to content

Instantly share code, notes, and snippets.

@tateren
Last active April 22, 2021 03:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tateren/d24699fdfaabec9c971c3037279a753f to your computer and use it in GitHub Desktop.
Save tateren/d24699fdfaabec9c971c3037279a753f to your computer and use it in GitHub Desktop.
// ブクログのアカウント情報
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
},
headers : {
"Referer" : LOGIN_URL
}
};
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" //積読
},
headers : {
"Cookie" : cookies.join(';'),
"Referer" : INPUT_URL
}
};
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