-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.js
More file actions
158 lines (113 loc) · 3.99 KB
/
web.js
File metadata and controls
158 lines (113 loc) · 3.99 KB
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
var SERVICE_PORT = 3000;
var express = require('express');
var request = require('request');
var bodyParser = require('body-parser');
var Dropbox = require('dropbox');
var xml2js = require('xml2js');
// Instantiate Parser
var xmlParser = new xml2js.Parser();
var xmlBuilder = new xml2js.Builder();
// Init App, Include Form Data Processor
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
// Set Up Front-End Path
app.use("/", express.static(__dirname+"/static"));
// Initialize Dropbox API Connection
// An access token from Dropbox's developer website must be specified as an env variable
var dbx = new Dropbox({
accessToken: process.env.TD_DROPBOX_TOKEN
});
// Handle POST requests to /download path
app.post('/download', function(req, res) {
// Both of these should be inclued in the POST data
var category = req.body.section;
var date = req.body.date;
var xmlFileName = category+".xml";
var xmlUrl = 'https://tuftsdaily.com/xml/category/'+category+'/date/'+date;
request({
'method': "GET",
'url': xmlUrl
}, function (xmlError, xmlResponse, xmlBody) {
if (!xmlError && xmlResponse.statusCode == 200) {
xmlParser.parseString(xmlBody, function(parseError, parseResult) {
if (!parseError) {
var xmlObj = JSON.parse(JSON.stringify(parseResult));
var photos = getPhotoListing(xmlObj);
photos.map(function(photoURL) {
request({
'method': "GET",
'url': photoURL,
'encoding': null
}, function(photoError, photoResponse, photoBody) {
// Get the filename of the photo
var photoFileName = photoURL.substr(photoURL.lastIndexOf('/')+1);
if (!photoError && photoResponse.statusCode == 200) {
// Copy the photo contents to Dropbox
var photoPath = '/CurrentDay/' + category + '/' + photoFileName;
dbx.filesUpload({ path: photoPath, contents:photoBody, mode:'overwrite' })
.then(function(photoDbxResponse) {
console.log("Success Downloading Photo: " + photoPath)
console.log(photoDbxResponse);
})
.catch(function(photoDbxError) {
console.log("Dropbox Error on " + photoUrl);
console.log(photoDbxError);
});
} else {
console.log("Photo Get Error on "+photoFileName);
console.log(photoError);
}
})
})
// Save the original XML file to Dropbox
var xmlPath = '/CurrentDay/' + category + '/' + xmlFileName;
dbx.filesUpload({ path: xmlPath, contents: xmlBody, mode:'overwrite' })
.then(function(xmlDbxResponse) {
console.log("Success Downloading XML: " + xmlPath);
console.log(xmlDbxResponse);
})
.catch(function(xmlDbxError) {
console.log("Dropbox Error on " + xmlUrl);
console.log(xmlDbxError);
});
// Photos Downloaded in Background
console.log("Success Loading XML: "+xmlFileName);
res.set('Content-Type', 'text/json');
res.send(JSON.stringify(xmlObj));
} else {
console.log("XML Parse Error on "+xmlFileName);
console.log(parseError);
res.sendStatus(500);
}
});
} else {
console.log("XML Get Error on " + xmlUrl);
console.log(xmlError);
res.status(500);
}
});
});
var server = app.listen(process.env.PORT || SERVICE_PORT, function () {
var host = server.address().address;
var port = server.address().port;
console.log('App Listening at http://%s:%s', host, port);
});
var getPhotoListing = function(xmlObj) {
var photosToDownload = [];
// If Something Wrong with XML, Don't Return any Photos
if (!xmlObj.hasOwnProperty('section') ||
!xmlObj.section.hasOwnProperty('article')) {
console.log('Malformed XML in getPhotoListing()');
return [];
}
// For Each Article, Create a List of Attached Photos
xmlObj.section.article.map(function(article) {
// If Photos Attached, Get them All
if (article.hasOwnProperty('photo')) {
article.photo.map(function(photo) {
photosToDownload.push(photo['$'].href);
})
}
});
return photosToDownload;
}