-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcalais.js
executable file
·78 lines (59 loc) · 1.61 KB
/
calais.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
#!/usr/bin/env node
/*!
* calais
* Copyright(c) 2011 Mike Cantelon
* MIT Licensed
*/
var fs = require('fs')
, path = require('path')
, Calais = require('./lib/calais').Calais
, argv = require('optimist').argv
, iniparser = require('iniparser')
var home = process.env.HOME
// if HOME not set, die
if (home === undefined) {
console.log('Error: HOME environmental variable not defined.')
process.exit(1)
}
iniparser.parse(home + '/.calais', function (err, data) {
var config = (err) ? false : data
// deal with command line input
if (argv['_'].length == 1) {
var api_key = (argv['k'] && (argv['k'] != true))
? argv['k']
: config.api_key
if (!api_key) {
var help = ''
help += "Please specify an OpenCalais API key using the -k option.\n"
help += "A default key may be specified by setting 'api_key' in an ini\n"
help += "file at $HOME/.calais."
console.log(help)
process.exit(1)
}
var file = argv['_'][0]
fs.exists(file, function (exists) {
if (exists) {
var content = fs.readFileSync(file)
var calais = new Calais(api_key, {
'cleanResult': true
})
calais.set('content', content)
calais.fetch(function (error, result) {
if (error) {
console.log('Error attempting to fetch data.');
} else {
console.log(result)
}
})
}
else {
console.log("Error: file doesn't exist.")
process.exit(1)
}
})
}
else {
console.log('Usage: calais -k <API key> <filename>')
process.exit(1)
}
})