Skip to content

Instantly share code, notes, and snippets.

@Kohsaku-Tobioka
Last active August 10, 2024 12:30
Show Gist options
  • Save Kohsaku-Tobioka/dcd837f01f7a127da6200c75aed2737f to your computer and use it in GitHub Desktop.
Save Kohsaku-Tobioka/dcd837f01f7a127da6200c75aed2737f to your computer and use it in GitHub Desktop.
function fetchArxivPapers() {
var url = createQueryUrl();
var xml = UrlFetchApp.fetch(url).getContentText();
var document = XmlService.parse(xml);
var root = document.getRootElement();
var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
var arxiv = XmlService.getNamespace('http://arxiv.org/schemas/atom');
var entries = root.getChildren('entry', atom);
var message = 'Here is the weekly digest of ArXiv:\n\n';
// Select a random index from the list of entries
var luckyIndex = Math.floor(Math.random() * entries.length);
var luckyMessage = '';
var otherMessages = '';
for (var i = 0; i < entries.length; i++) {
var arxivlink = entries[i].getChild('id', atom).getText();
var title = entries[i].getChild('title', atom).getText();
var abstract = entries[i].getChild('summary', atom).getText();
var category = entries[i].getChild('primary_category', arxiv).getAttribute('term').getValue();
var authors = entries[i].getChildren('author', atom);
var author_names = [];
for (var j = 0; j < authors.length; j++) {
author_names.push(authors[j].getChild('name', atom).getText());
}
var paperMessage = '';
if (i == luckyIndex) {
// This is the lucky paper
paperMessage += '🎉 Lucky Paper 🎉\n';
} else {
// Limit the abstract to 700 characters
if (abstract.length > 700) {
abstract = abstract.substring(0, 700) + '...';
}
}
paperMessage += 'Title: ' + title + '\n';
paperMessage += 'Authors: ' + author_names.join(', ') + '\n';
paperMessage += 'Category: ' + category + '\n';
paperMessage += 'Link: ' + arxivlink + '\n \n';
paperMessage += 'Abstract: ' + abstract + '\n\n';
if (i == luckyIndex) {
luckyMessage += paperMessage;
} else {
otherMessages += paperMessage;
}
}
message += luckyMessage + '\n' + otherMessages;
sendEmails(message);
console.log(message)
}
function createQueryUrl(maxResults = 100, sortBy = 'submittedDate', sortOrder = 'descending') {
var baseUrl = 'http://export.arxiv.org/api/query?';
// List of keywords
var keywords = ['%22Higgs+Effective+Field%22'];
var keyAuthors = ['%22Zohar+Komargodski%22', '%22Edward+Witten%22'];
// Combine keywords using the OR operator
var keywordQuery = '(' + keywords.join(' OR ') + ')';
var authorQuery = '(' + keyAuthors.join(' OR ') + ')';
// Set the date range to the last 1 week
var today = new Date();
var oneWeekAgo = new Date();
oneWeekAgo.setDate(today.getDate() - 7);
var todayStr = Utilities.formatDate(today, Session.getScriptTimeZone(), 'yyyyMMdd');
var oneWeekAgoStr = Utilities.formatDate(oneWeekAgo, Session.getScriptTimeZone(), 'yyyyMMdd');
var dateRangeQuery = 'submittedDate:[' + oneWeekAgoStr + '0630 TO ' + todayStr + '1645]';
// Combine the category, keyword, and date range queries
var searchQuery = "( (cat:hep-ph AND (ti:" + keywordQuery + " OR abs:" + keywordQuery + ") ) OR au:" + authorQuery + " ) AND " + dateRangeQuery;
var query = 'search_query=' + searchQuery;
maxResults = 'max_results=' + maxResults;
sortBy = 'sortBy=' + sortBy;
sortOrder = 'sortOrder=' + sortOrder;
var url = baseUrl + query + '&' + maxResults + '&' + sortBy + '&' + sortOrder;
//console.log(url)
return url;
}
function sendEmails(message) {
var emailAddress = 'wrhitemyemail@gmail.com';
var subject = 'ArXiv Weekly';
MailApp.sendEmail(emailAddress, subject, message, {
body: message,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment