-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (128 loc) · 4.18 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require('dotenv').load();
var tumblr = require('tumblr.js'),
async = require('async'),
cloudinary = require('cloudinary'),
pathToKeystoneInstall = "/Users/sartois/Documents/PersonalWork/ContrePensees",
keystone = require(pathToKeystoneInstall + '/node_modules/keystone'),
mongoose = require(pathToKeystoneInstall + '/node_modules/keystone/node_modules/mongoose/index');
var tumblrSiteUrl = process.env.TUMBLR_URL,
postFunctionFormat = "handle[POST-TYPE]Post",
authorEmail = process.env.AUTHOR_EMAIL,
queryParams = {
offset: 3,
limit: 50,
type:'photo'
},
client = tumblr.createClient({
consumer_key: process.env.TUMBLR_KEY
});
keystone.init({
'user model': 'User',
'cookie secret': process.env.COOKIE_SECRET,
'cloudinary config': process.env.CLOUDINARY_URL
});
keystone.import('../ContrePensees/models');
cloudinary.config({
cloud_name: 'contre-pensees',
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
var lib = require('./lib/common')(cloudinary),
adapter = require('./lib/tumblrAdapater')(keystone.list('Post'), cloudinary, lib),
User = keystone.list('User');
mongoose.connect('localhost', 'contre-pensees');
var db = mongoose.connection;
db.on('error', function() {
console.error('connection error:', arguments);
process.exit(2);
});
db.once('open', function() {
async.waterfall([
//Get author
function(callback) {
User.model.findOne()
.where('email', authorEmail)
.exec()
.then(function(user) {
if (user === null) {
callback("User not found");
} else {
callback(null, user);
}
}, callback);
},
//Launch Tumblr query
function(user, callback) {
client.posts(
tumblrSiteUrl,
queryParams,
function(err, data) {
if (err || ! data) {
callback(err ? err : "Can't find any post");
} else {
callback(null, data, user);
}
}
);
},
//Parse tumblr data and populate a post array
async.apply(handleTumblrPost),
//Save post to db
function(posts, waterfallCallback) {
async.each(posts, function(post, callback) {
if (! "save" in post) {
console.error(post);
callback("No save method");
return;
}
console.log("Save post " + post.key);
post.save(callback);
}, function(err) {
if (err) {
waterfallCallback(err);
} else {
waterfallCallback(null);
}
});
}
], function(err) {
if (err) {
console.error(err);
process.exit(1);
} else {
console.log("Success");
process.exit(0);
}
});
});
/**
* @param {Object} data JSON Tumblr api answer
* @param {Object} author User keystone document
* @param {Fucntion} callback Async waterfall callback
*/
function handleTumblrPost(data, author, callback) {
var postsToSave = [];
async.forEachOfSeries(
data.posts,
function(tpost, i, postCallback) {
if (tpost.state === 'published') {
console.log("Fetch tumblr data");
var handlePostCallbackName = postFunctionFormat.replace(
"[POST-TYPE]",
lib.ucFirst(tpost.type)
);
adapter[handlePostCallbackName](tpost, author, postCallback, postsToSave);
} else {
console.log("Not published", i, tpost.body);
postCallback();
}
},
function(err) {
if (err) {
callback(err);
} else {
callback(null, postsToSave);
}
}
);
};