-
Notifications
You must be signed in to change notification settings - Fork 2
/
updateRangesUri.js
58 lines (52 loc) · 1.84 KB
/
updateRangesUri.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
// Retrieve
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/open_shakespeare", function(err, db) {
if(!err) {
console.log("connected successfully to mongodb://localhost:27017/open_shakespeare");
updateAnnotationsRangesUri(db);
} else {
console.error("Error connecting to mongodb://localhost:27017/open_shakespeare");
}
});
//update the Ranges and URI to match DOM structure
function updateAnnotationsRangesUri(db) {
var annotations = db.collection('annotations');
annotations.find().toArray(function(err, results) {
if(!err) {
results.forEach(function(annotation){
if(annotation.ranges) {
//extract title from URI to make a relative pathname
var titleStart = (annotation._source.uri).search('/work') + 6,
title = (annotation._source.uri).slice(titleStart),
uri = '/#works/' + title;
//edit here to create a filepath relative to your DOM structure
var start = '/div[2]/div[1]/div[2]/div[2]' + annotation.ranges[0].start;
var end = '/div[2]/div[1]/div[2]/div[2]' + annotation.ranges[0].end;
annotations.update(
//update the changes in the db.
{'_id': annotation._id},
{
$set: {
'uri': uri,
'ranges.0.start': start,
'ranges.0.end': end
}
},
{safe: true},
function(err, result){
if(!err) {
console.log('Success!');
} else {
console.log('Error updating annotation ranges for %s', annotation._id);
}
}
);
};
});
console.log("Complete!");
} else {
console.error("Error querying annotations collection:", err );
}
});
}