-
Notifications
You must be signed in to change notification settings - Fork 0
/
tumblr_mailer.js
69 lines (52 loc) · 1.74 KB
/
tumblr_mailer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var fs = require('fs');
var ejs = require('ejs');
var tumblr = require('tumblr.js');
var csvFile = fs.readFileSync("friend_list.csv","utf8");
var emailTemplate = fs.readFileSync('email_template.html', 'utf8');
var latestPosts = [];
function csvParse(csvFile){
var arrayOfObjects = [];
var arr = csvFile.split("\n");
var newObj;
keys = arr.shift().split(",");
arr.forEach(function(contact){
contact = contact.split(",");
newObj = {};
for(var i =0; i < contact.length; i++){
newObj[keys[i]] = contact[i];
}
arrayOfObjects.push(newObj);
});
return arrayOfObjects;
}
var friendList = csvParse(csvFile);
console.log(friendList);
friendList.forEach(function(row){
var firstName = row["firstName"];
var numMonthsSinceContact = row["numMonthsSinceContact"];
var customizedTemplate = ejs.render(emailTemplate, {
firstName: firstName,
numMonthsSinceContact: numMonthsSinceContact,
latestPosts: latestPosts
});
var templateCopy = emailTemplate;
// use .replace to replace FIRST_NAME and NUM_MONTHS_SINCE_CONTACT with firstName and monthsSinceLastContact
templateCopy = templateCopy.replace(/firstName/gi,
firstName).replace(/numMonthsSinceContact/gi, numMonthsSinceContact);
var client = tumblr.createClient({
consumer_key: 'xxxxx',
consumer_secret: 'xxxxx',
token: 'xxxxx',
token_secret: 'xxxxx'
});
});
client.posts('codingmeow.tumblr.com', function(err, blog){
var currentTime = new Date().getTime();
var blogTime = new Date(posts.date).getTime();
var timeDiff = Math.abs(currentTime - blogTime)/24/60/60/1000;
blog.posts.forEach(function(post){
if (timeDiff <= 7) {
latestPosts.push(post);
}
});
});