-
Notifications
You must be signed in to change notification settings - Fork 0
/
push-project.js
180 lines (132 loc) · 3.36 KB
/
push-project.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var fs = require('fs');
var path = require('path');
var request = require('request');
var useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36';
// silence logging ...
var log = function() {};
// var log = console.log;
// export login and push functions seperately ...
module.exports = {
login: login,
push: create_and_push,
push_project: push_project
};
// login and push
function push_project(username, projectId, callback) {
var jar = request.jar();
login(username, jar,
function(jar) {
create(projectId, jar,
function(jar) {
push(username, projectId, jar,
function(jar) {
log('done.');
callback(jar);
});
});
});
}
function create_and_push(username, projectId, jar, callback) {
jar = jar || request.jar();
create(projectId, jar,
function(jar) {
push(username, projectId, jar,
function(jar) {
log('done.');
callback(jar);
});
});
}
// just login
function login(username, jar, callback) {
// password is defined by an environment variable
var password =process.env.NODEBOX_PASSWORD;
if(!password) {
console.error('$NODEBOX_PASSWORD is not defined!');
process.exit(1);
}
log('logging in as', username);
jar = jar || request.jar();
var url = 'https://nodebox.live/login';
var headers = {
'User-Agent': useragent,
'Content-Type': 'application/x-www-form-urlencoded'
};
var form = {
'login': username,
'password': password
};
request.post({
url: url,
form: form,
headers: headers,
followAllRedirects: true,
jar: jar
},
function (error, response, body) {
if (!error && response.statusCode === 200) {
log( 'logged in successfully' );
callback(jar);
} else {
console.error(body);
}
});
}
function create(projectId, jar, callback) {
log('creating %s.', projectId);
jar = jar || request.jar();
var url = 'https://nodebox.live/projects/create';
var headers = {
'User-Agent': useragent,
'Content-Type': 'application/x-www-form-urlencoded'
};
var form = {
'access': 'public',
'projectId': projectId,
'template': 'graphics'
};
request.post({
url: url,
form: form,
headers: headers,
followAllRedirects: true,
jar: jar
},
function (error, response, body) {
if (!error && response.statusCode === 200 ) {
log( 'project created successfully' );
callback(jar);
} else {
console.error(body);
}
});
}
function push(username, projectId, jar, callback) {
jar = jar || request.jar();
var filename = path.join('projects', projectId + '.json');
var json = fs.readFile( filename, function(err, data) {
if (err) throw err;
log('reading %s', filename);
var json = JSON.parse(data);
var url = [ 'https://nodebox.live', 'api', 'projects', username, projectId ].join('/');
var headers = {
'User-Agent': useragent,
'content-type': 'application/json',
};
request.post({
url: url,
json: json,
headers: headers,
followAllRedirects: true,
jar: jar
},
function (error, response, body) {
if (!error && response.statusCode === 200 ) {
log( 'project pushed successfully' );
callback(jar);
} else {
console.error(body);
}
});
});
}