Skip to content

Instantly share code, notes, and snippets.

@juzim
Last active March 30, 2019 07:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juzim/af0ef80f1233de51614d88551514b0ad to your computer and use it in GitHub Desktop.
Save juzim/af0ef80f1233de51614d88551514b0ad to your computer and use it in GitHub Desktop.
Google apps script for parsing the URL of the free ebook from the packtpub newsletter
function extractPacktpubUrl() {
try {
var ss = SpreadsheetApp.getActiveSheet();
var label = GmailApp.getUserLabelByName("packt_free_ebook");
var threads = label.getThreads();
if (threads[0] == undefined || threads[0].getMessages()[0] == undefined) {
Logger.log('No valid message found');
return;
}
var currentThread = threads[0];
var msg = currentThread.getMessages()[0];
Logger.log('Parsing ' + msg.getSubject());
var regExp = /(https.*\/packt\/free-ebook\/.*)\?/;
var freeBookData = regExp.exec(msg.getPlainBody());
if (!freeBookData) {
Logger.log('No ebook data found in mail');
} else {
var url = [freeBookData[1]];
ss.clear();
ss.appendRow(url);
Logger.log('Added URL ' + url);
}
} catch (err) {
GmailApp.sendEmail(YOUR_MAIL, 'Packtpub URL extractor error: ' + err.name, err.message + "\n\n" + err.stack);
return;
}
const newLabel = GmailApp.createLabel(label.getName() + '_processed');
currentThread.addLabel(newLabel);
currentThread.removeLabel(label);
currentThread.moveToArchive()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment