Skip to content

Instantly share code, notes, and snippets.

@pastak
Last active April 8, 2019 13:36
Show Gist options
  • Save pastak/3f188ccdfc61ef9451e7bdfe0ba3f962 to your computer and use it in GitHub Desktop.
Save pastak/3f188ccdfc61ef9451e7bdfe0ba3f962 to your computer and use it in GitHub Desktop.
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
<title>pastakのKindle本購入履歴</title>
<updated><?= getLastUpdated().toISOString() ?></updated>
<author>
<name>Pasta-K</name>
</author>
<id>https://docs.google.com/spreadsheets/d/1870lEluCFFovzkXKCOLrfjQnj2kdMbVs-CSVDaQghnk/</id>
<?
for(var i=0;i<20;i++){
if(!getRowData(i)[1]) continue;
?>
<entry>
<title><?= getRowData(i)[2] ?></title>
<link href="<?= getRowData(i)[3] ?>tag=pastalablog-22"/>
<id><?= getRowData(i)[3] ?></id>
<published><?= getRowData(i)[1].toISOString() ?></published>
<updated><?= getRowData(i)[1].toISOString() ?></updated>
</entry>
<? } // End For ?>
</feed>
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var range;
function parseURLQuery (url) {
var o = {};
var t = url.split('?')[1].split('&');
t.forEach(function (a) {
var t = a.split('=');
o[t[0]] = t[1]
})
return o;
}
// https://stackoverflow.com/questions/25735677/remove-duplicates-values-in-array-google-apps-script/25740182#25740182
function removeDups(array) {
var outArray = [];
array.sort(lowerCase);
function lowerCase(a,b){
return a[0].toLowerCase()>b[0].toLowerCase() ? 1 : -1;// sort function that does not "see" letter case
}
outArray.push(array[0]);
for(var n in array){
if(outArray[outArray.length-1][0].toLowerCase()!=array[n][0].toLowerCase()){
outArray.push(array[n]);
}
}
return outArray;
}
function getLastUpdated() {
return sheet.getRange('B2').getValue();
}
function getRowData(rowNum){
return range.getValues()[rowNum];
}
function doGet() {
range = sheet.getRange(2, 1, 20, 20);
// テンプレート呼び出し(rssTemplate.html)
var output = HtmlService.createTemplateFromFile('atomTemplate');
var result= output.evaluate();
// コンテントタイプ指定
return ContentService.createTextOutput(result.getContent())
.setMimeType(ContentService.MimeType.XML);
}
function myFunction() {
var threads = GmailApp.search('subject:(Amazon.co.jp ご注文の確認) Kindle本 ご購入ありがとうございます。', 0, 20);
var mails = GmailApp.getMessagesForThreads(threads);
var lastMailId = sheet.getRange('A2').getValue();
var info = [];
var amazonRegexp = /^https?:\/\/www\.amazon\.co\.jp\/dp\/([\W\w\d]+)\//;
for(var i=0;i < mails.length;i++){
var msg = mails[i][0];
var date = msg.getDate();
var id = msg.getSubject().replace('Amazon.co.jp ご注文の確認 ','');
if (id === lastMailId) break;
var doc = Xml.parse(msg.getBody(), true);
var bodyHtml = doc.html.body.toXmlString();
doc = XmlService.parse(bodyHtml);
var xml = doc.getRootElement();
var booksInfo = removeDups(
HTMLParser.getElementsByTagName(xml, 'a')
.map(function (a) { return [a.getAttribute('href').getValue(), a] })
.map(function (a) { return [decodeURIComponent(parseURLQuery(a[0])['U']), a[1]] })
.filter(function (a) { return amazonRegexp.test(a[0]) })
.map(function (a) { return [a[0].replace(/ref=.+$/, ''),a[1]] })
.map(function (a) { return [a[0],a[1], a[0].replace(amazonRegexp, '$1')] })
.map(function (a) { return [a[0], a[1].getText(), id, date, a[2]] })
);
info.push(booksInfo);
}
info.reverse().forEach(function (booksInfo) {
for (var j = 0; j < booksInfo.length;j++) {
sheet.insertRowBefore(2);
sheet.getRange(2, 1, 1, 5).setValues([
[booksInfo[j][2], booksInfo[j][3], booksInfo[j][1], booksInfo[j][0], booksInfo[j][4]]
]);
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment