Last active
January 2, 2018 10:32
-
-
Save somidad/0c980bd6971f8e333fd0c45c7ac30e7f to your computer and use it in GitHub Desktop.
Google Scholar Alert (G)Mails to RSS feeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var LABEL = '<your GMail label name storing Google Scholar Alert>' | |
var RECEIVER = '<your Blogger e-mail address>' | |
function get_quota() { | |
var quota = MailApp.getRemainingDailyQuota() | |
Logger.log(quota) | |
return quota | |
} | |
function myFunction() { | |
var label = GmailApp.getUserLabelByName(LABEL) | |
var idx = 0 | |
var threads_old = null | |
while (true) { | |
var threads = label.getThreads(idx, 1) | |
if (!threads[0]) { | |
// Maybe idx reaches the end of the label. Exit | |
return | |
} | |
if (threads == threads_old) { | |
// Stuck. Exit | |
return | |
} | |
threads_old = threads | |
var messages = threads[0].getMessages() | |
// check threads are Google Scholar Alert | |
var from = messages[0].getFrom() | |
if (!is_google_scholar(from)) { | |
idx = idx + 1 | |
continue | |
} | |
for (var j = 0; j < messages.length; j++) { | |
var body = messages[j].getBody() | |
var draft_arr = [] | |
while (true) { | |
// per feed | |
var pos_start = body.search('<h3') | |
var pos_end = body.search('</h3>') | |
if (pos_start < 0 || pos_end <= pos_start) { | |
break | |
} | |
//Logger.log(body.substring(pos_start, pos_end)) | |
var title_url = find_title_url(body.substring(pos_start, pos_end)) | |
Logger.log(title_url['title']) | |
Logger.log(title_url['url']) | |
body = body.substring(pos_end + 5) | |
// pos_end = body.search('</div>') | |
// var author_pub = find_author_pub(body.substring(0, pos_end)) | |
var author_pub = find_author_pub(body) | |
Logger.log(author_pub) | |
body = body.substring(pos_end + 6) | |
var abstract = find_abstract(body) | |
Logger.log(abstract) | |
var draft = GmailApp.createDraft(RECEIVER, | |
title_url['title'], | |
'', | |
{htmlBody: title_url['url'] + '<br/><br/>' + author_pub + '<br/><br/>' + abstract}) | |
draft_arr.push(draft) | |
} | |
if (draft_arr.length < get_quota()) { | |
for (var k = 0; k < draft_arr.length; k++) { | |
var sent = draft_arr[k].send() | |
if (sent) { | |
sent.moveToTrash() | |
} | |
} | |
messages[j].moveToTrash() | |
} else { | |
Logger.log('Not enough quota. Try on next trigger') | |
for (var k = 0; k < draft_arr.length; k++) { | |
draft_arr[k].deleteDraft() | |
} | |
return | |
} | |
} | |
} | |
} | |
function is_google_scholar(from) { | |
return from.indexOf('[email protected]') >= 0 | |
} | |
function find_title_url(title_html) { | |
var pos = title_html.search('<a href="') | |
title_html = title_html.substring(pos + 9) | |
pos = title_html.search('"') | |
var url = title_html.substring(0, pos) | |
// pos = title_html.search('<font size=3') | |
// title_html = title_html.substring(pos) | |
pos = title_html.search('>') | |
title_html = title_html.substring(pos + 1) | |
// pos = title_html.search('</font>') | |
pos = title_html.search('</a>') | |
title = title_html.substring(0, pos) | |
return {title: title, url: url} | |
} | |
function find_author_pub(author_pub_html) { | |
// var pos = author_pub_html.search('<font') | |
var pos = author_pub_html.search('<div style') | |
author_pub_html = author_pub_html.substring(pos) | |
pos = author_pub_html.search('>') | |
author_pub_html = author_pub_html.substring(pos + 1) | |
// pos = author_pub_html.search('</font>') | |
pos = author_pub_html.search('</div>') | |
author_pub = author_pub_html.substring(0, pos) | |
return author_pub | |
} | |
function find_abstract(abstract_html) { | |
// var pos = abstract_html.search('<font') | |
var pos = abstract_html.search('<div class') | |
abstract_html = abstract_html.substring(pos) | |
pos = abstract_html.search('>') | |
abstract_html = abstract_html.substring(pos + 1) | |
// pos = abstract_html.search('</font>') | |
pos = abstract_html.search('</div>') | |
abstract = abstract_html.substring(0, pos) | |
return abstract | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment