-
Notifications
You must be signed in to change notification settings - Fork 2
/
omg-add.js
executable file
·92 lines (80 loc) · 2.36 KB
/
omg-add.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
#!/usr/bin/env node
var Parse = require('parse').Parse,
comm = require('commander'),
request = require('request'),
inquirer = require('inquirer'),
validator = require('validator'),
cheerio = require('cheerio');
var models = require(__dirname + '/models'),
auth = require(__dirname + '/auth'),
spinner = require(__dirname + '/spinner'),
tagger = require(__dirname + '/tagger');
comm.parse(process.argv);
function fetchBookmark(user, addr, title) {
var query = new Parse.Query(models.Bookmark);
query.equalTo("url", addr);
query.first().then(function(object) {
var bookmark;
if (object) {
console.log('URL already stored'.grey);
bookmark = object;
} else {
bookmark = new models.Bookmark();
bookmark.setACL(new Parse.ACL(user));
bookmark.set('url', addr);
}
bookmark.set('title', title);
return tagger.selectTags(bookmark);
}).then(function(bookmark) {
console.log('OK'.green.bold);
process.exit(0);
});
}
function promptTitle(user, addr) {
var inqTitle = {name:'title', message:'Title:'};
inquirer.prompt([inqTitle], function(input) {
fetchBookmark(user, addr, input.title);
});
}
function add(user, addr) {
if (!validator.isURL(addr)) {
console.error(('Silly human, "'+addr+'" is not really a real URL').red);
process.exit(1);
}
spinner.start(('Fetching "'+addr+'"').grey);
request(addr, function (error, response, body) {
spinner.stop();
if (!error && response.statusCode === 200) {
var $ = cheerio.load(body),
title = $('title').text(),
meta = $('meta[name][content]');
console.log(
'URL'.blue.bold + ': '.grey +
addr.green +
'\nTitle'.blue.bold + ': '.grey +
title.green
);
for (var m = 0; m < meta.length; m++) {
console.log(
'Meta'.blue.bold + '-'.grey +
meta.eq(m).attr('name').blue.bold + ': '.grey +
meta.eq(m).attr('content').green
);
};
fetchBookmark(user, addr, title);
} else if (!error && response.statusCode !== 200) {
console.error(('"'+addr+'" returned HTTP status '+response.statusCode).red);
promptTitle(user, addr);
} else if (error.code === 'ENOTFOUND') {
console.error(('Silly human, "'+addr+'" does not exist').red);
process.exit(1);
} else {
console.error(error.message.red);
}
});
}
auth.session(function(user) {
for (var i = 0; i < comm.args.length; i++) {
add(user, comm.args[i]);
};
});