-
Notifications
You must be signed in to change notification settings - Fork 120
/
crawl_rosetta_images.js
146 lines (124 loc) · 4.08 KB
/
crawl_rosetta_images.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
'use strict';
let https = require('https');
var $ = require('cheerio');
let fs = require('fs');
let archiveUrl = 'https://planetgate.mps.mpg.de/Image_of_the_Day/public/IofD_archive.html';
let rootUrl = 'https://planetgate.mps.mpg.de/Image_of_the_Day/public/';
let missionName = '"ROSETTA"';
let targetName = '"67P"';
let instrumentHostName = '"ROSETTA-ORBITER"';
let instrumentId = '"NAVCAM"';
let detectorType = '"CAMERA"';
let downloadFolder = "rosettaimages";
function getHttps(url, cb) {
https.get(url, (res) => {
let body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
cb(body);
});
});
}
function padZeros(str, nZeros) {
str = '' + str;
while (str.length < nZeros) str = '0' + str;
return str;
}
function formatDate(date) {
return date.getFullYear() + '-' +
padZeros(date.getMonth(), 2) + '-' +
padZeros(date.getDate(), 2) + 'T' +
padZeros(date.getHours(), 2) + ':' +
padZeros(date.getMinutes(), 2) + ':' +
padZeros(date.getSeconds(), 2) + '.' +
padZeros(date.getMilliseconds(), 3);
}
function parseDate(str) {
let year = str.substr(0, 4);
let month = str.substr(5, 2);
let day = str.substr(8, 2);
let hours = str.substr(11, 2);
let minutes = str.substr(14, 2);
let seconds = str.substr(17, 2);
let milliseconds = str.substr(20, 3);
return new Date(year, month, day, hours, minutes, seconds, milliseconds);
}
function formatLbl(data) {
let maxKeyLength = 0;
Object.keys(data).forEach((key) => {
maxKeyLength = Math.max(maxKeyLength, key.length);
});
let outString = '';
Object.keys(data).forEach((key) => {
let value = data[key];
outString += key;
outString += (new Array(maxKeyLength - key.length + 1)).join(' ');
outString += ' = ';
outString += value;
outString += '\n';
});
outString += 'END';
return outString;
}
getHttps(archiveUrl, (body) => {
let $root = $.load(body);
$root('tr td').map((i, td) => {
let thumbnailName = $(td).find('img').attr('src');
let imageUrl = '';
if (thumbnailName) {
let originalName = thumbnailName.replace('_tn', '');
imageUrl = rootUrl + originalName;
}
let detailsName = $(td).find('a').attr('href');
if (detailsName) {
let detailsUrl = rootUrl + detailsName;
getHttps(detailsUrl, (detailsBody) => {
let $detailsRoot = $.load(detailsBody);
let startTime = '';
let id = '';
let cam = '';
let exposureTime = '';
$detailsRoot('tr').map((i, detailsTr) => {
let header = $(detailsTr).children('th').html();
let cell = $(detailsTr).children('td').html();
switch (header) {
case 'ID': id = cell; break;
case 'Date taken': startTime = cell; break;
case 'Camera': cam = cell; break;
case 'Exposure time': exposureTime = cell; break;
}
});
let exposureSeconds = 0;
if (exposureTime.substr(-2) === ' s') {
exposureSeconds = +exposureTime.substr(0, exposureTime.length - 2);
} else {
throw "exposure time expressed in unexpeded format, " + exposureTime;
}
let startDate = parseDate(startTime);
let stopDate = new Date(startDate.getTime() + exposureSeconds * 1000);
let lblContents = formatLbl({
MISSION_NAME: missionName,
TARGET_NAME: targetName,
INSTRUMENT_HOST_NAME: instrumentHostName,
INSTRUMENT_ID: instrumentId,
DETECTOR_TYPE: detectorType,
START_TIME: formatDate(startDate),
STOP_TIME: formatDate(stopDate)
});
let filenameWithoutSuffix = downloadFolder + '/' + id;
fs.writeFile(filenameWithoutSuffix + '.lbl', lblContents, (err) => {
if (err) {
throw err;
}
});
let suffix = imageUrl.substr(imageUrl.lastIndexOf('.'));
let file = fs.createWriteStream(filenameWithoutSuffix + suffix);
let request = https.get(imageUrl, (res) => {
res.pipe(file);
});
});
}
});
});