-
Notifications
You must be signed in to change notification settings - Fork 0
/
databraryapi.js
163 lines (147 loc) · 4.86 KB
/
databraryapi.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
const qs = require('qs')
const request_mod = require('request')
const logger = require('./logger')
const path = require('path')
const url = require('url')
const fs = require('fs')
const API_URL = 'https://nyu.databrary.org/api'
const request_cookie = request_mod.jar();
const request = request_mod.defaults({jar:request_cookie})
function get_all_db_session(volume_id) {
const uri = API_URL + '/volume/' + volume_id + '?containers'
const options = {
url: url.parse(uri),
method: 'GET',
headers: {
'User-Agent': 'request'
}
}
return new Promise(function (resolve, reject) {
request(options, function(error, response, body){
if(response.statusCode === 200) {
resolve(JSON.parse(body)['containers'])
} else {
reject(`Status Code ${response.statusCode}`)
}
})
}).catch(function(error){
logger.error(`Error: ${error}`)
})
}
function connect_to_db(username, password) {
let connected = false
const uri = API_URL + '/user/login'
const payload = {
"email": username,
"password": password
}
const options = {
method: 'POST',
url: url.parse(uri),
resolveWithFullResponse: true,
body: payload,
json: true // Automatically stringifies the body to JSON
};
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (response.statusCode === 200) {
logger.debug(`User ${username} successfully connected to Databrary`)
resolve(true)
} else {
reject(`Status Code ${response.statusCode}`)
}
})
}).catch(function(error){
logger.error(`Error: ${error}`)
})
}
function logout_from_db() {
const uri = API_URL + '/user/logout'
const options = {
method: 'POST',
url: url.parse(uri),
resolveWithFullResponse: true,
}
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (response.statusCode === 200) {
logger.debug(`User successfully disconnected`)
resolve(true)
} else {
reject(`Status Code ${response.statusCode}`)
}
})
}).catch(function(error){
logger.error(`Error: ${error}`)
})
}
function get_session_assets(volume_id, session_id) {
const qsObj= qs.parse('assets=1')
const uri = API_URL + '/volume/' + volume_id + '/slot/' + session_id
const options = {
url: url.parse(uri),
method: 'GET',
qs: qsObj,
headers: {
'User-Agent': 'request'
}
}
return new Promise(function (resolve, reject) {
request.get(options, function (error, response, body) {
if (response.statusCode === 200) {
resolve(JSON.parse(body)["assets"])
} else {
reject(`Status Code ${response.statusCode}`)
}
})
}).catch(function(error){
logger.error(`Error: ${error}`)
});
}
function get_volume_csv(volume_id, download_dir) {
try {
fs.statSync(download_dir)
logger.debug(`Directory ${download_dir} exists`)
} catch(e) {
fs.mkdirSync(download_dir)
logger.debug(`Make ${download_dir} Directory`)
}
/* Create an empty file where we can save data */
const uri = 'https://nyu.databrary.org/volume/'+volume_id+'/csv'
const options = {
url: url.parse(uri),
method: 'GET',
headers: {
'User-Agent': 'request'
}
}
return new Promise(function (resolve, reject) {
request(options, function (error, response, body) {
if (response.statusCode === 200) {
var filename, contentDisp = response.headers['content-disposition'];
if (contentDisp && /^attachment/i.test(contentDisp)) {
filename = contentDisp.toLowerCase()
.split('filename=')[1]
.split(';')[0]
.replace(/"/g, '');
} else {
filename = path.basename(url.parse(uri).path);
}
logger.info(`File name ${filename}`);
fs.createWriteStream(path.join(download_dir, filename)).write(body);
resolve(path.join(download_dir, filename))
} else {
reject(`Status Code ${response.statusCode}`)
}
})
}).catch(function(error){
logger.error(`Error: ${error}`)
});
}
module.exports = {
get_session_assets: get_session_assets,
logout_from_db: logout_from_db,
connect_to_db: connect_to_db,
get_all_db_session: get_all_db_session,
get_volume_csv: get_volume_csv
}