This repository has been archived by the owner on Aug 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
129 lines (118 loc) · 3.23 KB
/
index.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
var VERSION = '0.0.3',
request = require('request'),
querystring = require('querystring').stringify;
function shorturl(longurl, shorter, params, callback) {
// callback passed in params
if ( typeof params === 'function' ) {
callback = params;
params = {};
}
// callback passed in shorter
if ( typeof shorter === 'function' ) {
callback = shorter;
params = {};
shorter = 'is.gd';
// params passed in shorter
} else if ( typeof shorter === 'object' ) {
params = shorter;
shorter = 'is.gd';
}
// check shorter validity
if ( shorter in shorteners ) {
// short circuit the rest of the checks to avoid match()
} else if ( !shorter ) {
shorter = 'is.gd';
} else if ( shorter.match(/^https?:\/\/.*%@/) ) {
params.url = shorter;
shorter = 'string';
} else {
shorter = 'is.gd';
}
if ( typeof callback === 'function' ) {
shorteners[shorter](longurl, params, callback);
} else {
// FIXME: explode usefully here?
}
}
module.exports = shorturl;
var shorteners = {
'arseh.at': function(longurl, params, callback) {
params.url = 'http://arseh.at/api.php?action=shorturl&format=simple&url=%@';
shorteners['string'](longurl, params, callback);
},
'bit.ly': function(longurl, params, callback) {
if ( !(params.login && params.apiKey) ) {
callback(new Error("bit.ly requires a user and apiKey for authorisation."));
return;
}
var uri = 'https://api-ssl.bit.ly/v3/shorten?' + querystring({
login: params.login || null,
apiKey: params.apiKey || null,
longUrl: longurl,
format: 'json'
});
request({uri:uri}, function(error, response, body) {
if ( response && response.statusCode === 200 ) {
try {
var json = JSON.parse(body);
if ( json.data && json.data.url )
callback(json.data.url, json);
} catch(e) {
// FIXME: e vs. error? kinda stupid
callback(e, error, response, body);
}
} else {
callback.call(arguments);
}
});
},
'goo.gl': function(longurl, params, callback) {
request({
uri: 'https://www.googleapis.com/urlshortener/v1/url',
method: 'POST',
json: {longUrl:longurl}
}, function(error, response, body) {
if ( response && response.statusCode === 200 && body.id ) {
callback(body.id);
} else {
callback.call(arguments);
}
});
},
'is.gd': function(longurl, params, callback) {
var host = 'http://is.gd/';
if ( 'host' in params && ['is.gd', 'v.gd'].indexOf(params.host) >= 0 ) {
host = 'http://' + params.host + '/';
delete params.host;
}
var uri = host + 'create.php?' + querystring({
format: 'simple',
url: longurl
});
request({uri:uri}, function(error, response, body) {
if ( response && response.statusCode === 200 && body.substr(0, host.length) === host ) {
callback(body);
} else {
callback.call(arguments);
}
});
},
'v.gd': function(longurl, params, callback) {
params.host = 'v.gd';
shorteners['is.gd'](longurl, params, callback);
},
'string': function(longurl, params, callback) {
if ( !('url' in params) ) {
callback();
return;
}
var uri = params.url.replace('%@', escape(longurl));
request({uri:uri}, function(error, response, body) {
if ( response && response.statusCode === 200 ) {
callback(body);
} else {
callback.call(arguments);
}
});
}
};